2012-06-03 68 views
7

我如何調用額外的操作,當用戶關閉窗口JFrame?我必須停止現有的線程。上終止的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; 
     } 

我需要關閉套接字線程與合作。最佳做法是什麼?

+1

參見[*§12.8。計劃退出*](http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.8)。 – trashgod

回答

10

使用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左上角的窗口關閉圖標,則這是正確的,但如果JFrame通過調用'dispose()'關閉,則不是true,而不是非守護線程正在運行。 –

3
  • 你有一個WindowListener添加到JFrame

  • 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);