2013-05-18 13 views
1

我有多個自定義對話框,我想要一個簡單的方法來指定關閉操作。首先我使用匿名內部windowListener類,併爲每個對話框指定關閉方法。WindowClosing方法

我認爲創建我自己的類並實現WindowListener類併爲所有對話框指定一個窗口關閉方法會更高效。

所以我做到了這一點,它效果很好。

public class WindowWatcher implements WindowListener{ 

    @Override 
    public void windowClosing(WindowEvent e) { 
     System.out.println("Are you sure you wish to exit?"); 
     int Answer = JOptionPane.showConfirmDialog(frame, "Are you sure want to exit?", "Quit", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 
      if (Answer == JOptionPane.YES_OPTION) { 
       System.exit(0); 
      } 
     } 

    } 

注:有類中的其他實現的方法..

反正我跑的問題是,當我點擊退出,然後單擊否,然後我嘗試用一​​個對話框來進行並說點擊確定..沒有任何反應。

我明白這與調用JOptionPane的UNINITIALIZED_VALUE有關。

我需要看到調用optionPane到這個UNINITIALIZED_VALUE。我認爲??

類似:

 optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE); 

上面的代碼假設我要在訪問選項窗格不過。但是,在我的課堂'WindowWatcher'類中,我無法訪問它。

任何想法,我可以做到這一點?也許我可以將e.GetSource()作爲JOptionPane。

編輯。

((JOptionPane)e.getSource()).setValue(JOptionPane.UNINITIALIZED_VALUE); 

上面沒有工作。 「JDialog不能作爲JoptionPane投射」

非常感謝!

+1

JOptionPane也取消了,請我錯過目標 – mKorbel

+0

請您在Q體內丟失編輯,請檢查一下是否重要 – mKorbel

回答

3
  • 請您可以使用此代碼示例爲您SSCCE,

  • 放在那裏多JDialogs,

  • 添加的WindowListener

  • 修改內部showOptionDialog()

  • 代碼

    通過JOptionPane的父項

  • 開始與showOptionDialog(...........)

  • 然後我會在這裏刪除這個帖子

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class ClosingFrame extends JFrame { 

    private JMenuBar MenuBar = new JMenuBar(); 
    private static JFrame frame = new JFrame(); 
    private static JFrame frame1 = new JFrame("DefaultCloseOperation(JFrame.HIDE_ON_CLOSE)"); 
    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) { 
       frame.setVisible(false); 
       /*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.YES_OPTION) { 
       System.exit(1); 
       }*/ 
      } 
     }; 
     JButton btn = new JButton("Show second JFrame"); 
     btn.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       frame1.setVisible(true); 
      } 
     }); 
     frame.add(btn, BorderLayout.SOUTH); 
     frame.addWindowListener(exitListener); 
     frame.setDefaultCloseOperation(JFrame.HIDE_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.YES_OPTION) { 
       System.exit(1); 
      } 
     } 
    } 

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

      @Override 
      public void run() { 
       ClosingFrame cf = new ClosingFrame(); 
       JButton btn = new JButton("Show first JFrame"); 
       btn.addActionListener(new ActionListener() { 

        public void actionPerformed(ActionEvent e) { 
         frame.setVisible(true); 
        } 
       }); 
       frame1.add(btn, BorderLayout.SOUTH); 
       frame1.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 
       frame1.setPreferredSize(new Dimension(400, 300)); 
       frame1.setLocation(100, 400); 
       frame1.pack(); 
       frame1.setVisible(true); 
      } 
     }); 
    } 
} 

編輯

你有這樣的「ExitListener」當用戶點擊 退出菜單項,但是當我點擊紅色的X,它只是設置可見的 虛假效果很好。我沒有看到這對我有什麼幫助,對於Dialog或 JOptionPanes ..你想要我添加什麼windowListener?更多 混淆說實話。

  1. 因爲我已經要求張貼SSCCE

  2. 看到API方法setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE, EXIT_ON_CLOSE, ....);

    • 默認值是JFrame.HIDE_ON_CLOSE,顯示和使用作爲教訓
    • 這都是獨立的,在API中實施的直接方法,
    • JFrame.HIDE_ON_CLOSE == frame.setVisible(false);
    • Swing GUI的要求更改爲setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);,因爲hidded JFrameJDialog不到風度等於當前的JVM終止,和生活,直到重新啓動PC或關閉,EXIT_ON_CLOSE以終止當前的JVM System.exit(int);
  3. WindowListener是API實現的一個單獨並直接的方法,如何管理

    • 塊中的所有設置裏面windowXxx(Xxx == methods in WindowListener)
    • 否則沒什麼happends
  4. 回到我的代碼貼在這裏

  5. 將只執行代碼

    • 禁用frame.setVisible(false); == //frame.setVisible(false);
    • 和(下面的代碼行)刪除/**/,然後像上面提到的那樣工作
+0

我只是要檢查您的代碼。我的對話框和optionPanes都是在方法中創建的..我應該讓它們變成我的類的變量嗎?似乎有道理,然後我可以在任何地方訪問它們。 – whiteElephant

+0

@ mKorbel,你有這個'ExitListener',當用戶點擊退出菜單項時工作正常,但是當我點擊紅色的x時,它只會將可見設置爲false。對於Dialog或JOptionPanes,我看不出這對我有什麼幫助。你想要我添加什麼windowListener?說實話更容易混淆。 – whiteElephant

+0

@whiteElephant在這裏看到我的編輯 – mKorbel

0

我認爲這將是更有效地創建自己的類,並實現WindowListener的類,並指定所有對話框一個窗口關閉的方法。

好主意。我爲此創建了自己的班級。有關我的版本,請參閱Closing An Application

您的發佈代碼對我來說很合理。您是否在if語句內添加了println()語句以查看是否調用了System.exit()方法。

我的解決方案稍有不同。它爲您管理框架的default close operation。此值用於確定單擊關閉圖標時發生的情況。默認情況下,它必須設置爲do nothing,以防止用戶取消關閉。如果用戶接受關閉,那麼你需要設置它做你想要的屬性。

+0

+1的詳細說明,我仍然認爲在Java(non_deamon)線程的其餘部分中,System.exit(0)和EXIT_ON_CLOSE之間沒有區別, – mKorbel

0

我可能會有點遲到了,但對於將來參考:

注:此方法是有點不合常規,比平均法有點長,但它的作品,所以如果它歸結到它,這是可以接受的解決方案。

我喜歡做的是完全跳過JOptionPane並使用JDialog,只需在類的構造函數中構造消息和按鈕,然後讓ActionListener準備好捕獲所有按鈕,以防萬一,將WindowsListener附加到對話框,這樣你就可以捕捉到所有可能的結果。

假設你正在使用你自己的類的框架:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class WindowCloseDialog implements ActionListener, WindowListener { 

    private JDialog closeDialog; 
    private JButton yesButton, noButton; 

    WindowCloseDialog() { 
     JFrame myFrame = new JFrame("Window Close Dialog Application"); 
     myFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 
     myFrame.addWindowListener(this);//catch the window being closed 
     ... 
     closeDialog = new JDialog("Quit"); 
     closeDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 
     closeDialog.addWindowListener(this);//catch the dialog being closed 
     JLabel closeLabel = new JLabel("Would you like to exit?"); 
     yesButton = new JButton("Yes"); 
     yesButton.addActionListener(this);//catch an answer of yes 
     noButton = new JButton("No"); 
     noButton.addActionListener(this);//catch an answer of no 
     //add your components to your closeDialog in whatever manner you fancy 
     ... 
    } 
    ... 
    @Override 
    public void windowClosing(WindowEvent w) { 
     if (w.getWindow().equals(myFrame)) { 
      closeDialog.setVisible(true); 
     } else if (w.getWindow().equals(closeDialog)) { 
      closeDialog.setVisible(false);//no exit on dialog close 
     } 
    } 
    ... 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     if(e.getSource().equals(yesButton)) { 
      closeDialog.setVisible(false); 
      myFrame.setVisible(false); 
      System.exit(0); 
     } else if (e.getSource().equals(noButton)) { 
      closeDialog.setVisible(false); 
     } 
    } 
    ... 
} 

,如果你不使用你自己的類的框架,那麼同樣的規則仍然適用,因爲如果是這樣的話,其他類說所以你可以像我的代碼一樣完成工作,只需要省去構建自己的框架的部分。

相關問題