我使用Java的translucent and shaped windows来创建类似于Growl的通知系统。基本上,在屏幕的右侧潜伏着一个不可见的始终在顶部的窗口,当通知进入时会在其中添加通知。它可以按需工作。
但是,如果我尝试在此不可见窗口(最大宽度为400px)的范围内单击另一个应用程序中的按钮,则单击事件转到我的应用程序,这很奇怪,因为它是不可见的,并且不会做任何事情。这真让我感到困惑。我以为其他应用程序已冻结。
我有办法允许其他应用程序“点击”我的应用程序吗?
我觉得这里不需要完整的SSCCE,但是下面的代码片段可能是相关的:
// Set up frame: no border, etc. (undecorated); transparent.
frame.setUndecorated(true);
frame.setAlwaysOnTop(true);
frame.setResizable(false); // on Mac and maybe other platforms, even
// undecorated windows can be
// resized
AWTUtilities.setWindowOpaque(frame, false);
// Determine and set size and position.
// Height: maximized; width: maximized up to 400px.
Toolkit toolkit = Toolkit.getDefaultToolkit();
Insets insets = toolkit.getScreenInsets(frame
.getGraphicsConfiguration());
Dimension screenSize = toolkit.getScreenSize();
Dimension availableSpace = new Dimension(screenSize.width - insets.left
- insets.right, screenSize.height - insets.bottom - insets.top);
frame.setSize(new Dimension(
400 > availableSpace.width ? availableSpace.width : 400,
availableSpace.height));
frame.setLocation(screenSize.width - insets.right
- frame.getSize().width, insets.top);
// Set the content of the frame to the datum holder.
frame.setContentPane(client.createPanel());
谢谢!
最佳答案
仅仅因为您的窗口是不透明/半透明的,并不意味着它不存在,因此,它仍然在其范围内接收点击事件。
为什么不将窗口设置为不使用时不可见,而将新通知发布到以下位置则设置为可见:
window_instance.setVisible(false);//hides window
window_instance.setVisible(true);//shows window
您可能希望您的窗口在每个新通知发布到窗口后短时间内保持可见状态,只需使用
Timer
和TimerTask
并在delay
的TimerTask
方法中使用足够的run()
将窗口实例设置为不可见(如果尚未设置)。另请参见此处以获取有关
Timer
的帮助:Using the Timer and TimerTask Classes