[英]Encountering problems with Thread.sleep in Swing
This program was written to count from 0 to 1000 but it just goes straight to 1000 without displaying the counting process. 编写该程序的目的是从0到1000进行计数,但是它会直接显示1000,而不显示计数过程。 I have written similar code using a progress bar and
Thread.sleep()
method and it works perfectly. 我已经使用进度条和
Thread.sleep()
方法编写了类似的代码,并且效果很好。
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class project extends JFrame implements ActionListener {
JButton CountUpButton = new JButton("Count up");
JButton CountDownButton = new JButton("Count Down");
JButton ResetButton = new JButton("Reset");
JTextField NumberField = new JTextField();
int count = 0;
public project(){
setLayout(new GridLayout(1, 4));
setSize(500, 300);
add(NumberField);
add(CountUpButton);
add(CountDownButton);
add(ResetButton);
CountUpButton.addActionListener(this);
CountDownButton.addActionListener(this);
ResetButton.addActionListener(this);
NumberField.setText("0");
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent a){
if(CountUpButton.hasFocus()){
count = Integer.parseInt(NumberField.getText());
try{
while(count < 1000){
count = count + 1;
NumberField.setText(Integer.toString(count));
Thread.sleep(100);
}
}catch(InterruptedException r){
r.printStackTrace();
}
}
if(CountDownButton.hasFocus()){
count = Integer.parseInt(NumberField.getText());
try{
while(count > 0){
count -= 1;
NumberField.setText(Integer.toBinaryString(count));
Thread.sleep(100);
}
}catch(InterruptedException r){
r.printStackTrace();
}
}
if(ResetButton.hasFocus()){
NumberField.setText("0");
}
}
public static void main(String args[]){
new project();
}
}
Any long-running task should be run in a separate thread. 任何长时间运行的任务都应在单独的线程中运行。 Your use of Thread.sleep certainly qualifies as long-running.
您对Thread.sleep的使用肯定可以长期运行。
By running in the Swing user-interface thread, no updates can be rendered in that user-interface until your code completes. 通过在Swing用户界面线程中运行,在代码完成之前,无法在该用户界面中呈现任何更新。 Instead your counting should be spawned in another thread.
相反,您的计数应在另一个线程中产生。 That other thread should periodically update the user interface in a thread-safe manner using the SwingWorker.
该其他线程应该使用SwingWorker以线程安全的方式定期更新用户界面。
Study up on launching threads , executors such as a ScheduledExecutorService
, the Swing event-dispatch thread (EDT), and SwingWorker. 研究启动线程 ,诸如
ScheduledExecutorService
执行 ScheduledExecutorService
,Swing 事件调度线程 (EDT)和SwingWorker。
A simpler approach might be the Swing Timer class ( Tutorial ), not to be confused with java.util.Timer . 一个更简单的方法可能是Swing Timer类 ( Tutorial ),不要与java.util.Timer混淆。 It will do much of the thread-handling work for you.
它将为您完成很多线程处理工作。 But I have no experience with it.
但是我没有经验。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.