如何捕获trayicon.displayMessage()鼠标点击工具提示音 [英] How to capture trayicon.displayMessage() mouse click on the tooltip baloon

查看:178
本文介绍了如何捕获trayicon.displayMessage()鼠标点击工具提示音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户点击邮件气球工具提示后显示更详细的信息。



但是,我找不到如何捕获该事件。



可以这么做吗?

解决方案

1)可以通过将ActionListener添加到TrayIcon来监听MouseClickEvent,然后将消息体聆听MouseClicked



2)(不直接询问),但是如果消息被关闭按钮关闭,我无法给您回答,而消息从屏幕上相同的方式离开,但没有捕获任何事件(s)



3)看起来像这样 Java TrayIcon消息关闭按钮应该只有一个解决方案,因为API没有实现另一种方法,

  import java.awt。*; 
import java.awt.event。*;

public class FullTray {

private static class ShowMessageListener implements ActionListener {

private TrayIcon trayIcon;
private String title;
private String message;
private TrayIcon.MessageType messageType;

ShowMessageListener(TrayIcon trayIcon,String title,String message,TrayIcon.MessageType messageType){
this.trayIcon = trayIcon;
this.title = title;
this.message = message;
this.messageType = messageType;
}

public void actionPerformed(ActionEvent e){
trayIcon.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
System.out.println(Message Clicked);
}
});
trayIcon.displayMessage(title,message,messageType);
}
}

public static void main(String args []){
Runnable runner = new Runnable(){

public void run(){
if(SystemTray.isSupported()){
final SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit()。getImage(gifIcon.gif);
PopupMenu popup = new PopupMenu();
final TrayIcon trayIcon = new TrayIcon(image,提示文本,弹出窗口);
MenuItem item = new MenuItem(Error);
item.addActionListener(new ShowMessageListener(trayIcon,Error Title,Error,TrayIcon.MessageType.ERROR));
popup.add(item);
item = new MenuItem(Warning);
item.addActionListener(new ShowMessageListener(trayIcon,Warning Title,Warning,TrayIcon.MessageType.WARNING));
popup.add(item);
item = new MenuItem(Info);
item.addActionListener(new ShowMessageListener(trayIcon,Info Title,Info,TrayIcon.MessageType.INFO));
popup.add(item);
item = new MenuItem(None);
item.addActionListener(new ShowMessageListener(trayIcon,None Title,None,TrayIcon.MessageType.NONE));
popup.add(item);
item = new MenuItem(Close);
item.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){
tray.remove(trayIcon);
}
} );
popup.add(item);
try {
tray.add(trayIcon);
} catch(AWTException e){
System.err.println(Can not add to tray);
}
} else {
System.err.println(Tray unavailable);
}
}
};
EventQueue.invokeLater(runner);
}

private FullTray(){
}
}


Hi, I am trying to show more detailed information after a user clicks on the message balloon tooltip.

However, I can't find how to capture that event.

Is this possible to do?

解决方案

1) Is possible to listening MouseClickEvents by add ActionListener to the TrayIcon, then Message body listening for MouseClicked

2) (not asked directly) but I can't to give you an answer listening if message was closed by close button, and Message gone away from screen same way, but without catch any event(s)

3) looks like as this Java TrayIcon message close button should be only one solutions, because API doesn't implements another methods,

import java.awt.*;
import java.awt.event.*;

public class FullTray {

    private static class ShowMessageListener implements ActionListener {

        private TrayIcon trayIcon;
        private String title;
        private String message;
        private TrayIcon.MessageType messageType;

        ShowMessageListener(TrayIcon trayIcon, String title, String message, TrayIcon.MessageType messageType) {
            this.trayIcon = trayIcon;
            this.title = title;
            this.message = message;
            this.messageType = messageType;
        }

        public void actionPerformed(ActionEvent e) {
            trayIcon.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    System.out.println("Message Clicked");
                }
            });
            trayIcon.displayMessage(title, message, messageType);
        }
    }

    public static void main(String args[]) {
        Runnable runner = new Runnable() {

            public void run() {
                if (SystemTray.isSupported()) {
                    final SystemTray tray = SystemTray.getSystemTray();
                    Image image = Toolkit.getDefaultToolkit().getImage("gifIcon.gif");
                    PopupMenu popup = new PopupMenu();
                    final TrayIcon trayIcon = new TrayIcon(image, "The Tip Text", popup);
                    MenuItem item = new MenuItem("Error");
                    item.addActionListener(new ShowMessageListener(trayIcon, "Error Title", "Error", TrayIcon.MessageType.ERROR));
                    popup.add(item);
                    item = new MenuItem("Warning");
                    item.addActionListener(new ShowMessageListener(trayIcon, "Warning Title", "Warning", TrayIcon.MessageType.WARNING));
                    popup.add(item);
                    item = new MenuItem("Info");
                    item.addActionListener(new ShowMessageListener(trayIcon, "Info Title", "Info", TrayIcon.MessageType.INFO));
                    popup.add(item);
                    item = new MenuItem("None");
                    item.addActionListener(new ShowMessageListener(trayIcon, "None Title", "None", TrayIcon.MessageType.NONE));
                    popup.add(item);
                    item = new MenuItem("Close");
                    item.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent e) {
                            tray.remove(trayIcon);
                        }
                    });
                    popup.add(item);
                    try {
                        tray.add(trayIcon);
                    } catch (AWTException e) {
                        System.err.println("Can't add to tray");
                    }
                } else {
                    System.err.println("Tray unavailable");
                }
            }
        };
        EventQueue.invokeLater(runner);
    }

    private FullTray() {
    }
}

这篇关于如何捕获trayicon.displayMessage()鼠标点击工具提示音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆