2012-05-05 95 views
2

我想使我的系統嘗試圖標彈出氣球消息,但它不這樣做。這是代碼。它應該彈出如果一個簡單的if語句被滿足但沒有任何反應。系統托盤圖標是可見的,當左擊它時會顯示菜單。系統托盤氣球消息不顯示java

package systemtray; 

import java.awt.*; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class Systemtray { 

public static void main(String[] args) { 

    TrayIcon trayIcon = null; 
    if (SystemTray.isSupported()) { 
     // get the SystemTray instance 
     SystemTray tray = SystemTray.getSystemTray(); 
     // load an image 
     Image image = Toolkit.getDefaultToolkit().getImage("D:/xxx/facebook.jpg");   
     // create a action listener to listen for default action executed on the tray icon 
     ActionListener listener = new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       // execute default action of the application 
      } 
     }; 

    // create a popup menu 
    PopupMenu popup = new PopupMenu(); 
    //create menu item for the default action 
    MenuItem defaultItem = new MenuItem("A menu item"); 
    defaultItem.addActionListener(listener); 
    popup.add(defaultItem); 
    /// ... add other items 
    // construct a TrayIcon*/ 
    trayIcon = new TrayIcon(image, "Tray Demo", popup); 


    int a = 0; 
    int b = 1; 
    if (a + b == 1){ 
    trayIcon.displayMessage("Message Title", 
     "Message Content", 
     TrayIcon.MessageType.INFO); 
    } 
    // set the TrayIcon properties 
    trayIcon.addActionListener(listener); 
    // ... 
    // add the tray image 
    try { 
     tray.add(trayIcon); 
    } catch (AWTException e) { 
     System.err.println(e); 
    } 
    // ... 
} else { 
    // disable tray option in your application or 
    // perform other actions 
} 
// ... 
// some time later 
// the application state has changed - update the image 
// if (trayIcon != null) { 
    // trayIcon.setImage(updatedImage); 
//} 
// ... 
} 
} 
+1

在黑暗中拍攝,但您試圖在將圖標添加到紙盤之前顯示氣球。嘗試將其添加到托盤中,然後顯示氣球。 –

+0

謝謝,你是正確的 –

回答

3

除非顯示圖標,否則無法顯示彈出框。你必須先顯示圖標(我假設你的代碼是tray.add)。

+0

謝謝,你是正確的 –