我的登录GUI表单(用javax.swing
创建(需要一些帮助,基本上我想(有点阻止(用户在10次失败尝试时登录一段时间,它从5秒开始,第一次一切都很好,但当尝试达到>10我想增加等待时间,如何使时间增加5秒?,更清楚地说,我想做这个
10次失败尝试,登录表单被禁用,您必须等待5秒!然后重试
11尝试失败,登录表单被禁用,您必须等待10秒!然后重试
我为此尝试了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();
}
}
但我没有想出增加等待时间的方法。
这将是第一次完美的工作,但我如何才能增加5s的时间,为另一次失败的尝试?在这种情况下,使用timer
是最好的解决方案吗?
感谢
您实际上根据您的注释将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();
}
}