一个用Java实现简单的用户登录界面:
成效:
具体代码:
package Code.a;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public class LogIn {
JFrame logframe;
JPanel panel1,panel2,panel3;
JLabel labelusername,labelpassword;
JTextField username;
JPasswordField password;
JButton logbutton;
JButton resetbutton;
public LogIn() {
//创建顶级容器;
logframe = new JFrame("登录");
logframe.setSize(300,200);
//创建三个中间容器panel1,panel2,panel3;
//panel1包括一个label、一个text;
panel1 = new JPanel();
panel1.setSize(300,50); //设置大小
panel1.setBackground(Color.white); //设置背景颜色
panel1.setBorder(BorderFactory.createLoweredBevelBorder()); //设置边框
panel1.setLayout(new FlowLayout()); //设置布局为流式布局
labelusername = new JLabel("用户名");
labelusername.setSize(80,40);
username = new JTextField(20);
username.setSize(200,40);
panel1.add(labelusername);
panel1.add(username);
//panel2包括一个label、一个text;
panel2 = new JPanel();
panel2.setSize(300,50);
panel2.setBackground(Color.white);
panel2.setBorder(BorderFactory.createLoweredBevelBorder());
panel2.setLayout(new FlowLayout());
labelpassword = new JLabel("密码");
labelpassword.setSize(80,40);
password = new JPasswordField(20);
password.setEchoChar('*');
password.setSize(200,40);
panel2.add(labelpassword);
panel2.add(password);
//panel3包括一个label、一个text;
panel3 = new JPanel();
panel3.setSize(300,50);
panel3.setBackground(Color.white);
panel3.setBorder(BorderFactory.createLoweredBevelBorder());
panel3.setLayout(new FlowLayout());
logbutton = new JButton("登录");
logbutton.setSize(50,30);
resetbutton = new JButton("重置");
resetbutton.setSize(50,30);
panel3.add(logbutton);
panel3.add(resetbutton);
//顶级容器添加三个中间容器
logframe.add(panel1);
logframe.add(panel2);
logframe.add(panel3);
logframe.setLayout(new FlowLayout()); //设置布局为流式布局
logframe.setVisible(true); //设置可见
logframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new LogIn();
}
}