下面是一个实现闪动效果的线程执行过程: private class Prompt implements Runnable { @Override public void run() { while(isFlash) { try { trayItem.setImage(SWTResourceManager.getImage(MainShell.class,"/org/wayne/feiq/ui/image/comment.png")); Thread.sleep(500); trayItem.setImage(SWTResourceManager.getImage(MainShell.class,"/org/wayne/feiq/ui/image/comments.png")); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } }但是会出现以下错误: (我试了,只要在此线程中调用trayItem的方法就会出这错,打印trayItem都不出错) Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid thread access at org.eclipse.swt.SWT.error(SWT.java:3884) at org.eclipse.swt.SWT.error(SWT.java:3799) at org.eclipse.swt.SWT.error(SWT.java:3770) at org.eclipse.swt.widgets.Widget.error(Widget.java:463) at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:355) at org.eclipse.swt.widgets.TrayItem.setImage(TrayItem.java:397) at org.wayne.feiq.ui.UserList$Prompt.run(UserList.java:155) at java.lang.Thread.run(Thread.java:619)
而且icon图标不能是动态的吧。
private class Prompt implements Runnable {
@Override
public void run() {
while(isFlash) {
try {
trayItem.setImage(SWTResourceManager.getImage(MainShell.class,"/org/wayne/feiq/ui/image/comment.png"));
Thread.sleep(500);
trayItem.setImage(SWTResourceManager.getImage(MainShell.class,"/org/wayne/feiq/ui/image/comments.png"));
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}但是会出现以下错误:
(我试了,只要在此线程中调用trayItem的方法就会出这错,打印trayItem都不出错)
Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:3884)
at org.eclipse.swt.SWT.error(SWT.java:3799)
at org.eclipse.swt.SWT.error(SWT.java:3770)
at org.eclipse.swt.widgets.Widget.error(Widget.java:463)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:355)
at org.eclipse.swt.widgets.TrayItem.setImage(TrayItem.java:397)
at org.wayne.feiq.ui.UserList$Prompt.run(UserList.java:155)
at java.lang.Thread.run(Thread.java:619)
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.ImageIcon;
import javax.swing.JFrame;import org.omg.CORBA.PUBLIC_MEMBER;/**
* 创建闪动的托盘图像
* @author 黄根华
*/
public class TestTray extends JFrame implements Runnable { private SystemTray sysTray;// 当前操作系统的托盘对象 private TrayIcon trayIcon;// 当前对象的托盘 private int num = 10; ImageIcon icon = null; public TestTray() {
this.createTrayIcon();// 创建托盘对象
init();
} /**
* 初始化窗体的方法
*/
/**
*
*/
public void init() {
this.setTitle("闪动托盘");
this.setSize(400, 400);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
// 添加窗口事件,将托盘添加到操作系统的托盘
this.addWindowListener(new WindowAdapter() { public void windowIconified(WindowEvent e) {
addTrayIcon();
}
}); this.setVisible(true);
} /**
* 添加托盘的方法
*/
public void addTrayIcon() {
try {
sysTray.add(trayIcon);// 将托盘添加到操作系统的托盘
setVisible(false);// 使得当前的窗口隐藏
new Thread(this).start();
} catch (AWTException e1) {
e1.printStackTrace();
}
} /**
* 创建系统托盘的对象 步骤: 1,获得当前操作系统的托盘对象 2,创建弹出菜单popupMenu 3,创建托盘图标icon
* 4,创建系统的托盘对象trayIcon
*/
public void createTrayIcon() {
sysTray = SystemTray.getSystemTray();// 获得当前操作系统的托盘对象
icon = new ImageIcon("1.gif");// 托盘图标
PopupMenu popupMenu = new PopupMenu();// 弹出菜单
MenuItem mi = new MenuItem("弹出");
MenuItem exit = new MenuItem("关闭");
popupMenu.add(mi);
popupMenu.add(exit);
// 为弹出菜单项添加事件
mi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
setVisible(true);
sysTray.remove(trayIcon);
}
});
exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
trayIcon = new TrayIcon(icon.getImage(), "闪动托盘", popupMenu);
} /**
* @param args
*/
public static void main(String[] args) {
TestTray testTray = new TestTray();
} /*
* 线程控制闪动 (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
while (num >= 0) {
this.trayIcon.setImage(new ImageIcon("").getImage());
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.trayIcon.setImage(icon.getImage());
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
num--;
}
}}这个可以实现...................记得把图片换成你的..........
org.eclipse.swt.SWTException: Invalid thread access
在SWT程序中,SWT会自动创建一个用户界面线程,非用户界面线程不能直接操作用户界面线程,要想在另外一个线程中尝试修改用户界面,应采用display的syncExec或者asyncExec方法。
忘了问了,在另一线程里修改,该怎么做才行呢?
这样吗:Display.getCurrent().asyncExec(new Prompt());
这样并没开启另一线程,主线程也停住了。Prompt是让托盘闪动的线程执行代码。