Terminate running threads on JFrame close

前端 未结 3 1733
梦谈多话
梦谈多话 2021-02-09 11:09

How do I invoke extra operations when the user closes a JFrame window? I have to stop existing threads.

As I understand it, setDefaultCloseOperation(J

3条回答
  •  悲哀的现实
    2021-02-09 11:30

    • You have to add a WindowListener to the JFrame.

    • Inside the windowClosing method, you can provide required code.

    For example:

    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 == 0) {
                        System.exit(1);
                    }
                }
            };
            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 == 0) {
                    System.exit(1);
                }
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    ClosingFrame cf = new ClosingFrame();
                }
            });
        }
    }
    

提交回复
热议问题