• 用户登录界面


    public class LoginIn {
        private String name;
        private String password;
        public LoginIn(String name,String password){
            this.name = name;
            this.password = password;
        }
    
        public boolean checkOut() {
            if (("1" .equals(name)) && ("123456".equals(password))){
                return true;
            } else {
                return false;
            }
        }
    }
    package Practice.demo21AWT;
    
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    public class ActionHandle {
        private JFrame frame = new JFrame("用户登录系统");
        private JButton login = new JButton("登录");
        private JButton reset = new JButton("重置");
        private JLabel lab = new JLabel("用户登录系统");
        private JLabel nameLab = new JLabel("用户名:");
        private JLabel passwordLab = new JLabel("密 码:");
        private JTextField nameText = new JTextField();
        private JPasswordField passwordText = new JPasswordField();
    
        public ActionHandle() {
    
            nameLab.setBounds(5,5,60,20);
            passwordLab.setBounds(5,40,60,20);
            nameText.setBounds(65,5,220,30);
            passwordText.setBounds(65,40,220,30);
            login.setBounds(70,100,60,20);
            reset.setBounds(170,100,60,20);
            lab.setBounds(75,150,50,40);
    
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e){
                    System.out.println("窗口关闭");
                }
            });
    
            login.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == login){
                        String name = nameText.getText();
                        String password = new String(passwordText.getPassword());
                        LoginIn log = new LoginIn(name, password);
                        if (log.checkOut() == true){
                            lab.setText("登陆成功!");
                        } else {
                            lab.setText("登录失败!请检查用户名或密码");
                        }
                    }
                }
            });
    
            reset.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == reset){
                        nameText.setText("");
                        passwordText.setText("");
                        lab.setText("用户登录系统");
    
                    }
                }
            });
            frame.add(nameLab);
            frame.add(passwordLab);
            frame.add(nameText);
            frame.add(passwordText);
            frame.add(login);
            frame.add(reset);
            frame.add(lab);
            frame.setSize(350,200);
            frame.setLocation(500,300);
            frame.setVisible(true);
        }
    }
    public class MyActionEventDemo01 {
        public static void main(String[] args) {
            new ActionHandle();
        }
    }

    测试结果如下:

     输入错误用户名或密码时

     点击重置按钮会返回开始时界面

     输入正确的用户名和密码时,显示登录成功

  • 相关阅读:
    Github+Jekyll 搭建个人网站详细教程
    github发布博客
    如何在GitHub部署自己的个人网站
    VS2017git 提交提示错误 Git failed with a fatal error.
    CAD 二次开发----- 块
    Updates were rejected because the remote contains work that you do(gitee报错解决方案)
    关于错误CSC : error CS0006:未能找到元数据文件
    js 四舍五入
    spingboot 邮件模板发送;
    springboot 邮件
  • 原文地址:https://www.cnblogs.com/Gazikel/p/13775496.html
Copyright © 2020-2023  润新知