2012-08-31 57 views
5

我正在製作一個文字處理器應用程序來練習Java,我希望它能夠在用戶嘗試關閉應用程序時,JFrame會出現要求保存更改的信息。setDefaultCloseOperation顯示一個JFrame而不是

我正在考慮setDefaultCloseOperation(),但到目前爲止我的運氣不大。如果可能的話,我還希望看到他用戶單擊窗口右上角的「X」。

回答

9

您可以將JFrame DefaultCloseOperation設置爲類似DO_NOTHING,然後設置WindowsListener以抓取關閉事件並執行所需操作。我會在幾分鐘後發佈一個例子。

編輯:這裏的例子:

public static void main(String[] args) { 
     final JFrame frame = new JFrame("Test Frame"); 

     frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 

     frame.setSize(800, 600); 

     frame.addWindowListener(new WindowAdapter() { 
      //I skipped unused callbacks for readability 

      @Override 
      public void windowClosing(WindowEvent e) { 
       if(JOptionPane.showConfirmDialog(frame, "Are you sure ?") == JOptionPane.OK_OPTION){ 
        frame.setVisible(false); 
        frame.dispose(); 
       } 
      } 
     }); 

     frame.setVisible(true); 
    } 
+0

1)ITYM'frame.addWindowListener(新WindowAdapter的(){'(否則它是一個編譯錯誤)2)由於JRE不會結束EDT仍在運行。這是調用'System.exit(0)'可能是必要的罕見場合之一。 –

+0

@AndrewThompson你認爲什麼是最安全的方式來關閉應用程序,而不是System.exit(0); ? – Andrei0427

+0

那麼,你可能會首先看看正在運行的線程,並檢查它是否只有EDT仍在運行。如果是這樣,那麼使用'System.exit(0)'來結束JVM是非常安全的。但有一個更好的解決方案在這裏,看到我的答案.. –

3
  • 您必須添加一個WindowListenerJFrame

  • 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 == JOptionPane.OK_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.OK_OPTION) { 
       System.exit(0); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       ClosingFrame cf = new ClosingFrame(); 
      } 
     }); 
    } 
} 
3
import java.awt.event.*; 
import javax.swing.*; 

public class QuickGuiTest { 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       final JFrame frame = new JFrame("Test Frame"); 

       frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
       frame.setSize(600, 400); 
       frame.addWindowListener(new WindowAdapter() { 
        @Override 
        public void windowClosing(WindowEvent e) { 
         int result = JOptionPane.showConfirmDialog(
           frame, "Are you sure?"); 
         if(result==JOptionPane.OK_OPTION){ 
          // NOW we change it to dispose on close.. 
          frame.setDefaultCloseOperation(
            JFrame.DISPOSE_ON_CLOSE); 
          frame.setVisible(false); 
          frame.dispose(); 
         } 
        } 
       }); 
       frame.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
}