Java 使用计时器在按钮用完时自动单击按钮?
我有两个按钮,一个可以忽略,另一个可以打击犯罪。我想给用户10秒钟的时间来做出选择。如果他们在10秒钟内没有这样做,我希望ignore自动单击并执行代码。我该怎么做呢?我一直在努力让计时器工作。java中有一个Java 使用计时器在按钮用完时自动单击按钮?,java,swing,button,timer,Java,Swing,Button,Timer,我有两个按钮,一个可以忽略,另一个可以打击犯罪。我想给用户10秒钟的时间来做出选择。如果他们在10秒钟内没有这样做,我希望ignore自动单击并执行代码。我该怎么做呢?我一直在努力让计时器工作。java中有一个javax.swing.Timer类。 在创建Timer类的对象时,它将在给定的时间后执行事件。。以毫秒为单位 您还必须为事件提供ActionListener // javax.swing.Timer(timeAfterWhichExecuteTheEventInMillis
javax.swing.Timer
类。
在创建Timer
类的对象时,它将在给定的时间后执行事件。。以毫秒为单位
您还必须为事件提供ActionListener
// javax.swing.Timer(timeAfterWhichExecuteTheEventInMilliseconds, ListenerForTheEvent)
javax.swing.Timer timer = new javax.swing.Timer(10000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
your_button.doClick();
}
});
我将在屏幕加载时启动一个线程:
//class creation
public class MyThread extends Thread {
public void run() {
//disable the button that the user has to press
button.setEnable(false);
//add what you want to show to make him know that he loose
//so execute your code or call a function
}
}
//On your main class execution, do the following when screen loaded
MyThread timerThread = new MyThread();
timerThread.execute();
//continue with your code
您甚至还没有告诉我们您使用的是什么GUI,您到底使用什么为用户提供这些按钮,您尝试了什么以及失败的地方。基于这么少的信息,我们应该如何帮助您呢?了解
javax.swing.Timer
。。。。它可能会帮助你..你能更详细地介绍一下你的代码将如何解决这个问题吗?作为一段没有解释的代码,这不是很有帮助。