线程问题Exception in thread "Thread-0"

huxiuliang 2007-11-17 08:37:34
程序目的,在右下角弹出小窗体

当关闭主窗体是抛出异常
Exception in thread "Thread-0" org.eclipse.swt.SWTException: Device is disposed
at org.eclipse.swt.SWT.error(SWT.java:3374)
at org.eclipse.swt.SWT.error(SWT.java:3297)
at org.eclipse.swt.SWT.error(SWT.java:3268)
at org.eclipse.swt.widgets.Display.error(Display.java:978)
at org.eclipse.swt.widgets.Display.asyncExec(Display.java:602)
at test.Popup.run(Popup.java:111)
源代码为
package test;

//Popup.java
//实现像MSN,QQ一样的好友上线通知popup

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.swtdesigner.SWTResourceManager;

public class Popup extends Thread {


Shell shell;
protected int moveStep = 2; // 每次移动的pixel

protected int upPosition; // 能移动到的最上面坐标

protected int downPosition; // 当前popup的边框坐标

protected int leftPosition;// popup左边边框坐标

public Popup(final String message) {


//Display display = new Display();//new display
shell = new Shell(SWT.NO_TRIM | SWT.BORDER);
shell.setBackground(SWTResourceManager.getColor(192, 192, 192));

// shell.setBackgroundImage(new Image(Display.getCurrent(),
// getClass().getResourceAsStream("/11.gif")));
shell.setBackgroundMode(SWT.INHERIT_DEFAULT);

final Label label_msg = new Label(shell, SWT.NONE);// 显示具体消息的label
label_msg.setBackground(SWTResourceManager.getColor(255, 255, 255));
label_msg.setText(message);
label_msg.setBounds(10, 31, 150, 60);

final Label label = new Label(shell, SWT.NONE);// 显示具体消息的label的背景,目的是做个边框
label.setBackground(SWTResourceManager.getColor(255, 255, 255));
// label.setBackgroundImage(new Image(Display.getCurrent(),
// getClass().getResourceAsStream("/11.gif")));
label.setBounds(2, 25, 166, 73);
// 取屏莫大小
Rectangle area = Display.getDefault().getClientArea();

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

shell.setSize(170, 100);

// 初始化popup位置
shell.setLocation(leftPosition, downPosition);
final Label colse = new Label(shell, SWT.NONE);// colse按钮
colse.addMouseListener(new MouseAdapter() {
public void mouseDown(final MouseEvent e) {

shell.dispose();
}
});
colse.setBackgroundImage(new Image(Display.getCurrent(), getClass()
.getResourceAsStream("/close.gif")));

colse.setBounds(150, 5, 14, 14);

final Label top2 = new Label(shell, SWT.NONE);// 标题框
top2.setBackgroundImage(new Image(Display.getCurrent(), getClass()
.getResourceAsStream("/T.png")));
top2.setText(" 系统提示");
top2.setBounds(2, 5, 166, 20);

final Label top1 = new Label(shell, SWT.NONE);// 顶部label,目的是样‘系统提示’4个子垂直居中
top1.setBackgroundImage(new Image(Display.getCurrent(), getClass()
.getResourceAsStream("/T.png")));
top1.setBounds(2, 2, 166, 5);

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;
}
});
// 此时已经移动到了最高位置,显示5秒钟后,关闭窗口并退出。
} else {
Thread.sleep(5000);
display.asyncExec(new Runnable() {
public void run() {
shell.dispose();
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {



final Display display = new Display();
Shell shell = new Shell();
shell.setText("系统提示");
shell.setSize(250, 150);

final Button button = new Button(shell, SWT.NONE);
button.setBounds(50, 20, 100, 25);
button.setText("button");
// 监听button的事件,当用户点击时调用Popup类显示popup界面。
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// 实例化popup类,构造函数为popup界面中出现的提示信息。
Popup popup = new Popup("您的好友xxx上线了。");
popup.start();
}
});

shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();

}
}
...全文
1057 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiang218 2009-10-15
  • 打赏
  • 举报
回复
else {
Thread.sleep(5000);
display.asyncExec(new Runnable() {
public void run() {
shell.dispose();
}
});
break;
}
fightplane 2007-11-21
  • 打赏
  • 举报
回复
不行,异常还是照样抛出。
qybao 2007-11-17
  • 打赏
  • 举报
回复
else {
Thread.sleep(5000);
display.asyncExec(new Runnable() {
public void run() {
shell.dispose();
}
});
这部分也应该判断
qybao 2007-11-17
  • 打赏
  • 举报
回复
当你关掉窗口以后
while (true) {
try {
Thread.sleep(10);
// 判断当前位置是否小于能出现的最高位置,小于的话就说明还可以向上移动。
if ((downPosition - moveStep) > upPosition) {
display.asyncExec(new Runnable() {
public void run() {
shell.setLocation(leftPosition, downPosition
- moveStep);
downPosition -= moveStep;
}
});
这段代码还继续被执行,所以就出错了。在判断位置之前应该加个判断,if(!shell.isDisposed())

62,623

社区成员

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

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