如何在关闭应用程序后显示生成的java应用程序?我的意思是我想关闭我制作的应用程序,在那之后它仍然在后台进程(托盘)上运行,我做到了,但是在我双击我的托盘图标之后,如何使应用程序再次显示(弹出)?有人能帮我吗?
public MainMenu(String access) {
this.access = access;
initComponents();
customInit();
this.setLocationRelativeTo(null);
this.setExtendedState(this.MAXIMIZED_BOTH);
requestJobList("ALL");
setDefaultCloseOperation(HIDE_ON_CLOSE);
createReminder();
}
private void customInit(){
joblistTable.setCellSelectionEnabled(rootPaneCheckingEnabled);
}
private void createReminder(){
String icoPath = "C:/Users/user/Documents/NetBeansProjects/MOADesktopBaru/src/com/mayora/image/MOA.PNG";
if(!SystemTray.isSupported()){
System.out.println("System tray is not supported !!! ");
return ;
}
//get the systemTray of the system
SystemTray systemTray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage(icoPath);
//popupmenu
PopupMenu trayPopupMenu = new PopupMenu();
//1t menuitem for popupmenu
MenuItem setting = new MenuItem("Setting");
setting.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SettingFrame SF = new SettingFrame ();
jDesktopPane.add(SF);
SF.show();
}
});
trayPopupMenu.add(setting);
//2nd menuitem of popupmenu
MenuItem close = new MenuItem("Close");
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
trayPopupMenu.add(close);
//setting tray icon
TrayIcon trayIcon = new TrayIcon(image, "MOA Desktop", trayPopupMenu);
//adjust to default size as per system recommendation
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2)
{
initComponents();
customInit();
setExtendedState(MAXIMIZED_BOTH);
requestJobList("ALL");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
});
try{
systemTray.add(trayIcon);
}catch(AWTException awtException){
awtException.printStackTrace();
}
}发布于 2015-05-15 08:44:00
我认为setDefaultCloseOperation(EXIT_ON_CLOSE);不应该放在托盘图标侦听器中。
要想再次显示您的应用程序,只需在mouseClicked()方法中添加一行:
setVisible(true);请注意,您要使JFrame再次可见,所以如果您需要指定它,只需在setVisible(true)方法之前添加它。顺便说一句,由于应用程序已经执行(在后台),您不需要再次调用initComponents()方法。
另一个建议是,不要使用mouseClicked()方法。而是使用来自MouseListener接口的MouseListener接口。为什么?有时候你点击的速度太快以至于应用程序检测到了mouseDragged事件,所以点击就不算了。如果使用mousePressed(),则保证它检测到两次单击。
https://stackoverflow.com/questions/30254896
复制相似问题