2017-08-26 30 views
1

私はjavax.swingを学ぶのが初めてで疑いがあります。JFrameを閉じてJOptionPaneを直接閉じる方法は?

私は、defaultCloseOperationEXIT_ON_CLOSEに設定されているJFrameを持っています。私は別のコンポーネント - JOptionPane - JFrameを持っています。私は、JOptionPaneメッセージダイアログに焦点を当てていても、JFrameウィンドウのxボタンをクリックすると、プログラムを終了させたいと考えています。

正確には、JOptionPaneメッセージダイアログが表示されないようにして、JFrameをフォーカスして表示させたいのですが、JFrameウィンドウを閉じてプログラムを終了させることができます。私はJFrameウィンドウの閉じるボタン(x)をクリックしたときに何も起こらない、現在のコードで

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

} 

は、ここに私のコードです。

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()); 
    } 
} 

このコードへの鍵はここにある:

私はそのコンストラクタ経由ではなく、静的なメソッドを介してのJOptionPaneを作成
// 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); 

、私は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 
    } 
})); 

ちょうど最初のボタンが追加された場所の下に。あなたは、ボタンを閉じて、それを表示しているウィンドウ(ここではJDialogのウィンドウ)を処分することを可能にするDisposeWindowActionクラスを、また必要があります

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

これは非常によく説明された答えでした。しかし、あなたが書いたコードを実際に理解するには、少なくとも1年は待たなければなりません。私はただのGUIに移行している学生です(今年は大学に来ました)。いつか、私がソフトウェアエンジニアになれば、おそらくこのコードを "修正する"ことさえできるでしょう。ありがとうbtw :-) – progyammer

+1

@progyammer:[Swingチュートリアル](http://docs.oracle.com/javase/tutorial/uiswing/index.html)とその他のSwingリソースはこちら[Swing Info ](http://stackoverflow.com/questions/tagged/swing)、あなたは答えについて特定の質問があるかどうか尋ねます。 –

関連する問題