在下一個代碼中應該有一個錯誤/遺漏,但我看不到主要是因爲我從來沒有使用過線程。有人可以找到我失蹤的東西嗎?這裏有什麼遺漏/錯誤?
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JDialog;
import javax.swing.JLabel;
public class JavaSwingTest extends JDialog {
private JLabel m_countLabel;
private Timer m_timer = new Timer();
private class IncrementCountTask extends TimerTask {
@Override
public void run() {
m_countLabel.setText(Long.toString(System.currentTimeMillis()
/1000));
}
}
private JavaSwingTest() {
createUI();
m_timer.schedule(new IncrementCountTask(), 1000, 1000);
}
private void createUI() {
Button button1 = new Button("Action1");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
doLongOperation();
}
});
add(button1, BorderLayout.NORTH);
m_countLabel = new JLabel(Long.toString(System.currentTimeMillis()
/1000));
add(m_countLabel, BorderLayout.CENTER);
}
/**
* Simulates an operation that takes time to complete.
*/
private void doLongOperation() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// ignored for this test
}
}
/**
* @param args
*/
public static void main(String[] args) {
new JavaSwingTest().setVisible(true);
}
}
這個省略/錯誤是你省略了你問題的一部分,你告訴我們你想要解決的錯誤是什麼。 – 2010-09-29 19:29:40
嗯。您正在設置線程(不是主線程(定時器))的Swing組件的值。 – Dirk 2010-09-29 19:33:52
@Dirk:'setText'是線程安全的極少數Swing方法之一。 – 2010-09-29 19:38:09