2012-03-23 4 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)을 사용하여 닫습니다.

관련 문제