改成下面这段:
class StartL implements ActionListener {
public void actionPerformed(ActionEvent e) {
for(int i = 1; i <= 5; i++) {
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
System.err.println("Interrupted");
}
t.setText(t.getText()+Integer.toString(i));
}
}
}
class StartL implements ActionListener {
public void actionPerformed(ActionEvent e) {
for(int i = 1; i <= 5; i++) {
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
System.err.println("Interrupted");
}
t.setText(t.getText()+Integer.toString(i));
}
}
}
你继承Thread类试试。——————————————————
个人想法,没实验过,你试试
——————————————————
应该在另外一个线程中设置刷新才可以
public void setText(String t)Sets the text of this TextComponent to the specified text. If the text is null or empty, has the effect of simply deleting the old text. When text has been inserted, the resulting caret location is determined by the implementation of the caret class.
This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.
2、刚接触线程,有许多概念还不是很清楚,按我的理解,执行Thread.sleep(1000);语句的时候线程sleep了,那执行t.setText(Integer.toString(i));语句的瞬间,主线程应该是active的吧。不知我的理解是否正确?
3、不知Thread safe是个什么概念,哪位大虾能帮忙解释几句?