I can't seem to get this timer to go off. the program compiles and from my understanding this should ping every 1000ms or 1 second and perform the lines in the actionPerformed{} function.
我似乎无法让这个计时器关闭。程序编译,根据我的理解,这应该每1000毫秒或1秒ping一次并执行actionPerformed {}函数中的行。
public void stringGeneration(String args[]){
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String fullIstring = java.lang.String.valueOf(injectString[0] + injectString[1] + injectString[2] + injectString[3] + injectString[4]);
jLabel3.setText(fullIstring);
System.out.println("output");
}
};
Timer timer = new Timer(1000, taskPerformer);
timer.setRepeats(true);
timer.start();
//Thread.sleep(500);
}
2 个解决方案
#1
0
i just gave you an example and not something to copy paste. But you can try this if you want to try as is. In your case the above example should look like:
我给了你一个例子而不是复制粘贴的东西。但是如果你想按原样尝试,你可以试试这个。在您的情况下,上面的示例应如下所示:
class HeartBeatTask extends TimerTask {
private int timerInterval;
public HeartBeatTask(int timeInterval)
{
this.timerInterval = timeInterval;
}
public void run()
{
String fullIstring = java.lang.String.valueOf(injectString[0] + injectString[1] + injectString[2] + injectString[3] + injectString[4]);
jLabel3.setText(fullIstring);
System.out.println("output");
}
}
Your method will then call the above class like this:
然后您的方法将调用上面的类,如下所示:
public void stringGeneration(String args[]){
HeartBeatTask tt = new HeartBeatTask();
t1.schedule(tt, 0, 1000 * 60 * 2);
}
#2
0
This is how i would to a scheduled task in java:
这就是我对java中的计划任务的看法:
import java.util.TimerTask;
class HeartBeatTask extends TimerTask {
class HeartBeatTask扩展TimerTask {
private int timerInterval;
public HeartBeatTask(int timeInterval)
{
this.timerInterval = timeInterval;
}
public void run()
{
// Your function call to schedule here
}
public static void main(String[] args)
{
java.util.Timer t1 = new java.util.Timer();
HeartBeatTask tt = new HeartBeatTask();
t1.schedule(tt, 0, 1000 * 60 * 2);
}
}
Hope that helps
希望有所帮助
#1
0
i just gave you an example and not something to copy paste. But you can try this if you want to try as is. In your case the above example should look like:
我给了你一个例子而不是复制粘贴的东西。但是如果你想按原样尝试,你可以试试这个。在您的情况下,上面的示例应如下所示:
class HeartBeatTask extends TimerTask {
private int timerInterval;
public HeartBeatTask(int timeInterval)
{
this.timerInterval = timeInterval;
}
public void run()
{
String fullIstring = java.lang.String.valueOf(injectString[0] + injectString[1] + injectString[2] + injectString[3] + injectString[4]);
jLabel3.setText(fullIstring);
System.out.println("output");
}
}
Your method will then call the above class like this:
然后您的方法将调用上面的类,如下所示:
public void stringGeneration(String args[]){
HeartBeatTask tt = new HeartBeatTask();
t1.schedule(tt, 0, 1000 * 60 * 2);
}
#2
0
This is how i would to a scheduled task in java:
这就是我对java中的计划任务的看法:
import java.util.TimerTask;
class HeartBeatTask extends TimerTask {
class HeartBeatTask扩展TimerTask {
private int timerInterval;
public HeartBeatTask(int timeInterval)
{
this.timerInterval = timeInterval;
}
public void run()
{
// Your function call to schedule here
}
public static void main(String[] args)
{
java.util.Timer t1 = new java.util.Timer();
HeartBeatTask tt = new HeartBeatTask();
t1.schedule(tt, 0, 1000 * 60 * 2);
}
}
Hope that helps
希望有所帮助