java多线程创建窗体报错

caoshangfei 2014-05-29 03:20:15
初学java。
需要做一个右下角的通知栏窗体,麻烦看看,为啥报错,谢谢。
主要代码如下:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

public class Notifier extends Thread {

Shell shell;

protected int moveStep = 2; //每次移动的pixel
protected int upPosition; //能移动到的最上面坐标
protected int downPosition; //当前popup的边框坐标
protected int leftPosition; //popup左边边框坐标
public Notifier() {



shell = new Shell(SWT.ON_TOP);



Text text = new Text(shell, SWT.MULTI | SWT.WRAP);
text.setBounds(10, 20, 180, 20);
text.setBackground(shell.getBackground());

text.setText("新通知XXX条");


//取屏莫大小
Rectangle area = Display.getDefault().getClientArea();

upPosition = area.height - 100;//计算出popup界面在屏幕显示的最高位置
downPosition = area.height + 100;//计算出popup界面的初始位置
leftPosition = area.width - 180;

shell.setSize(180, 40);

//初始化popup位置
shell.setLocation(leftPosition, downPosition);

shell.open();

}



public void run() {

Display display = shell.getDisplay();
while (true) {
try {
Thread.sleep(10);

//判断当前位置是否小于能出现的最高位置,小于的话就说明还可以向上移动。
if ((downPosition - moveStep) > upPosition) {
display.asyncExec(new Runnable() {
public void run() {
shell.setLocation(leftPosition, downPosition- moveStep);
downPosition -= moveStep;
}
});
//此时已经移动到了最高位置,显示10秒钟后,关闭窗口并退出。
} else {
Thread.sleep(10000);
display.asyncExec(new Runnable() {
public void run() {
shell.dispose();
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

上面代码,直接通过按钮点击,是能弹出来的,但是在另外一个线程里,调用弹出窗口,在运行到shell = new Shell(SWT.ON_TOP); 报错了。应该是多线程调用的问题。
我线程调用代码如下:

while(true){

try {
//获取新通知。

int i=10;


if(i>0){

Notifier tongzhi=new Notifier();
tongzhi.start();
}
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
...全文
150 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
caoshangfei 2014-05-29
  • 打赏
  • 举报
回复
引用 6 楼 jacky4631 的回复:
在线程中操作UI需要用
Display.getDefault().syncExec(runnable)
或者
Display.getDefault().asyncExec(runnable)
,不然就会报Invalid thread access错误
是的。搞定了。 分给你了。
忘了隐藏 2014-05-29
  • 打赏
  • 举报
回复
在线程中操作UI需要用
Display.getDefault().syncExec(runnable)
或者
Display.getDefault().asyncExec(runnable)
,不然就会报Invalid thread access错误
caoshangfei 2014-05-29
  • 打赏
  • 举报
回复
shell = new Shell(SWT.ON_TOP); 这一行
vnvlyp 2014-05-29
  • 打赏
  • 举报
回复
引用 3 楼 caoshangfei 的回复:
[quote=引用 1 楼 rui888 的回复:] 报什么错误呢。
报错: Exception in thread "Thread-6" org.eclipse.swt.SWTException: Invalid thread access[/quote] 具体哪一行?
caoshangfei 2014-05-29
  • 打赏
  • 举报
回复
引用 1 楼 rui888 的回复:
报什么错误呢。
报错: Exception in thread "Thread-6" org.eclipse.swt.SWTException: Invalid thread access
u010772735 2014-05-29
  • 打赏
  • 举报
回复
tony4geek 2014-05-29
  • 打赏
  • 举报
回复
报什么错误呢。

62,614

社区成员

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

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