简体   繁体   English

awt.TrayIcon是否可以区分双击托盘图标和关闭气球消息?

[英]awt.TrayIcon is it posible to distinguish between a double click on the tray icon and a close of a balloon message?

Currently in my (JRuby code), I want to handle "balloon close" messages differently than "double click on the tray icon" (awt.TrayIcon instance): 目前在我的(JRuby代码)中,我想处理“气球关闭”消息的方式不同于“双击托盘图标”(awt.TrayIcon实例):

import java.awt.TrayIcon
import java.awt.event.MouseListener

tray = java.awt.SystemTray::system_tray
image = java.awt.Toolkit::default_toolkit.get_image('')

trayIcon = TrayIcon.new(image, 'name', nil)

tray.add(trayIcon)
trayIcon.addActionListener do |evt|
  puts "in here", evt.id
end
trayIcon.displayMessage("title", "try clicking within the baloon, but not the x, then double clicking the tray icon", TrayIcon::MessageType::INFO)
puts "try clicking within the balloon message, or double clicking, both seem to generate the same event"

and the equivalent code in java https://gist.github.com/4338167 以及java中的等效代码https://gist.github.com/4338167

it seems that this action listener gets called either when 1) a user double clicks the icon in the system tray, or 2) a user closes a tray's recent balloon message by clicking on the balloon (not on the x). 似乎这个动作监听器在1)用户双击系统托盘中的图标时被调用,或2)用户通过单击气球(不在x上)关闭托盘的最近气球消息。

Is it possible to distinguish between the 2 different event types (both seem to have event id 1001), or must I just infer it by timing relative or something else? 是否可以区分两种不同的事件类型(两者似乎都有事件ID 1001),或者我只能通过计时相对或其他东西来推断它? Or can I somehow "know" that the balloon is still up, and thus the click must have come from it? 或者我可以以某种方式“知道”气球仍在运转,因此点击必须来自它?

The documentation for TrayIcon seems to imply that clicking on the balloon message can trigger an ActionEvent , which is the same as, for your case, double-clicking on the icon itself. TrayIcon的文档似乎暗示单击气球消息可以触发ActionEvent ,这与您的情况相同,双击图标本身。

You may need to add a mouse listener to trigger on clicks, and if a click event is followed by an ActionEvent within some small period then it's a double-click, otherwise it's a click on the message being displayed. 您可能需要添加一个鼠标侦听器来触发点击,如果在一小段时间内单击事件后跟一个ActionEvent ,那么它就是双击,否则就是单击要显示的消息。 By choosing a sufficiently small interval, you should be able to properly distinguish them. 通过选择足够小的间隔,您应该能够正确区分它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM