btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("start count");
count();
System.out.println("end count");
}
});
private void count() {
new Thread() {
@Override
public void run() {
for (i = 0; i < 10; i++) {
//将下面runnable对象放入EventQueue队列
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
label.setText(String.valueOf(i));
}
});
try {
Thread.currentThread().sleep(1000);
// Thread.currentThread().sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
private void count1(){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//for循环10次后,事件分发线程才处理完这个事件,然后才能处理label刷新
for(int i=0; i<10; i++){
label.setText(String.valueOf(i));
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}