2017-08-26 189 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),并询问你是否有关于答案的具体问题。 –