简体   繁体   English

使 X 关闭按钮在 Java 中打开一个新的 JFrame

[英]Make the X Close button open a new JFrame in Java

I would like to make it so that when you click the X close button on the top right, a new JFrame will appear and the current one will close.我想这样当你点击右上角的 X 关闭按钮时,会出现一个新的JFrame并关闭当前的。

How would I be able to do it?我怎么能做到呢?

The question is, can another JFrame open automatically when one JFrame closes.问题是,当一个 JFrame 关闭时,另一个 JFrame 能否自动打开。 As noted in comments, two possible common solutions include use of a WindowListener or use of a modal dialog (one example of which is as mentioned by Chance Hoard, a JOptionPane, which again is one solution, but certainly not the only solution).如评论中所述,两种可能的常见解决方案包括使用 WindowListener 或使用模式对话框(其中一个示例是 Chance Hoard 提到的 JOptionPane,这也是一种解决方案,但肯定不是唯一的解决方案)。

For a WindowListener to work, you can open the new JFrame in the windowClosed() method, for example:要使 WindowListener 工作,您可以在windowClosed()方法中打开新的 JFrame,例如:

JFrame frame1 = new JFrame("Frame 1");
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame1.add(Box.createRigidArea(new Dimension(500, 400)));
frame1.pack();
frame1.setLocationByPlatform(true);
frame1.setVisible(true);

frame1.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosed(WindowEvent e) {
        JFrame frame2 = new JFrame("Frame 2");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.add(Box.createRigidArea(new Dimension(500, 400)));
        frame2.pack();
        frame2.setLocationByPlatform(true);
        frame2.setVisible(true);
    }
});

The key here is to set the default close operation to not be JFrame.EXIT_ON_CLOSE so as not to close down the JVM after the initial JFrame window has closed. The key here is to set the default close operation to not be JFrame.EXIT_ON_CLOSE so as not to close down the JVM after the initial JFrame window has closed. In the window listener's windowClosed() method, create and display the 2nd JFrame.在 window 监听器的windowClosed()方法中,创建并显示第二个 JFrame。


To solve this using a modal dialog, I would create the second window, which could be a JFrame, first, to give the dialog an parent application, but then I would write code to display it after displaying the modal dialog.为了使用模态对话框解决这个问题,我将创建第二个 window,它可能是 JFrame,首先,为对话框提供父应用程序,但随后我将编写代码以在显示模态对话框显示它。 This way, it only shows up once the dialog is no longer visible:这样,它只会在对话框不再可见时显示:

JFrame frame3 = new JFrame("Frame 3");
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame3.add(Box.createRigidArea(new Dimension(500, 400)));
frame3.pack();
frame3.setLocationByPlatform(true);
// don't display this JFrame yet

// create the dialog, passing in the JFrame and making the dialog "modal"
JDialog dialog1 = new JDialog(frame3, "Dialog 1", ModalityType.APPLICATION_MODAL);
dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog1.add(Box.createRigidArea(new Dimension(500, 400)));
dialog1.pack();
dialog1.setLocationRelativeTo(frame3);

// show the dialog first
dialog1.setVisible(true);

// this is called only after the dialog is no longer visible
frame3.setVisible(true);

Having said all this, I still recommend that you read the The Use of Multiple JFrames, Good/Bad Practice?说了这么多,我还是推荐你阅读The Use of Multiple JFrames, Good/Bad Practice? link and I recommend avoiding both of the above "solutions" and instead recommend that one create and display a single main application window, a JFrame, and instead swap GUI views , usually JPanels, by using a CardLayout.链接,我建议避免使用上述两种“解决方案”,而是建议创建并显示单个主应用程序 window、JFrame,而是使用 CardLayout 交换 GUI视图,通常是 JPanel。

I went out and tried this myself and have some bad news.我自己出去尝试了这个,但有一些坏消息。 The method i'm sure you're familiar with setDefaultCloseOperation requires an int as parameter, not a function nor frame.我确定您熟悉 setDefaultCloseOperation 的方法需要一个 int 作为参数,而不是 function 或框架。 Therefore the only way I can see this action being feasible is if you create a JOptionPane instead, that has built in functions to tell your program what to do if the pane is simply closed.因此,我认为此操作可行的唯一方法是,如果您改为创建一个 JOptionPane,它具有内置函数来告诉您的程序在窗格关闭时该怎么做。 I wish you luck on your adventure.祝你冒险顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM