2017-08-26 187 views
1

我是新來學習javax.swing,並有疑問。如何通過關閉JFrame直接關閉JOptionPane?

我有一個JFrame,其defaultCloseOperation設置爲EXIT_ON_CLOSE。我有另一個組件 - JOptionPane-超過JFrame。我想要發生的是,即使重點在JOptionPane消息對話框上,我也希望程序在我單擊JFrame窗口上的x按鈕時終止。

因此,我正在尋找將JFrame放在焦點上,而不關閉JOptionPane消息對話框,以便我可以關閉JFrame窗口,從而使程序終止。

這裏是我的代碼:

import javax.swing.*; 

public class JJSS { 
    public JFrame jf; 

    public JJSS(){ 
     jf = new JFrame(); 

     jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     jf.setSize(400, 400); 
     jf.setVisible(true); 
    } 

    public void runMethod(){ 
     String str = JOptionPane.showInputDialog(jf, "Enter something..."); 
     str = String.valueOf(new StringBuffer(str).reverse()); 
     JOptionPane.showMessageDialog(jf, "Reversed: "+str, "Output", JOptionPane.PLAIN_MESSAGE); 
    } 

    public static void main(String[] args){ 
     new JJSS().runMethod(); 
     System.exit(0); 
    } 

} 

在當前的代碼,當我點擊JFrame窗口的關閉按鈕(x)沒有任何反應。

當JOptionPane對話框仍然處於關閉狀態時,如何關注JFrame窗口並關閉JFrame窗口來終止程序?

+0

請參閱編輯回答,並隨時問,如果你有任何問題。 –

回答

4

對話框模態是關鍵

作爲模態可以防止用戶在顯示對話框,其中GUI的其他組分相互作用不能用正常的JOptionPane或與任何模態對話框做到這一點。如果你創建一個非模態對話框,你只能得到這個工作,這意味着JOptionPane必須不是用JOptionPane靜態工廠方法創建的,而是以非傳統的方式使用JOptionPane構造函數創建的 - 請檢查JOptionPane API這個怎麼做。

例如:

import java.awt.Dialog.ModalityType; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 

import javax.swing.*; 

public class NonModalJOptionPane { 

    private static void createAndShowGui() { 
     JPanel panel = new JPanel(); 
     panel.setPreferredSize(new Dimension(400, 300)); 

     final JFrame frame = new JFrame("NonModalJOptionPane"); 

     panel.add(new JButton(new AbstractAction("Push Me") { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       JOptionPane optionPane = new JOptionPane("My Message", JOptionPane.PLAIN_MESSAGE); 
       JDialog dialog = optionPane.createDialog(frame, "My Option"); 
       dialog.setModalityType(ModalityType.MODELESS); // **** key *** 
       dialog.setVisible(true); 
      } 
     })); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(panel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 

這個代碼的關鍵是在這裏:

// create the JOptionPane using one of its constructors 
JOptionPane optionPane = new JOptionPane("My Message", JOptionPane.PLAIN_MESSAGE); 

// create a JDialog from it, tying it to the parent JFrame, here called "frame" 
JDialog dialog = optionPane.createDialog(frame, "My Option"); 

// setting the modality type so that it is **not** modal 
dialog.setModalityType(ModalityType.MODELESS); // **** key *** 

// and then displaying it 
dialog.setVisible(true); 

當我通過它的構造函數創建的JOptionPane,而不是通過一個靜態方法,我創建一個JDialog並設置它是MODELESS,然後顯示它。


另一個可行的選擇是創建您自己的JDialog,確保您將它設置爲非模態。

例如,您可以將以下代碼添加到上面的代碼:

panel.add(new JButton(new AbstractAction("Push Me 2 -- Using Dialog") { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     // button that when pressed, closes the JDialog that holds it 
     // similar to a JOptionPane's OK button 
     JButton disposeWinBtn = new JButton(new DisposeWindowAction("OK", KeyEvent.VK_O)); 

     // create a bunch of JPanels, add components to them, ... 
     JPanel bottomPanel = new JPanel(); 
     bottomPanel.add(disposeWinBtn); 

     JLabel msgLabel = new JLabel("My Message"); 
     JPanel msgPanel = new JPanel(); 
     msgPanel.add(msgLabel); 

     JPanel panel = new JPanel(new BorderLayout()); 
     panel.add(msgPanel, BorderLayout.CENTER); 
     panel.add(bottomPanel, BorderLayout.PAGE_END); 

     // create a JDialog whose parent component is the main JFrame 
     // and make sure that it is *****non-modal ***** <===== this is KEY ***** 
     JDialog dialog = new JDialog(frame, "Dialog", ModalityType.MODELESS); 
     dialog.add(panel); // add the JPanel, panel, created above, with components 
     dialog.pack(); // have layout managers do their thing 
     dialog.setLocationRelativeTo(frame); // center it over the main JFrame 
     dialog.setVisible(true); // and display it 
    } 
})); 

不到其中的第一個按鈕添加。您還需要在DisposeWindowAction類,允許按鈕關閉並正在顯示它的窗口(這裏一個JDialog窗)的配置:

import java.awt.Component; 
import java.awt.Window; 
import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.SwingUtilities; 

@SuppressWarnings("serial") 
public class DisposeWindowAction extends AbstractAction { 
    public DisposeWindowAction(String name, int mnemonic) { 
     super(name); 
     putValue(MNEMONIC_KEY, mnemonic); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     Component component = (Component) e.getSource(); 
     if (component == null) { 
      return; 
     } 
     Window win = SwingUtilities.getWindowAncestor(component); 
     if (win == null) { 
      return; 
     } 
     win.dispose(); 
    } 
} 
+0

這是一個很好解釋的答案。然而,我必須等待至少一年,才能真正理解你寫的代碼:我只是一個剛剛進入GUI的學生(我今年上大學)。也許有一天,當我成爲一名軟件工程師時,我甚至可以修改這些代碼。謝謝btw :-) – progyammer

+1

@progyammer:請查看[Swing教程](http://docs.oracle.com/javase/tutorial/uiswing/index.html)以及其他Swing資源:[Swing Info ](http://stackoverflow.com/questions/tagged/swing),並詢問你是否有關於答案的具體問題。 –