[英]How to minimize the application to system tray on close from ApplicationWindow object?
I am in process of compiling an open source backup application. 我正在编译一个开源备份应用程序。 I want the application not to be closed using X nor any key combination such as Alt+F4 in Windows. 我希望不要使用X或Windows中的任何组合键(例如Alt + F4)关闭应用程序。 What I prefer would be minimizing the application to system tray in such cases. 在这种情况下,我希望将应用程序最小化到系统托盘中。
I am new to Java and so far what I have tried are: 我是Java新手,到目前为止,我尝试过的是:
Setting the shell style: I can get the shell using getShell method of ApplicationWindow instance, which I can only manipulate to show no buttons at all (SWT.TITLE) or all of the buttons. 设置外壳样式:我可以使用ApplicationWindow实例的getShell方法获取外壳,该方法只能操作为根本不显示任何按钮(SWT.TITLE)或所有按钮。 No luck only hiding the X using setShellStyle(getShellStyle() & ~SWT.CLOSE); 只能使用setShellStyle(getShellStyle()&〜SWT.CLOSE)隐藏X是没有运气的;
Getting the JFrame list using (getFrames) method, and trying to manipulate the item at 0. However I could not find the setDefaultCloseOperation method of the object, which seems to resolve my minimizing to tray problem. 使用(getFrames)方法获取JFrame列表,并尝试将项目设置为0。但是,找不到该对象的setDefaultCloseOperation方法,这似乎解决了我的最小化托盘问题。
No luck getting the JFrame instance from ApplicationWindow neither, so I couldn't use the setDefaultCloseOperation method. 也没有从ApplicationWindow获取JFrame实例的运气,因此我无法使用setDefaultCloseOperation方法。
What I do is to use a hidden shell as the main window and let the application windows close normally. 我要做的是使用隐藏的外壳作为主窗口,并让应用程序窗口正常关闭。 Something like: 就像是:
// Main shell
Shell shell = new Shell(display, SWT.NO_TRIM);
shell.setBounds(0, 0, 0, 0);
shell.open();
shell.setVisible(false);
// Add system tray
Tray tray = display.getSystemTray();
TrayItem item = new TrayItem(tray, SWT.NONE);
item.setImage(image);
// System tray menu
final Menu menu = new Menu(shell, SWT.POP_UP);
MenuItem exitItem = new MenuItem(menu, SWT.PUSH);
exitItem.setText("Exit");
exitItem.addSelectionListener(new ExitListener());
item.addMenuDetectListener(new MenuDetectListener()
{
@Override
public void menuDetected(MenuDetectEvent e)
{
menu.setVisible(true);
}
});
// Main loop
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
Here is how I resolved the issue after 2 days of research: 经过2天的研究,这是我解决问题的方法:
Override the close method! 覆盖关闭方法!
public boolean close() {
final Shell grandShell = this.getShell();
grandShell.setVisible(false);
Display display = Display.getCurrent();
Tray tray = display.getSystemTray();
if(tray != null) {
TrayItem item = new TrayItem(tray, SWT.NONE);
item.setImage(ArecaImages.ICO_SMALL);
final Menu menu = new Menu(getShell(), SWT.POP_UP);
MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
menuItem.setText("Areca");
menuItem.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
grandShell.setVisible(true);
}
});
item.addListener (SWT.MenuDetect, new Listener () {
public void handleEvent (Event event) {
menu.setVisible (true);
}
});
}
return true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.