在这种特定情况下,当用户单击X按钮时,合并JFrame窗口会遇到一些问题,有什么想法吗



我有以下问题:我正在开发一个Java Swing应用程序,该应用程序向我显示JFrame。我要做的是,当用户点击X按钮时,窗口必须被图标化,而不是关闭(这是客户要求的)

我的体系结构非常复杂,它是这样工作的:

我有一个名为GUI的类,它是这样的:

public class GUI extends SingleFrameApplication implements PropertyChangeListener {
private MainFrame mainFrame = null;
@Override
public void propertyChange(PropertyChangeEvent arg0) {
showMainFrame();
}
private void showMainFrame() {
mainFrame = new MainFrame(settings, tasksSettings, logAppender);
// I add a PropertyChangeListener to the created MainFrame object:
mainFrame.addPropertyChangeListener(this);
//mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
mainFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
logger.info("Minimize the MainFrameWindows instead close it");
((MainFrame)e.getSource()).setState(MainFrame.ICONIFIED);
logger.info("EVENTO ICONIZZAZIONE: " + e.getSource().toString());
}
});
WindowListener exitListener = new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("GUI SingleFrameApplication --> windowClosing");
mainFrame.OnWindowClose();
shutdown();
// mainFrame.setVisible(false);
/*int confirm = JOptionPane.showOptionDialog(frame,
"Are You Sure to Close this Application?",
"Exit Confirmation", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.YES_OPTION) {
System.exit(1);
}*/
}
};
mainFrame.addWindowListener(exitListener);
mainFrame.setVisible(true);
}
@Override
protected void shutdown() {
System.out.println("Entered into GUI ---> shutdown()");
logger.debug("Termino l'applicazione.");
ulogger.info(com.techub.crystalice.utils.Constants.APP_TITLE + "|Arresto " + com.techub.crystalice.utils.Constants.APP_TITLE);
// FileUtils.saveGeneralLogFile(logAppender.getLogInFile());
logAppender.saveGeneralLogFile();
EventBusService.unsubscribe(this);
if (mainFrame != null)
mainFrame.setVisible(false);
}
}

因此,在前面的代码中,我已经声明了showMainFrame()方法,在这个方法中,我添加了一个窗口侦听器,该侦听器必须将我的MainFrame JFrame图标化,以这种方式

mainFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
logger.info("Minimize the MainFrameWindows instead close it");
((MainFrame)e.getSource()).setState(MainFrame.ICONIFIED);
logger.info("EVENTO ICONIZZAZIONE: " + e.getSource().toString());
}
});

然后我有我的MainFrame窗口,它扩展了一个经典的JFrame,类似于以下内容:

公共类MainFrame扩展JFrame{

private final ConfigHelper settings;
private final TasksSettings tasksSettings;
public MainFrame(ConfigHelper settings, TasksSettings tasksSettings, LogAppender logAppender) {
super();
this.settings = settings;
this.tasksSettings = tasksSettings;
.......................................................
.......................................................
.......................................................
DO SOME STUFF
.......................................................
.......................................................
.......................................................
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}

问题是,当我点击MainFrane窗口的X按钮时,它将关闭,并且不会被图标化。

你能帮助我解决这种情况吗?以这种方式,MainFrame窗口被图标化而不是关闭?

我认为问题在于,在执行结束时,它会执行shutDown()执行此操作的方法:

if (mainFrame != null)
mainFrame.setVisible(false);

因此窗口不是图标化的而是关闭的\使其不可见

有什么想法吗?

Tnx

Andrea

如果我理解正确,当你点击X按钮时,你需要在系统托盘中显示应用程序的图标,这是真的吗?

在这种情况下,关闭事件的默认行为已经足够了(如文档中所述,默认行为是关闭时隐藏)。

http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

为了管理系统托盘图标,这里有一些提示:

http://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html

在系统托盘中,您将有必要的代码来隐藏或显示主窗口(我不知道,但也许您甚至不需要JFrame中的窗口侦听器)

相关内容

最新更新