在JFrame关闭时终止运行中的线程

8

当用户关闭JFrame窗口时,我如何调用额外的操作?我必须停止现有线程。

据我所知,setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);会导致窗口关闭并停止其线程。在JFrame.EXIT_ON_CLOSE之后是否应关闭线程?

客户端:

static boolean TERMINATE = false;
public static void main(String[] args) {
// some threads created
 while(true) {

                if(TERMINATE){
                         // do before frame closed
                    break;
                         }
              }

}
    private static JPanel startGUI(){
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel gui = new JPanel();
             f.add( gui);
             f.setSize(500,500);
             f.setVisible(true);
             return gui;
        }

我需要关闭线程正在使用的socket。最佳实践应该是什么?


1
请参见§12.8.程序退出 - trashgod
3个回答

11

使用JFrame.EXIT_ON_CLOSE实际上终止了JVM (System.exit)。所有正在运行的线程将自动停止。

如果您想在JFrame关闭前执行某些操作,可以使用WindowListener

JFrame frame = ...
frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        // close sockets, etc
    }
});

2
如果用户按JFrame左上角的窗口关闭图标,则为真,但如果通过调用 dispose() 关闭JFrame则不为真,非守护线程正在运行也不为真。 - Hovercraft Full Of Eels

3
  • 您需要向 JFrame 添加一个 WindowListener

  • windowClosing 方法中,您可以提供所需的代码。

例如:

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();
            }
        });
    }
}

1
您可以在JFrame上设置默认的关闭操作。
JFrame frame = new JFrame("My Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

这种方法不会杀死线程,它们仍然保持活动状态。 - Lele

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