关于多线程过程中进行swing操作:

westwin 2004-01-05 04:19:44
我自己New了一个线程,如下:
public class ParserThread extends Thread {
.....................
public void run() {
while(.....) //循环进行计算
...........//耗时的大量计算;
//然后可能会生成一个Dialog,让用户手动设置一下计算条件等等。
new MyDialog(....).show();//自定义的一个JDialog,setModal(true)了;
//等待MyDialog返回继续进行下面的计算;
................
}
}
整个程序框架大致就这样,现在的问题是在线程中生成的MyDialog无法进行按钮点击等会产生awt、swing事件的操作。这应该是这些操作都不是thread-safe的原因吧,请教一下解决思路,呵呵,时间有限,来不及自己看书,谢谢大家了。


...全文
64 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
westwin 2004-01-06
  • 打赏
  • 举报
回复
楼上正解,先结贴了,然后再细细研究研究。
oeichenwei 2004-01-06
  • 打赏
  • 举报
回复
是的,使用SwingUtlity.invokelater就好了啦!各位
westwin 2004-01-06
  • 打赏
  • 举报
回复
先去看书学习去,书中说:
The single-thread rule is as follows:
Rule: Once a Swing component has been realized, all code that might affect or depend on the state of that component should be executed in the event-dispatching thread.
westwin 2004-01-06
  • 打赏
  • 举报
回复
碰到的问题,正是楼上所说的。大家可以做个例子试试。
loveyousomuch 2004-01-05
  • 打赏
  • 举报
回复
难道在线程中产生的Dialog不能响应事件?
oeichenwei 2004-01-05
  • 打赏
  • 举报
回复
试试SwingUtility.invokelater?
westwin 2004-01-05
  • 打赏
  • 举报
回复
如果MyDialog不是在Thread中new的话,当然可以响应事件,我试过了。
noratong 2004-01-05
  • 打赏
  • 举报
回复
产生对话框后就使线程进入等待状态,然后再在对话框的点击事件里面使线程重新运行起来。

作起来可能有些困难。
Hodex 2004-01-05
  • 打赏
  • 举报
回复
会么,不用线程能响应事件么
chinarendotcom 2004-01-05
  • 打赏
  • 举报
回复
gz
chinarendotcom 2004-01-05
  • 打赏
  • 举报
回复
冰天雪地裸体跪求才行!◎

62,615

社区成员

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

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