Java 更新JOptionPane以反映组件状态更改

Java 更新JOptionPane以反映组件状态更改,java,swing,joptionpane,Java,Swing,Joptionpane,在我的GUI项目中,有一种方法可以显示一个包含多个组件的JOptionPane,其中两个组件是ButtonGroups,每个组件中有两个JRadioButtons,在第一组中默认选择第一个按钮,在第二组中默认选择第二个按钮,在第二组中,我希望禁用第一个按钮,直到选择了第一组中的第二个按钮,即如果用户对BG1中的默认选择感到满意,则他们无法在BG2中进行选择,只有在BG1中进行第二次选择,他们才能在BG2中拥有其他选项 使用JOptionPane是否可能出现这种行为 我一直在看JDialog,JO

在我的GUI项目中,有一种方法可以显示一个包含多个组件的
JOptionPane
,其中两个组件是
ButtonGroups
,每个组件中有两个
JRadioButtons
,在第一组中默认选择第一个按钮,在第二组中默认选择第二个按钮,在第二组中,我希望禁用第一个按钮,直到选择了第一组中的第二个按钮,即如果用户对BG1中的默认选择感到满意,则他们无法在BG2中进行选择,只有在BG1中进行第二次选择,他们才能在BG2中拥有其他选项

使用
JOptionPane
是否可能出现这种行为


我一直在看
JDialog
JOptionPane
的教程,并做了其他研究,但没有一个对本例有帮助。如果有人能给我一个可能的解决方案的方向,那将是非常棒的…

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

例:

当您打开对话框时,您可以使用命令JFrame(这里您必须写入窗口名)。enable(false)

你可以让它有关闭按钮在咨询 你可以有一个复选框
当该复选框为true时。它将显示一个按钮,当您单击它时,它可能会使按钮不可见


因为JOptionPane不支持这么多组件。

可选.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);                     
        }
    }
public void displayAddSomething(数组列表项)
{
//仅显示按钮面板的精简版本
//初始化变量
JPanel buttPanel=新的JPanel(新的网格布局(0,1));
JPanel pointPanel=新的JPanel(新的GridLayout(0,1));
JRadioButton core=新的JRadioButton(“核心项:”,true);
最终JRadioButton可选=新JRadioButton(“可选项:”);
最终JRadioButton cp6=新JRadioButton(“6分”);
JRadioButton cp12=新的JRadioButton(“12点:”,正确);
ButtonGroup bg1=新建ButtonGroup();
ButtonGroup bg2=新建ButtonGroup();
//将按钮添加到buttPanel和按钮组1
添加(新JLabel(“选择课程类型”);
对接面板。添加(核心);
补充(选修);
buttPanel.setBorder(BorderFactory.createLineBorder(Color.GREEN,2));
bg1.add(核心);
bg1.add(选修);
//添加按钮点面板和bg2
pointPanel.add(新JLabel(“选择积分”);
pointPanel.add(cp6);
pointPanel.add(cp12);
setboorder(BorderFactory.createLineBorder(Color.GREEN,2));
bg2.add(cp6);
bg2.add(cp12);
cp6.setEnabled(false);
//为每个bg1按钮添加操作侦听器,以便
//cp6按钮将在启用和禁用之间切换。
可选。addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e)
{   
cp6.setEnabled(true);
}});
core.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e)
{
cp6.setEnabled(false);
}});
Object[]message={buttPanel,pointPanel};
int result=JOptionPane
.显示选项对话框(
这
消息
“…一些文本…”,
JOptionPane.OK\u取消选项,JOptionPane.INFORMATION\u消息,
空,新字符串[]{“提交”、“取消”},“默认值”);
if(result==JOptionPane.OK\u选项)
{
//收集字符串数组中要传递给的所有输入值
//模型处理的另一个类。。。
}
else if(result==JOptionPane.NO_选项)
{
JOptionPane.showMessageDialog(此,
“您已选择此时不做任何事情”,
“你已经走了这么远,现在就辞职了……”,
JOptionPane.QUESTION_消息);
}
}

“使用
作业窗格是否可能出现这种行为?”。使用与在
JFrame
中相同的方法进行操作。如果无法在一帧内完成,请发布您的最佳尝试。
JOptionPane
的消息参数采用
对象。如果您向其传递
组件
作业窗格
将使用它作为“主”视图,并在其周围添加图标和按钮。如果您要回答自己的问题,请将您的答案作为答案发布,并接受您的答案。(这样人们就不会把时间浪费在阅读你的问题而希望回答它)