jlText.setText("1");
//执行代码
jlText.setText("2");
//执行代码
jlText.setText("3");
//执行代码
jlText.setText("4");
//执行代码
jlText.setText("5");每次执行的时候只有最后才显示5,前面的都被卡死不显示
如果在每次更改之后就显示?
//执行代码
jlText.setText("2");
//执行代码
jlText.setText("3");
//执行代码
jlText.setText("4");
//执行代码
jlText.setText("5");每次执行的时候只有最后才显示5,前面的都被卡死不显示
如果在每次更改之后就显示?
//执行代码
jlText.setText("2");
//执行代码
jlText.setText("3");
//执行代码
jlText.setText("4");
//执行代码
jlText.setText("5");这些都在一个线程中,没有中断,肯定只能显示最后的5
你要把这些放在一个单独的线程中,然后才能看出来效果
import javax.swing.JLabel;public class Frame5 extends JFrame {
private JLabel lab;
public Frame5() {
init();
} private void init() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(100,100);
this.setLocationRelativeTo(null);
lab = new JLabel();
Container cont = this.getContentPane();
cont.add(lab);
}
public void startThread() {
ThreadShow th = new ThreadShow();
new Thread(th, "").start();
}
class ThreadShow implements Runnable {
public void run() {
try {
lab.setText("1");
Thread.sleep(1000);
lab.setText("2");
Thread.sleep(1000);
lab.setText("3");
Thread.sleep(1000);
lab.setText("4");
Thread.sleep(1000);
lab.setText("5");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Frame5 frame = new Frame5();
frame.setVisible(true);
frame.startThread();
}
}看下这个例子吧,
这样你就知道写在另一个线程中有什么不同了
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
jlText.setText("1");
}
});
//执行代码
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
jlText.setText("2");
}
});
//执行代码
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
jlText.setText("3");
}
});
//执行代码
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
jlText.setText("4");
}
});
//执行代码
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
jlText.setText("5");
}
});
} catch (InterruptedException e1) {
e1.printStackTrace();
} catch (InvocationTargetException e1) {
e1.printStackTrace();
}用invokeAndWait让Swing线程完成界面展示后,再往下继续
JAVA和VB,VC这些语言还是有一定区别的
JAVA的语法更追求安全性Swing,你想在长时间计算的同时还能刷新界面
那就必须用到多线程,或者
SwingUtilities.invokeAndWait
具体怎么选楼主自己取舍吧