swt 簡(jiǎn)單的托盤程序
java寫和操作系統(tǒng)相關(guān)的程序難度非常大。在java 6出現(xiàn)之前,如果你想實(shí)現(xiàn)一個(gè)托盤程序,最簡(jiǎn)單的就是用swt了。
通過(guò)google我找到了一段代碼。
其實(shí)很簡(jiǎn)單。主要的代碼如下:
?????????????final Tray tray = display.getSystemTray();
??????????? final TrayItem trayItem = new TrayItem(tray, SWT.NONE);
??????????? Image image = new Image (display, 16, 16);
??????????? trayItem.setImage(image);
知道了重點(diǎn),事情變的很簡(jiǎn)單了。看看所有程序
//-----------------
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();
??? }
??? /**
???? * Create contents of the window
???? */
??? protected void createContents() {
??????? setText("SWT Application");
??????? setSize(500, 375);
??? }
??? //swt 默認(rèn)情況下不允許shell被繼承
??? //所以我重載了父類的方法
??? protected void checkSubclass() {
???????? }
}
//--------------------
如果你想成功運(yùn)行以上代碼,你最好在eclipse下新建一個(gè)swt的類。具體操作你搜索一下吧。
但是上面的代碼只是加入了托盤,這可能是最簡(jiǎn)單的實(shí)現(xiàn)托盤的程序了。我們加入事件處理,讓程序能夠最大和最小化。然后把托盤用圖片來(lái)表示。
詳細(xì)的代碼不參考
下載
到eclipse里運(yùn)行
posted on 2007-01-03 22:58
nake 閱讀(3415)
評(píng)論(4) 編輯 收藏