swt 简单的托盘程序
java写和操作系统相关的程序难度非常大。在java 6出现之前,如果你想实现一个托盘程序,最简单的就是用swt了。
通过google我找到了一段代码。
其实很简单。主要的代码如下:
final Tray tray = display.getSystemTray();
final TrayItem trayItem = new TrayItem(tray, SWT.NONE);
Image image = new Image (display, 16, 16);
trayItem.setImage(image);
知道了重点,事情变的很简单了。看看所有程序
//-----------------
public class SystemTray extends Shell {
public static void main(String args[]) {
try {
Display display = Display.getDefault();
SystemTray shell = new SystemTray(display, SWT.SHELL_TRIM);
// shell.createSystemTray(shell);
final Tray tray = display.getSystemTray();
final TrayItem trayItem = new TrayItem(tray, SWT.NONE);
Image image = new Image (display, 16, 16);
trayItem.setImage(image);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public SystemTray(Display display, int style) {
super(display, style);
createContents();
}