Java实现简单GUI登录和注册界⾯本⽂实例为⼤家分享了Java实现简单GUI登录和注册界⾯的具体代码,供⼤家参考,具体内容如下先看效果图:
登陆界⾯:
注册界⾯:
实现代码如下:javascript登录注册界面
⼀、登陆界⾯
package cn.bms.view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;
import ls.GUITools;
/*
* 登录窗⼝
*/
@SuppressWarnings("serial")
public class AdminLogin extends JFrame {
private JPanel contentPanel = new JPanel();
// Label标签存放背景图⽚
private JLabel label;
// 设置按钮组件
private JButton login = new JButton("登录"), register = new JButton("注册");
private JLabel jlb1 = new JLabel("⽤户名:"), jlb2 = new JLabel("密码:"), jlbtitle = new JLabel("登录界⾯");
// 设置⽂本框组件
private JTextField admin = new JTextField(), password = new JTextField();
public AdminLogin() {
this.init();
this.addListener();
}
private void init() {
this.setTitle("管理员登陆界⾯");
this.setSize(500, 350);
<(this);
ImageIcon image1 = new ImageIcon("837878.jpg"); // 界⾯背景图⽚
JLabel backLabel = new JLabel();
backLabel.setIcon(image1);
label = new JLabel(image1);
label.setBounds(0, 0, 1000, 400);
// 在LayeredPane最底层上添加两个带图⽚的标签,并且label2在label上⽅
// 将内容⾯板设置为透明,就能够看见添加在LayeredPane上的背景。
((JPanel) ContentPane()).setOpaque(false);
/*
* 添加组件到contentPanel容器中布局⽅式为⾃由布局。
add(admin);
add(password);
add(login);
add(register);
add(jlb1);
add(jlb2);
add(jlbtitle);
/*
* 组件绝对位置
*/
jlb1.setBounds(50, 130, 90, 25);
jlb1.setForeground(Color.WHITE);
admin.setBounds(95, 130, 300, 25);
password.setBounds(95, 154, 300, 25);
jlb2.setBounds(50, 154, 90, 25);
jlb2.setForeground(Color.WHITE);
register.setBounds(95, 225, 90, 20);
login.setBounds(315, 225, 90, 20);
jlbtitle.setBounds(180, 45, 200, 50);
Font f = new Font("微软雅⿊", Font.BOLD, 30);
jlbtitle.setFont(f);
jlbtitle.setForeground(Color.BLUE);
/*
* 组件透明化
*/
admin.setOpaque(true);
password.setOpaque(true);
contentPanel.setOpaque(false);
getContentPane().add(contentPanel);
/*
* 组件边框颜⾊
*/
textSet(admin);
textSet(password);
}
/*
* JTextField⽂本框设置⽅法.
*/
private void textSet(JTextField field) {
field.setBackground(new Color(255, 255, 255));
field.setPreferredSize(new Dimension(150, 28));
MatteBorder border = new MatteBorder(0, 0, 2, 0, new Color(192, 192, 192)); field.setBorder(border);
}
/*
* 事件监听
*/
private void addListener() {
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Text(), Text());
}
});
register.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
forRegister();
}
});
}
// 登录⽅法
public void forLogin(String admin, String pwd) {
}
// 注册⽅法
public void forRegister() {
}
⼆、注册界⾯
package cn.bms.view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;
import ller.AdminLoginController;
import ls.GUITools;
/*
* 注册窗⼝
*/
@SuppressWarnings("serial")
public class AdminRegister extends JFrame {
private JPanel contentPanel = new JPanel();
// Label标签存放背景图⽚
private JLabel label;
// 设置按钮组件
private JButton ok = new JButton("确定注册"), back = new JButton("返回登录");
private JLabel jlb1 = new JLabel("⽤户名:"), jlb2 = new JLabel("密码:"), jlb3 = new JLabel("确认密码:"),
jlbtitle = new JLabel("注册界⾯");
// 设置⽂本框组件
private JTextField admin = new JTextField(), password1 = new JTextField(), password2 = new JTextField(); public AdminRegister() {
this.init();
this.addListener();
}
private void init() {
this.setTitle("管理员注册界⾯");
this.setSize(500, 350);
<(this);
ImageIcon image1 = new ImageIcon("837878.jpg"); // 界⾯背景图⽚
JLabel backLabel = new JLabel();
backLabel.setIcon(image1);
label = new JLabel(image1);
label.setBounds(0, 0, 1000, 400);
// 在LayeredPane最底层上添加两个带图⽚的标签,并且label2在label上⽅
// 将内容⾯板设置为透明,就能够看见添加在LayeredPane上的背景。
((JPanel) ContentPane()).setOpaque(false);
/*
* 添加组件到contentPanel容器中布局⽅式为⾃由布局。
*/
contentPanel.setLayout(null);
add(admin);
add(password1);
add(password2);
add(ok);
add(back);
add(jlb1);
add(jlb2);
add(jlb3);
add(jlbtitle);
/*
* 组件绝对位置
jlb1.setBounds(40, 130, 90, 25);
jlb1.setForeground(Color.WHITE);
admin.setBounds(95, 130, 300, 25);
password1.setBounds(95, 154, 300, 25);
jlb2.setBounds(40, 154, 90, 25);
jlb2.setForeground(Color.WHITE);
password2.setBounds(95, 178, 300, 25);
jlb3.setBounds(40, 178, 90, 25);
jlb3.setForeground(Color.WHITE);
ok.setBounds(315, 225, 90, 20);
back.setBounds(95, 225, 90, 20);
jlbtitle.setBounds(180, 45, 200, 50);
Font f = new Font("微软雅⿊", Font.BOLD, 30);
jlbtitle.setFont(f);
jlbtitle.setForeground(Color.BLUE);
/*
* 组件透明化
*/
admin.setOpaque(true);
password1.setOpaque(true);
password2.setOpaque(true);
contentPanel.setOpaque(false);
getContentPane().add(contentPanel);
/*
* 组件边框颜⾊
*/
textSet(admin);
textSet(password1);
textSet(password2);
}
/
*
* JTextField⽂本框设置⽅法.
*/
private void textSet(JTextField field) {
field.setBackground(new Color(255, 255, 255));
field.setPreferredSize(new Dimension(150, 28));
MatteBorder border = new MatteBorder(0, 0, 2, 0, new Color(192, 192, 192));
field.setBorder(border);
}
/*
* 事件监听
*/
private void addListener() {
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Text(), Text(), Text());
}
});
back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new AdminLoginController().setVisible(true);
}
});
}
// 实现注册账户⽅法
public void setRegister(String admin, String pwd1, String pwd2) {
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论