Swing登录错误3次锁定窗口

2 篇文章 0 订阅
1 篇文章 0 订阅

使用Swing实现登录错误三次锁定窗口
一、Swing页面

import java.awt.*;

public class Login {

    //在类中定义主函数
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //在主函数中,实例化Login类的对象,调用初始化界面的方法
        Login login = new Login();
        login.initUI();

    }

    //在类中定义初始化界面的方法
    public void initUI() {
        //在initUI中实例化JFrame类的对象
        JFrame frame = new JFrame();
        //设置窗体对象的属性值
        //设置窗体标题
        frame.setTitle("Login");
        //设置窗体大小,只对顶层容器生效
        frame.setSize(400, 250);
        //设置窗体关闭操作,3表示关闭窗体退出程序
        frame.setDefaultCloseOperation(3);
        //设置窗体相对于另一组间的居中位置,参数null表示窗体相对于屏幕的中央位置
        frame.setLocationRelativeTo(null);
        //禁止调整窗体大小
        frame.setResizable(false);
        frame.setFont(new Font("宋体",Font.PLAIN,14));
        //实例化FlowLayout流式布局类的对象,指定对齐方式为居中对齐组件之间的间隔为10个像素
        FlowLayout fl = new FlowLayout(FlowLayout.CENTER,10,10);
        //实例化流式布局类的对象
        frame.setLayout(fl);

        //实例化JLabel标签对象,该对象显示“账号”
        JLabel labname = new JLabel("账号:");
        labname.setFont(new Font("宋体",Font.PLAIN,14));
        //将labname标签添加到窗体上
        frame.add(labname);

        //实例化JTextField标签对象化
        JTextField text_name = new JTextField();
        Dimension dim1 = new Dimension(300,30);
        //设置除顶级容器组件以外其他组件的大小
        text_name.setPreferredSize(dim1);
        //将textName标签添加到窗体上
        frame.add(text_name);

        //实例化JLabel标签对象,该对象显示“密码”
        JLabel labpass = new JLabel("密码:");
        labpass.setFont(new Font("宋体",Font.PLAIN,14));
        //将labpass添加到窗体上
        frame.add(labpass);

        //实例化JPasswordField
        JPasswordField text_password = new JPasswordField();
        //设置大小
        text_password.setPreferredSize(dim1);
        //添加到窗体
        frame.add(text_password);

        //实例化JButton组件
        JButton button1 = new JButton();
        //设置按键的显示内容
        Dimension dim2 = new Dimension(100,30);
        button1.setText("登录");
        button1.setFont(new Font("宋体",Font.PLAIN,14));
        //设置按键大小
        button1.setSize(dim2);
        frame.add(button1);


        frame.setVisible(true);

        LoginListener ll = new LoginListener(frame,text_name,text_password);
        button1.addActionListener(ll);
    }
}


二、逻辑代码实现
这里使用Swing的定时器建议不要使用 定时时间过长好像又问题

public class LoginListener implements ActionListener {
    private JTextField text_name;
    private JPasswordField text_password;
    private JFrame login;

    /**
     * 出错次数最多为三次
     */
    private static int i = 3;


    public LoginListener(JFrame login, JTextField text_name, JPasswordField text_password) {
        //获取登录界面、账号密码输入框对象
        this.login = login;
        this.text_name = text_name;
        this.text_password = text_password;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Dimension dim3 = new Dimension(300, 30);

        //生成新界面
        JFrame login2 = new JFrame();
        login2.setSize(400, 200);
        login2.setDefaultCloseOperation(3);
        login2.setLocationRelativeTo(null);
        //宋体,正常风格,14号字体
        login2.setFont(new Font("宋体", Font.PLAIN, 14));
        //创建组件
        JPanel jp1 = new JPanel();



        if (text_name.getText().equals("admin") && text_password.getText().equals("123456")) {
            JLabel message = new JLabel("登陆成功!");
            //宋体,正常风格,14号字体
            message.setFont(new Font("宋体", Font.PLAIN, 14));
            message.setPreferredSize(dim3);
            jp1.add(message);
            login2.add(jp1, BorderLayout.CENTER);

            login2.setResizable(false);
            login2.setVisible(true);

            //通过我们获取的登录界面对象,用dispose方法关闭它
            login.dispose();
        } else {
            i--;
            if (i==0) {
                Timer timer = new Timer(1000 * 10, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("定时器执行");
                        login.setEnabled(true);
                        i=3;
                    }
                });
                timer.start();
                timer.setRepeats(false);
//                timer.stop();
                login.setEnabled(false);

            }

        }

    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值