[英]how to add listener to java dock icon on osx
i have in my applications jframe Hide on close, but when i click the dock icon, i want it to setVisible(true); 我的应用程序中的jframe隐藏在关闭位置,但是当我单击停靠图标时,我希望它设置为setVisible(true);。 how do i add an action listener to the dock icon?
如何将动作监听器添加到停靠图标? i tried
我试过了
Image im = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("SLogo.png"));
final TrayIcon tri = new TrayIcon(im);
tri.addActionListener(this);
@Override
public void actionPerformed(ActionEvent ae) {
this.setVisible(true);
System.out.print("ok");
}
but its not triggered, and also, how will it affect the app on windows machine? 但它没有被触发,并且还将如何影响Windows计算机上的应用程序?
You need to use an AppForegroundListener
and/or AppReOpenedListener
. 您需要使用
AppForegroundListener
和/或AppReOpenedListener
。 See this example: 请参阅以下示例:
public static void main(String[] args)
{
final JFrame frame = new JFrame();
Application app = Application.getApplication();
app.addAppEventListener(new AppForegroundListener() {
@Override
public void appMovedToBackground(AppForegroundEvent arg0)
{
System.out.println("appMovedToBackground");
}
@Override
public void appRaisedToForeground(AppForegroundEvent arg0)
{
System.out.println("appRaisedToForeground");
frame.setVisible(true);
}
});
app.addAppEventListener(new AppReOpenedListener() {
@Override
public void appReOpened(AppReOpenedEvent arg0)
{
System.out.println("app reoponed");
frame.setVisible(true);
}
});
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
If you develop on Windows, you'll need to include stubs of the Mac/Java classes or else you'll get compiler errors. 如果您使用Windows开发,则需要包括Mac / Java类的存根,否则会出现编译器错误。 See here .
看这里 。
If you develop on Mac, just make sure the code is not executed when running on Windows. 如果您在Mac上进行开发,只需确保在Windows上运行时未执行该代码即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.