2013-01-19 56 views
7

在我的GUI项目中有一个方法用于显示其上有多个组件的JOptionPane,这些组件中的其中2个为ButtonGroups,其中每个组件有,第一组中第一个按钮被选中默认情况下,在第二组中默认选择第二个按钮,在第二组中我希望禁用第一个按钮,直到选择第一组中的第二个按钮,即如果用户对BG1中的默认选择满意,那么他们不能在BG2中进行选择,只有当他们在BG1中进行第二次选择时,他们才能在BG2中选择其他选项。更新JOptionPane以反映组件状态更改

这种类型的行为可能与JOptionPane

一直在寻找JDialog,JOptionPane的教程和做其他研究,但这些案例都没有证明有帮助。如果有人可以给我一个方向,以一个可能的解决方案,将是一个梦幻般的...

+2

*“是这种类型的行为可能与'的JOptionPane “?”*当然。这和你在'JFrame'中做的事情一样。如果你无法在框架中实现它,请发布最佳尝试的[SSCCE](http://sscce.org/)。 –

+4

'JOptionPane'的消息参数需要一个'Object'。如果你传递一个'Component','JOptionPane'将把它用作“主”视图,在它周围添加图标和按钮 – MadProgrammer

+12

如果你要回答你自己的问题,请发表你的答案作为答案,并接受你的答案。 (这样人们不会浪费时间来阅读你的问题,希望能够回答它) – Gus

回答

0

我不认为这是可行的JOption。但我认为这是JDialog的可能性。

examble:

当你打开,你可以使用命令的JFrame(在这里你必须写你的窗口名).enable(假)

的对话框,你可以得到它在关闭按钮当复选框为真时,您可能有一个复选框 。它会显示一个按钮,当你点击它可以使按钮invisble

0

最好的事情是去的JDialog

因为JOptionPane的是不支持这种多组件。

0

elective.addActionListener声明我叫错变量,有cp12代替cp6,张贴了我切下来的代码下面,一切都很好,谢谢

public void displayAddSomething(ArrayList<String> items) 
    { 
// cut down version only showing the button panels 

    // initialising the variables  
    JPanel buttPanel = new JPanel(new GridLayout(0, 1)); 
    JPanel pointPanel = new JPanel(new GridLayout(0, 1)); 
    JRadioButton core = new JRadioButton("Core Item: ", true); 
    final JRadioButton elective = new JRadioButton("Elective Item: "); 
    final JRadioButton cp6 = new JRadioButton("6 Points: "); 
    JRadioButton cp12 = new JRadioButton("12 Points: ", true); 
    ButtonGroup bg1 = new ButtonGroup(); 
    ButtonGroup bg2 = new ButtonGroup(); 

    // adding the buttons to buttPanel and the button group 1 
    buttPanel.add(new JLabel("Select Course Type")); 
    buttPanel.add(core); 
    buttPanel.add(elective); 
    buttPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2)); 
    bg1.add(core); 
    bg1.add(elective); 

    // add buttons pointPanel and bg2 
    pointPanel.add(new JLabel("Select Credit Points")); 
    pointPanel.add(cp6); 
    pointPanel.add(cp12); 
    pointPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2)); 
    bg2.add(cp6); 
    bg2.add(cp12); 

    cp6.setEnabled(false); 


    // add action listener for each of bg1 buttons so if event 
    // occurs the cp6 button will toggle between enabled and disabled. 

    elective.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 
      cp6.setEnabled(true); 
     }}); 

    core.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 
      cp6.setEnabled(false); 
     }}); 

    Object[] message = {buttPanel,pointPanel}; 

    int result = JOptionPane 
     .showOptionDialog(
     this, 
     message, 
     "...some text...", 
     JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, 
     null, new String[] { "Submit", "Cancel" }, "Default"); 

    if (result == JOptionPane.OK_OPTION) 
     { 
     // collecting all the input values in a string array to pass to 
      // another class for processing by the model... 
     } 
    else if (result == JOptionPane.NO_OPTION) 
     { 
      JOptionPane.showMessageDialog(this, 
      "You Have Elected Not to do anything At This Time", 
      "YOU HAVE COME THIS FAR AND YOU QUIT NOW....", 
      JOptionPane.QUESTION_MESSAGE);      
     } 
    } 
相关问题