Java 自动更新jtextfield
有人知道如何每5秒钟更新一次jTextfield吗?自动的。因此不需要用户输入。它用于更新文本字段中的时间。这是我尝试过的,但后来我的程序冻结了Java 自动更新jtextfield,java,swing,concurrency,jtextfield,event-dispatch-thread,Java,Swing,Concurrency,Jtextfield,Event Dispatch Thread,有人知道如何每5秒钟更新一次jTextfield吗?自动的。因此不需要用户输入。它用于更新文本字段中的时间。这是我尝试过的,但后来我的程序冻结了 while(true){ Thread.sleep(1000); txtCheck.setText(convert.getCheck()); System.out.println("Ja");
while(true){
Thread.sleep(1000);
txtCheck.setText(convert.getCheck());
System.out.println("Ja");
}
convert是一个线程,我试图抛出一个异常,但失败了,因为Eclise说线程不能抛出异常
Convert.getCheck:
public String getCheck() {
return check;
}
您希望使用Swing计时器对象。这是一个 首先,您需要让类实现ActionListener接口。在类内部,还需要实现actionPerformed()方法。最后,应该在main()函数或其他地方启动计时器
timer = new Timer(5000, MyClass);
timer.setInitialDelay(pause);
timer.start();
然后,您将实现您的类,如:
public class MyClass implements ActionListener
{
...
void actionPerformed(ActionEvent e) {
/*
This is called every time the timer fires, put your code here
*/
}
}
您希望使用Swing计时器对象。这是一个 首先,您需要让类实现ActionListener接口。在类内部,还需要实现actionPerformed()方法。最后,应该在main()函数或其他地方启动计时器
timer = new Timer(5000, MyClass);
timer.setInitialDelay(pause);
timer.start();
然后,您将实现您的类,如:
public class MyClass implements ActionListener
{
...
void actionPerformed(ActionEvent e) {
/*
This is called every time the timer fires, put your code here
*/
}
}
您正在显示的while循环在哪里?在main()中,在JFrame中,在JPanel中?您正在显示的while循环在哪里?在main()中,在JFrame中,在JPanel中?非常感谢!工作完美!非常感谢你!工作完美!