2013-01-23 71 views
0
private void cancelButtonPressed() { 
    jPane = new JOptionPane("quit?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, buttons, buttons[1]); 
    jPane.setName("WPane"); 
    dialog = jPane.createDialog(jPane.getParent(), "Confirm Dialog"); 

    dialog.setVisible(true); 
    dialog.dispose(); 

    Object selectedValue = jPane.getValue(); 
    System.out.println("selectedValue:" + selectedValue); 
    if(selectedValue == null) 
     System.out.println("selectedValue:" + selectedValue); 
     //return CLOSED_OPTION; 
    if(buttons == null) { 
     if(selectedValue instanceof Integer) { 
      //return ((Integer)selectedValue).intValue(); 
      System.out.println("selectedValue instanceof Integer, selectedValue:" + selectedValue); 
     //return CLOSED_OPTION; 
     } 
    } 
    for(int counter = 0, maxCounter = buttons.length; 
     counter < maxCounter; counter++) { 
     if(buttons[counter].equals(selectedValue)){ 
      System.out.println("buttons[counter].equals(selectedValue) selectedValue:" + selectedValue); 
      //return counter; 
     } 
    } 
    //return CLOSED_OPTION; 
} 

在構造函數中我設置按鈕的名稱:我只從null得到:Object selectedValue = jPane.getValue(); (jPane =新的JOptionPane(...)

buttons[0]= new JButton("Yes"); 
    buttons[1]= new JButton("No"); 
    buttons[0].setName("Yes_Next_Btn"); 
    buttons[1].setName("No_Back_Btn"); 

的問題是,我需要setNames的組件:JOptionPane,和所以我不使用:JOptionPane.showConfirmDialog

而在這種情況下: 當我點擊按鈕(是,否)我沒有得到任何價值,只有當我點擊「X」 ,出口我得到null

回答

3

如果您將JButtons作爲選項放入JOptionPane,那麼您需要指定單擊它們時發生的情況。由於您沒有任何ActionActionListener綁定到您的按鈕,沒有任何反應。

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 

public class JOptionPaneTest { 

    public void createAndShowJOptionPane() { 
     final JButton b1 = new JButton("Yes, please"); 
     b1.setName("yes_please"); 
     final JButton b2 = new JButton("No, thx"); 
     b2.setName("no_thx"); 
     final JButton b3 = new JButton("Leave me alone"); 
     b3.setName("leave_me_alone"); 
     Object[] options = new Object[]{b1, b2, b3}; 

     final JOptionPane optionPane = new JOptionPane("Message", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[2]); 
     final JDialog dialog = optionPane.createDialog("Title"); 
     b1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       System.out.println(b1.getText()); 
       optionPane.setValue(0); 
       dialog.dispose(); 
      } 
     }); 
     b2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       System.out.println(b2.getText()); 
       optionPane.setValue(1); 
       dialog.dispose(); 
      } 
     }); 
     b3.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       System.out.println(b3.getText()); 
       optionPane.setValue(2); 
       dialog.dispose(); 
      } 
     }); 
     dialog.setVisible(true); 

     if ((Integer)optionPane.getValue() == 0) { 
      System.out.println("Woohoo!"); 
     } 
    } 

    public static void main(String[] argv) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JOptionPaneTest test = new JOptionPaneTest(); 
       test.createAndShowJOptionPane(); 
      } 
     }); 
    } 
} 
+1

你可以,或者,使用'#的JOptionPane的setValue'動作監聽器裏面,而不是維護另一個狀態變量 – MadProgrammer

+0

@MadProgrammer的,沒注意到那。謝謝。 – predi

3

的問題是,在JOptionPane沒有任何偵聽器添加到按鈕,所以當你點擊它們,沒有任何反應。

嘗試用String!而非更換按鈕...

public class TestOptionPane06 { 

    public static void main(String[] args) { 
     new TestOptionPane06(); 
    } 

    public TestOptionPane06() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

//    JButton buttons[] = new JButton[2]; 
//     
//    buttons[0] = new JButton("Yes"); 
//    buttons[1] = new JButton("No"); 
//    buttons[0].setName("Yes_Next_Btn"); 
//    buttons[1].setName("No_Back_Btn"); 

       String values[] = new String[2]; 
       values[0] = "Yes"; 
       values[1] = "No"; 

       JOptionPane jPane = new JOptionPane("quit?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, values, values[1]); 
       jPane.setName("WPane"); 
       JDialog dialog = jPane.createDialog(jPane.getParent(), "Confirm Dialog"); 

       dialog.setVisible(true); 
       dialog.dispose(); 

       Object selectedValue = jPane.getValue(); 
       System.out.println("selectedValue:" + selectedValue); 
       if (selectedValue == null) { 
        System.out.println("selectedValue:" + selectedValue); 
       } 
       //return CLOSED_OPTION; 
       if (values == null) { 
        if (selectedValue instanceof Integer) { 
         //return ((Integer)selectedValue).intValue(); 
         System.out.println("selectedValue instanceof Integer, selectedValue:" + selectedValue); 
         //return CLOSED_OPTION; 
        } 
       } 
       for (int counter = 0, maxCounter = values.length; 
           counter < maxCounter; counter++) { 
        if (values[counter].equals(selectedValue)) { 
         System.out.println("buttons[counter].equals(selectedValue) selectedValue:" + selectedValue); 
         //return counter; 
        } 
       } 
      } 

     }); 
    } 

} 
+0

完美,非常感謝 – nayden

+0

我被遺忘了或者我不知道我必須添加聽衆... – nayden