2012-03-23 7 views
1

私のアプリケーションを閉じると表示されるオプションペインがあります(windowClosing())。 私には、終了、最小化、またはキャンセルのオプションがあります。オプションペインをキャンセルします。

アプリケーション全体を閉じずに[キャンセル]を選択すると、オプションペインを閉じるにはどうすればよいですか?あなたが削除する必要が

 final JDialog dialog = new JDialog(frame, 
            "Click a button", 
            true); 
     dialog.setContentPane(optionPane); 
     dialog.setDefaultCloseOperation(
      JDialog.DO_NOTHING_ON_CLOSE); 
     dialog.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent we) { 
       setLabel("Thwarted user attempt to close window."); 
      } 
     }); 
     optionPane.addPropertyChangeListener(
      new PropertyChangeListener() { 
       public void propertyChange(PropertyChangeEvent e) { 
        String prop = e.getPropertyName(); 

        if (dialog.isVisible() 
        && (e.getSource() == optionPane) 
        && (prop.equals(JOptionPane.VALUE_PROPERTY))) { 
         //If you were going to check something 
         //before closing the window, you'd do 
         //it here. 
         dialog.setVisible(false); 
        } 
       } 
      }); 
     dialog.pack(); 
     dialog.setVisible(true); 

     int value = ((Integer)optionPane.getValue()).intValue(); 
     if (value == JOptionPane.YES_OPTION) { 
      setLabel("Good."); 
     } else if (value == JOptionPane.NO_OPTION) { 
      setLabel("Try using the window decorations " 
        + "to close the non-auto-closing dialog. " 
        + "You can't!"); 
     } 

Oracle documentation

Object[]options = {"Minimize", "Exit","Cancel"}; 

     int selection = JOptionPane.showOptionDialog(
      null, "Please select option", "Options", 0, 
      JOptionPane.INFORMATION_MESSAGE, null, options, options[1]); 
     System.out.println(selection); 

     switch(selection) 
     { 
      case 2: 
      { 
       // do something 
      } 
     } 
+0

'JOptionPane'が呼び出されたときに、あなたがやっていることの完全なコードを投稿してください。 – Marcelo

答えて

3
ユーザーが "キャンセル" を選択した場合は、あなたの windowClosing()メソッド内 yourFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);を呼び出すことができます

....

+1

私もそれを得ました...ありがとう、申し訳ありません – Arianule

+1

これを正しい答えとして選択できます。 –

+1

+1、私は間違った意味で質問を理解しました:-) –

2
If (selection == JOptionPane.CANCEL_OPTION) 
{ 
    // DO your stuff related to cancel click event. 
} 
1

はヒントを提供しますデフォルトのクローズ操作を行い、独自のリスナーを追加してから、setVisible(false)を使用してリスナーを閉じます。

関連する問題