java 终止 JFrame 上正在运行的线程关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10873668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:56:13  来源:igfitidea点击:

Terminate running threads on JFrame close

javamultithreadingswingjframewindowlistener

提问by J.Olufsen

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

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

As I understand it, setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);causes the frame to be closed and its thread to be stopped. Should threads be closed after JFrame.EXIT_ON_CLOSE?

据我了解,setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);导致框架关闭并停止其线程。线程应该在之后关闭JFrame.EXIT_ON_CLOSE吗?

Client:

客户:

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


I need to close sockets the thread's working with. What is the best practice to do that?

我需要关闭线程正在使用的套接字。这样做的最佳做法是什么?

回答by Jeffrey

Using JFrame.EXIT_ON_CLOSEactually terminates the JVM (System.exit). All running threads will automatically be stopped.

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

If you want to perform some action when a JFrameis about to close, use a WindowListener.

如果要在 aJFrame即将关闭时执行某些操作,请使用 a WindowListener

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

回答by mKorbel

  • You have to add a WindowListenerto the JFrame.

  • Inside the windowClosingmethod, you can provide required code.

  • 您必须WindowListenerJFrame.

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

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

回答by Luke Carpenter

You can set the default close operation on the JFrame

您可以在 JFrame 上设置默认关闭操作

JFrame frame = new JFrame("My Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);