界面由七个组件组成,两个标签,两个文本输入框,两个按钮,一个标签用来显示通知。
两个标签是用来显示“用户名”和“密码”;
两个文本输入框用来实现账户和密码的输入;
两个按钮分别具有登录和重置功能;
最后的标签用来通知登陆成功与否。
这串代码用来比较账户和密码是否匹配,主要目的是用来学习swing,所以这个很浅
```java
class LoginCheck{
private String name;
private String password;
public LoginCheck(String name,String password){
this.name = name;
this.password = password;
}
public boolean validate(){
if ("java".equals(name) && "yyds".equals(password)){
return true;
}else{
return false;
}
}
}
下面这串代码是界面的绘制代码
class User_Login{
private JFrame frame = new JFrame("欢迎您");
private JButton submit = new JButton("登录");
private JButton reset = new JButton("重置");
private JLabel nameLab = new JLabel("用户名:");
private JLabel passLab = new JLabel("密 码:");
private JLabel infoLab = new JLabel("用户登录系统");
private JTextField nameText = new JTextField();
private JPasswordField passText = new JPasswordField();
public User_Login(){
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == submit){
String name = nameText.getText();
String password = new String(passText.getPassword());
LoginCheck login = new LoginCheck(name,password);
if(login.validate()){
infoLab.setText("登录成功,欢迎光临");
}else{
infoLab.setText("账号或密码错误");
}
}
}
});
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == reset){
nameText.setText(null);
passText.setText(null);
infoLab.setText("用户登录系统");
}
}
});
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("成功关闭");
System.exit(1);
}
});
frame.setLayout(null);
nameLab.setBounds(5,5,60,20);
passLab.setBounds(5,30,60,20);
infoLab.setBounds(5,65,220,30);
nameText.setBounds(65,5,100,20);
passText.setBounds(65,30,100,20);
submit.setBounds(165,5,60,20);
reset.setBounds(165,30,60,20);
frame.add(nameLab);
frame.add(passLab);
frame.add(infoLab);
frame.add(nameText);
frame.add(passText);
frame.add(submit);
frame.add(reset);
frame.setSize(280,130);
frame.setLocation(400,300);
frame.setVisible(true);
}
}
class MyActionEventDemo1{
public static void main(String[] args) {
new User_Login();
}
}
所有代码仅仅只能实现登录界面的绘制和通知的显示,这就是全部功能(自己为了学习理解swing敲的)