有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何限制cowndown计时器的操作(例如登录)

我需要一些登录GUI表单的帮助(使用javax.swing),基本上我想(某种程度上阻止)一个用户在10次失败尝试时登录一段时间,它从5秒开始,每次都很好,但当尝试达到>;10我想增加等待时间,如何使时间量增加5秒?,更清楚地说,我想这样做

10 failure attempt, login form is disabled, you have to wait 5 seconds! then try again

11 failure attempt, login form is disabled, you have to wait 10 seconds! then try again

.

.

我为此尝试了javax.swing.Timer

else if(ATTEMPT>10)
    {
        System.out.println("attempt is more than 10");
        try
        {
            javax.swing.JOptionPane.showMessageDialog(null, "you can wait for now", "10 attemtp", 1);
            this.setEnabled(false);
            Timer timer = new Timer(1000, new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent evt)
                {
                    System.out.println("timer started");
                    SECONDS--;                           //SECONDS is an integer =5 in class
                    jLabel6.setText("you can try again in "+SECONDS);
                    jLabel6.setVisible(true);
                    if(SECONDS==0)
                    {
                        setEnabled(true);
                        login_btn.setEnabled(true);
                        ((Timer)evt.getSource()).stop();
                        System.out.println("timer has stopped");
                    }
                }
            });
            timer.start();
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

但我没有找到增加等待时间的方法

这将是第一次完美地工作,但我如何才能为另一次失败尝试增加5秒的时间?在这种情况下,使用timer是最好的解决方案吗

谢谢


共 (1) 个答案

  1. # 1 楼答案

    根据您的评论,您实际上将SECONDS硬编码为5

    SECONDS ;   //SECONDS is an integer =5 in class
    

    因此,等待延迟为5秒(从计时器开始为1000*5=5000毫秒)
    您需要的是一个可变的时间,它取决于尝试的次数,即:SECONDS = 5 * (ATTEMPT - 9);

    • 第10次尝试的秒数=5*1=5
    • 第11次尝试的秒数=5*2=10

    所以对于

    它给出:

    else if(ATTEMPT>10)
        {
            SECONDS = 5 * (ATTEMPT - 9);` // CHANGE HERE
            System.out.println("attempt is more than 10");
            try
            {
                javax.swing.JOptionPane.showMessageDialog(null, "you can wait for now", "10 attemtp", 1);
                this.setEnabled(false);
                Timer timer = new Timer(1000, new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent evt)
                    {
                        System.out.println("timer started");
                        SECONDS ;                           //SECONDS is an integer =5 in class
                        jLabel6.setText("you can try again in "+SECONDS);
                        jLabel6.setVisible(true);
                        if(SECONDS==0)
                        {
                            setEnabled(true);
                            login_btn.setEnabled(true);
                            ((Timer)evt.getSource()).stop();
                            System.out.println("timer has stopped");
                        }
                    }
                });
                timer.start();
            } 
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }