程序中只有四行注释,源程序是没有那四行注释的,也就是用一个线程来执行edt.incrementLabel()的方法。但我把线程去了,只用循环,为什么程序运行的就和以前不一样了呢?我线程学的不好,谁能告诉我下
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SwingThreading extends JFrame implements ActionListener {
private JLabel counter;
private int tickCounter = 0;
private static SwingThreading edt;
public SwingThreading() {
super("Swing Threading");
JButton freezer = new JButton("Increment");
freezer.addActionListener(this);
counter = new JLabel("0");
add(freezer, BorderLayout.CENTER);
add(counter, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
incrementLabel();
}
private void incrementLabel() {
tickCounter++;
Runnable code = new Runnable() {
public void run() {
counter.setText(String.valueOf(tickCounter));
}
};
if (SwingUtilities.isEventDispatchThread()) {
code.run();
} else {
SwingUtilities.invokeLater(code);
}
}
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
edt = new SwingThreading();
edt.setVisible(true);
// new Thread(new Runnable() {
// public void run() {
while (true) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
edt.incrementLabel();
}
// }
// }).start();
}
});
}
}