我想用線程創建一個倒數計時器(這是我被告知要嘗試的)。我確實製作了UI和所有,但是一旦我添加了線程就凍結了。我試過使用Thread.yield()
,但它沒有奏效。我試圖做的invokeLater()
把戲,我看到了一個不同的問題,但它不斷給我不能convert void to Thread
。線程在做倒計時時凍結UI
每次經過UI後應該更新JTextField。
field = new JTextArea();
Button = new JButton();
Button.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
for (int i = Integer.parseInt(field.getText()); i >= 0; i--)
{
try
{
Thread.sleep(1000);
}
field.setText(Integer.toString(i));
}
}
}
);
你不能睡在UI線程上。 – SLaks
你可能想看看這一個http://stackoverflow.com/questions/40265427/updating-swt-periodically-causes-gui-to-freeze –
請詳細說明我還挺新鮮的使用線程D: – Bruce