社区
非技术区
帖子详情
为什么线程中的对话框收不到消息?
Cline
2002-01-16 09:18:58
程序中有两个线程A和B,A是主线程,B是一个副线程,在B中会弹出一个对话框。我在A线程刚启动时,即Application的Main()中启动线程B,是可以弹出对话框的;但是,我在A线程的事件处理函数中启动B,虽然弹出了对话框,可是对话框什么事件也收不到,按关闭按钮也没反应。
------------------一位Java初学者
...全文
141
14
打赏
收藏
为什么线程中的对话框收不到消息?
程序中有两个线程A和B,A是主线程,B是一个副线程,在B中会弹出一个对话框。我在A线程刚启动时,即Application的Main()中启动线程B,是可以弹出对话框的;但是,我在A线程的事件处理函数中启动B,虽然弹出了对话框,可是对话框什么事件也收不到,按关闭按钮也没反应。 ------------------一位Java初学者
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
14 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
Cline
2002-01-17
打赏
举报
回复
可以用System.out.println(time).
我想原因可能是,事件队列被线程A锁住了,因此线程B收不到事件!我对事件对列不了解,望赐教!
z_yheart
2002-01-17
打赏
举报
回复
你在
if(nIndex>=0)
{
time=timeFormat.substring(0,nIndex);
time+=elapseTime;
time+=timeFormat.substring(nIndex+2);
}
后面System.out.println(teme);先,看看有没有输出
z_yheart
2002-01-17
打赏
举报
回复
你在
if(nIndex>=0)
{
time=timeFormat.substring(0,nIndex);
time+=elapseTime;
time+=timeFormat.substring(nIndex+2);
}
后面System.out.println(teme);先,看看有没有输出
Cline
2002-01-17
打赏
举报
回复
我在Frame中用this,还是不正常.
z_yheart
2002-01-17
打赏
举报
回复
InitThread waitMsg=new InitThread(this,"Database Information","Testing connection,please wait...","Elapsed time: %ds");
Cline
2002-01-17
打赏
举报
回复
两个线程A和B的关系是,A要做比较耗时的处理时,启动线程B。线程B会弹出一个对话框,并有一个Timer,Timer的TimerTask中会去刷新对话框上JLabel的内容。
TimerTask保留了一个JLabel的句柄。
问题描述:
如果在线程A的Application的构造函数中,执行上面的逻辑,一切如愿;但在线程A的事件处理函数中,就不正常了,对话框的JLabel的文字不显示了,按(X)也关了对话框,就象对话框收不到消息一样!
///////线程B中所涉及的类
public class DWInitTask extends TimerTask {
JLabel jLabel;
long beginTime;
String timeFormat="Elapsed time: %ds";
public DWInitTask(JLabel jLabel,String timeFormat) {
this.jLabel =jLabel;
this.timeFormat =timeFormat;
beginTime=System.currentTimeMillis();
}
public void run() {
/**@todo: implement this java.util.TimerTask abstract method*/
long elapseTime=System.currentTimeMillis()-beginTime;
elapseTime=elapseTime/1000+((elapseTime%1000)>0?1:0);
if(null!=jLabel)
{
String time=timeFormat+"";
int nIndex=time.indexOf("%d");
if(nIndex>=0)
{
time=timeFormat.substring(0,nIndex);
time+=elapseTime;
time+=timeFormat.substring(nIndex+2);
}
jLabel.setText(time);
}
}
public void setTextField(JLabel jLabel)
{
this.jLabel=jLabel;
}
}
public class InitDialog extends JDialog {
JPanel panel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JLabel jLabelMsg = new JLabel();
Border border1;
JLabel jLabelTime = new JLabel();
java.util.Timer timer=new java.util.Timer();
String msg="";
String timeFormat="";
public InitDialog(Frame frame, String title, boolean modal,String msg,String timeFormat) {
super(frame, title, modal);
try {
this.msg=msg;
this.timeFormat =timeFormat;
jbInit();
pack();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public InitDialog() {
this(null, "", false,"","");
}
void jbInit() throws Exception {
border1 = BorderFactory.createEmptyBorder(10,10,10,10);
panel1.setLayout(borderLayout1);
panel1.setBorder(border1);
panel1.setPreferredSize(new Dimension(251, 73));
jLabelTime.setMaximumSize(new Dimension(0, 20));
jLabelTime.setMinimumSize(new Dimension(0, 20));
jLabelTime.setPreferredSize(new Dimension(0, 20));
this.getContentPane().setBackground(Color.lightGray);
this.setEnabled(true);
this.jLabelMsg.setText(msg);
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
this_windowClosing(e);
}
public void windowOpened(WindowEvent e) {
this_windowOpened(e);
}
});
getContentPane().add(panel1);
panel1.add(jLabelMsg, BorderLayout.CENTER);
panel1.add(jLabelTime, BorderLayout.SOUTH);
timer.schedule(new DWInitTask(jLabelTime,timeFormat),0,1000);
}
public void stopTimer()
{
timer.cancel() ;
}
void this_windowClosing(WindowEvent e) {
timer.cancel() ;
System.exit(0);
}
void this_windowOpened(WindowEvent e) {
System.out .println("Window opened!") ;
}
}
public class InitThread extends Thread {
JFrame frame;
InitDialog initDlg=null;
String title="";
String msg="";
String timeFormat="Elapse time: %d s";
boolean bToRun=true;
public InitThread(JFrame frame,String title,String msg,String timeFormat) {
this.frame =frame;
this.title=title;
this.timeFormat=timeFormat;
this.msg =msg;
}
public void run()
{
if(bToRun)
{
createDlg();
if(initDlg!=null)
initDlg.show();
}
}
public synchronized void stopTimer()
{
if(initDlg!=null)
{
initDlg.setVisible(false);
initDlg.stopTimer() ;
initDlg.dispose();
initDlg=null;
}
else
bToRun=false;
}
public synchronized void createDlg()
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dlgSize;
initDlg=new InitDialog(frame,title,true,msg,timeFormat);
dlgSize = initDlg.getPreferredSize();
initDlg.setLocation((screenSize.width - dlgSize.width) / 2, (screenSize.height - dlgSize.height) / 2);
}
}
///End of Thread B Classes
//线程A的一个事件处理函数
void jButtonOK_actionPerformed(ActionEvent e) {
InitThread waitMsg=new InitThread((JFrame)(getParent()),"Database Information","Testing connection,please wait...","Elapsed time: %ds");
waitMsg.start() ;
//耗时处理
waitMsg.stopTimer();
}
Cline
2002-01-17
打赏
举报
回复
我找了一下帮助文档资料,发现EventQueue有一个函数isDispatchThread(),判断当前线程是不是事件Dispatch线程。即任何时候,只有某个线程是事件Dispatch线程。那么,如果一个线程的事件处理函还没返回,如何让另一个线程获得EventQueue的控制权?
lovejsp
2002-01-16
打赏
举报
回复
可以在程序中做个调试.
z_yheart
2002-01-16
打赏
举报
回复
最好贴出代码和出现的异常信息
z_yheart
2002-01-16
打赏
举报
回复
调用setSize方法先
Cline
2002-01-16
打赏
举报
回复
主要问题是,线程B中的对话框弹出后,所有控件都有没了,只有标题还在。
Cline
2002-01-16
打赏
举报
回复
主要是,线程B中的对话框弹出后,所有控件都有没了,只有标题还在。
yhc0125
2002-01-16
打赏
举报
回复
对话框属于线程B,线程A中无法控制。只能在线程B中控制。
yhc0125
2002-01-16
打赏
举报
回复
对话框的消息应通过线程B发送,A线程对对话框无作用
android demo 我的微信 by:谜@BUAA 摇一摇
通过分析和调试代码,开发者可以学习到Android的UI设计、事件处理、网络通信(如使用OkHttp或Retrofit实现微信的API调用)、数据存储(SQLite或SharedPreferences)以及多
线程
编程等方面的知识。不过,对于商业项目...
198个经典C_WinForm实例源码(超赞)
C#提供了System.Windows.Forms命名空间下的Dialog类,如OpenFileDialog和SaveFileDialog,便于集成到应用
中
。 6. **图形和图像处理**:GDI+(Graphics Device Interface Plus)是.NET框架
中
的图形处理库,可以用来...
类似QQ的全套聊天系统[极品] 源码
1. **用户认证与授权**:源码可能会包含用户注册、登录的实现,涉及到数据库操作(如SQL Server或SQLite)、身份验证机制(如OAuth2.0)和加密算法(如MD5或SHA256)。 2. **网络通信**:使用C#的System.Net命名...
技术派-在工作
线程
与UI
线程
中
创建
对话框
工作
线程
UI
线程
对话框
MFC自定义
消息
的实现方法----(
线程
向主
对话框
发送
消息
)、MFC不能用UpdateData的解决方法
在MFC
中
,我们一边在使用多
线程
时,经常会遇到在需要调用到新建的控件,此时建议不要在新建的
线程
中
直接调用主
对话框
的控件,我们可以通过自定义
消息
,在新建
线程
中
发送并触发主
线程
进行相关的界面控件操作。...
非技术区
23,407
社区成员
70,514
社区内容
发帖
与我相关
我的任务
非技术区
Java 非技术区
复制链接
扫一扫
分享
社区描述
Java 非技术区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章