【问题标题】:Basic Java Swing, how to exit and dispose of your application/JFrame基本 Java Swing,如何退出和处理您的应用程序/JFrame
【发布时间】:2011-08-16 15:18:53
【问题描述】:

用这样的代码处理 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);    
  }

}

【问题讨论】:

标签: java swing windowlistener


【解决方案1】:
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 我希望是的,对我有用,但在 Swing 中不可能使用 setDefaultCloseOperation(EXIT_ON_CLOSE); 声明两个或更多 download.oracle.com/javase/tutorial/uiswing/components/…,因为它们每个都会杀死 JVM 实例,您必须接受这一点或使用 HIDE 或 DISPOSE 设置另一个 :-)
  • 我喜欢这个。我问了一个通用问题,但这是我正在寻找的答案。另外,其他的都很好,但是这个有代码。
【解决方案2】:

JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE) 在窗口关闭时释放资源。其他一些操作可以看Java教程here.

如果您对自己的选项感到好奇,您可以在方法中使用的可能参数在WindowConstants 接口中定义。

【讨论】:

  • 这与 EXIT_ON_CLOSE 有何不同
  • @Berlin, EXIT_ON_CLOSE 终止应用程序,而DISPOSE_ON_CLOSE 只处理JFrame 实例。
  • DISPOSE_ON_CLOSE 将在没有未处理的 AWT 组件时让 AWT 线程退出。这将是您的最后一个线程,因此主线程将退出。
  • 没错。 @Berlin,查看指向 Java 教程的链接;它列举了差异。
【解决方案3】:

如果您需要在关闭应用程序时执行一些操作,您可能需要一个 shut down hook。看看这个post

【讨论】:

    【解决方案4】:
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    

    JFrame.setDefaultCloseOperation(int operation)

    JFrame.EXIT_ON_CLOSE

    【讨论】:

    • 这实际上使用了System.exit(0)
    • 是否有 frame.setDefaultCloseOperation 的事件处理程序?例如。有没有我可以覆盖的方法? onExit() { } 或类似的东西。
    • @Berlin:一旦你设置了默认的关闭操作,就不需要重写方法了。除非你实现了一个窗口监听器来做一些不同的事情,否则默认的关闭操作是在用户关闭窗口时执行的。
    • 但如果我愿意,我可以使用窗口监听器。
    • @Berlin:是的,如果你愿意的话,你可以用窗口监听器做其他事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2010-10-10
    相关资源
    最近更新 更多