Java awt SystemTray MouseListener不工作

时间:2018-01-04 11:54:26

标签: java notifications awt

显示通知部分始终有效但注册的鼠标事件不(从未)被解雇。这是我的代码;由于它是一个控制台应用程序,所有方法都是静态的,我不知道它是否有影响。

public  class NotificationUtils {

public static void showNotification(String Message,final String url) throws AWTException {

    SystemTray tray = SystemTray.getSystemTray();
    //If the icon is a file
    Image image = Toolkit.getDefaultToolkit().createImage("icon.png");
    //Alternative (if the icon is on the classpath):
    //Image image = Toolkit.getToolkit().createImage(getClass().getResource("icon.png"));
    TrayIcon trayIcon = new TrayIcon(image,"Tray Demo");
    trayIcon.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println("Eventte");
            try {
                Desktop.getDesktop().browse(new URL(url).toURI());
            }catch (URISyntaxException|IOException ex){
                ex.printStackTrace();
            }
        }
        @Override public void mousePressed(MouseEvent e) {System.out.println("Eventte");}
        @Override public void mouseReleased(MouseEvent e) {}
        @Override public void mouseEntered(MouseEvent e) {}
        @Override public void mouseExited(MouseEvent e) { }
    });
    //Let the system resizes the image if needed
    trayIcon.setImageAutoSize(true);
    //Set tooltip text for the tray icon
    trayIcon.setToolTip("New Notification");
    tray.add(trayIcon);
    trayIcon.displayMessage("Atanmamış Bildirim Var", Message, TrayIcon.MessageType.INFO);
}
}

如何调用此方法;

   NotificationUtils.showNotification(ticket.getSubject(), address);

这是我的编辑,它还没有打开。为什么不触发鼠标事件。我用System.out检查它

private static SystemTray tray;
private static TrayIcon trayIcon;

public static void showNotification(String Message,final String url) throws AWTException {

    if(tray == null) {
        tray = SystemTray.getSystemTray();
        //If the icon is a file
        Image image = Toolkit.getDefaultToolkit().createImage("icon.png");
        //Alternative (if the icon is on the classpath):
        //Image image = Toolkit.getToolkit().createImage(getClass().getResource("icon.png"));
        trayIcon = new TrayIcon(image, "Tray Demo");
        trayIcon.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Eventte");
                try {
                    Desktop.getDesktop().browse(new URL(url).toURI());
                } catch (URISyntaxException | IOException ex) {
                    ex.printStackTrace();
                }
            }

            @Override public void mousePressed(MouseEvent e) {}
            @Override public void mouseReleased(MouseEvent e) {}
            @Override public void mouseEntered(MouseEvent e) {}
            @Override public void mouseExited(MouseEvent e) {}
        });
        //Let the system resizes the image if needed
        trayIcon.setImageAutoSize(true);
        //Set tooltip text for the tray icon
        trayIcon.setToolTip("New Notification");
        tray.add(trayIcon);
    }
    trayIcon.displayMessage("Atanmamış Bildirim Var", Message, TrayIcon.MessageType.INFO);
}

0 个答案:

没有答案