控件的属性状态怎么立即更新?

Mars.CN 2010-12-06 02:59:10
jlText.setText("1");
//执行代码
jlText.setText("2");
//执行代码
jlText.setText("3");
//执行代码
jlText.setText("4");
//执行代码
jlText.setText("5");

每次执行的时候只有最后才显示5,前面的都被卡死不显示
如果在每次更改之后就显示?
...全文
84 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunyiz 2010-12-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 suolong123 的回复:]
大哥 太麻烦了……
[/Quote]

JAVA和VB,VC这些语言还是有一定区别的
JAVA的语法更追求安全性

Swing,你想在长时间计算的同时还能刷新界面
那就必须用到多线程,或者
SwingUtilities.invokeAndWait
具体怎么选楼主自己取舍吧
Mars.CN 2010-12-06
  • 打赏
  • 举报
回复
大哥 太麻烦了……
sunyiz 2010-12-06
  • 打赏
  • 举报
回复
那就改成这样:
		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线程完成界面展示后,再往下继续
Mars.CN 2010-12-06
  • 打赏
  • 举报
回复
不是这个意思,我想要个类似VB中doEvents这样的东西
sunyiz 2010-12-06
  • 打赏
  • 举报
回复
import java.awt.Container;

import javax.swing.JFrame;
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();
}
}


看下这个例子吧,
这样你就知道写在另一个线程中有什么不同了
Mars.CN 2010-12-06
  • 打赏
  • 举报
回复
代码过来看,界面的东西很少搞,不懂……
sunyiz 2010-12-06
  • 打赏
  • 举报
回复
jlText.setText("1");
//执行代码
jlText.setText("2");
//执行代码
jlText.setText("3");
//执行代码
jlText.setText("4");
//执行代码
jlText.setText("5");

这些都在一个线程中,没有中断,肯定只能显示最后的5
你要把这些放在一个单独的线程中,然后才能看出来效果

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧