基础Java Swing,如何退出并销毁您的应用程序/JFrame。

10

在类似于这样的代码中,如何优雅地处理JFrame的销毁?我想要处理窗口的退出和关闭。

我知道我们不应该使用System.exit();

public class JavaCellularAutomataSquare {

  public static final String TITLE = "Cellular Automata - Squaring Example";

  private int maxWidth = 600;
  private int maxHeight = 600;

  public void launch() {    
    final JFrame frame = new JFrame(TITLE);   
    frame.setLocation(20, 20);    
    frame.setPreferredSize(new Dimension(maxWidth, maxHeight));
    frame.setResizable(false);
    frame.setFocusable(true);

    final JPanel panel = new JPanel();
    panel.setLocation(20, 20);
    panel.setVisible(true);
    panel.setPreferredSize(new Dimension(maxWidth, maxHeight));    
    panel.setFocusable(true);
    panel.setBackground(Color.white);

    // Panel setup, toggle visibility on frame
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);    
  }

}

2
请在此处查看Andrew的答案:awt-window-close-listener-event - Hovercraft Full Of Eels
可能是如何在程序内部退出Java应用程序的重复问题。 - james.garriss
4个回答

10
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ClosingFrame extends JFrame {

    private JMenuBar MenuBar = new JMenuBar();
    private JFrame frame = new JFrame();
    private static final long serialVersionUID = 1L;
    private JMenu File = new JMenu("File");
    private JMenuItem Exit = new JMenuItem("Exit");

    public ClosingFrame() {
        File.add(Exit);
        MenuBar.add(File);
        Exit.addActionListener(new ExitListener());
        WindowListener exitListener = new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                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(0);
                }
            }
        };
        frame.addWindowListener(exitListener);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setJMenuBar(MenuBar);
        frame.setPreferredSize(new Dimension(400, 300));
        frame.setLocation(100, 100);
        frame.pack();
        frame.setVisible(true);
    }

    private class ExitListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            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(0);
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ClosingFrame cf = new ClosingFrame();
            }
        });
    }
}

这是处理关闭事件的标准(好的方式)吗?看起来不错。 - Berlin Brown
@Berlin Brown,我希望是的,它对我来说像我预期的那样工作,但在Swing中不可能声明两个或更多http://download.oracle.com/javase/tutorial/uiswing/components/toplevel.html,并设置setDefaultCloseOperation(EXIT_ON_CLOSE);,因为它们中的每一个都会终止JVM实例,你必须接受这一点或使用HIDE或DISPOSE设置另一个 :-) - mKorbel
我喜欢这个。我问了一个比较通用的问题,但这是我想要的答案。其他的也不错,但这个有代码。 - Berlin Brown

8

JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE) 的作用是在窗口关闭时释放资源。您可以在Java教程这里中看到一些其他操作。

如果您对可用的参数有兴趣,可以在WindowConstants接口中查看定义。


这与EXIT_ON_CLOSE有何不同? - Berlin Brown
@Berlin,“EXIT_ON_CLOSE”终止应用程序,而“DISPOSE_ON_CLOSE”仅处理“JFrame”实例。 - mre
DISPOSE_ON_CLOSE 将在没有未处置的 AWT 组件时使 AWT 线程退出。那将是您的最后一个线程,因此主线程将退出。 - Clint
没错。@Berlin,看一下那个Java教程的链接,它列举了这些不同之处。 - Feanor

3

如果您需要在关闭应用程序时执行一些操作,可能需要一个关闭挂钩。请查看此文章


1

这实际上使用了 System.exit(0) - Feanor
有没有针对frame.setDefaultCloseOperation的事件处理程序?例如,有没有我可以重写的方法?onExit(){}或类似的东西。 - Berlin Brown
@Berlin:一旦设置了默认的关闭操作,就无需覆盖一个方法。除非您实现了一个窗口侦听器来执行不同的操作,否则当用户关闭窗口时会执行默认的关闭操作。 - Feanor
但如果我想的话,我可以使用窗口监听器。 - Berlin Brown
1
@Berlin:是的,如果你想的话,你可以使用窗口监听器做其他事情。 - Feanor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接