对于当前的项目,我们需要允许用户每5秒左右按一次按钮。我们使用一个按钮来启动打印作业,但我们需要阻止用户发送垃圾邮件按钮并启动十几个打印作业。
我们目前正在尝试使用下面的代码,但它似乎在按钮被禁用的情况下仍然会出现点击。因此,在5秒的延迟后,即使在按钮被禁用的时间内,点击也会被记录下来。
private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {
Button1.setEnabled(false);
pressCount++;
System.out.println("Press count: " + pressCount);
PrintJob print = new PrintJob();
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(GUIFrame.class.getName()).log(Level.SEVERE, null, ex);
}
try {
print.PrintJob();
} catch (IOException ex) {
Logger.getLogger(GUIFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}发布于 2012-10-15 21:00:14
编程按钮在java中最多每5秒按一次
Swing JButton#setMultiClickThreshhold(long threshhold)
Swing Action添加到JButton中,而不是` `ActionListenerand by using [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) to block [isEnable`](http://docs.oracle.com/javase/7/docs/api/javax/swing/Action.html#isEnabled%28%29)发布于 2012-10-15 20:50:34
不要让美国东部夏令时等待5秒。您应该使用另一个线程休眠5秒,并启用该按钮的设置。如下所示:
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// handle it
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Button1.setEnabled(true);
}
});
}
}).start();https://stackoverflow.com/questions/12895870
复制相似问题