【问题标题】:Updating a JOptionPane to reflect a component state change更新 JOptionPane 以反映组件状态更改
【发布时间】:2013-01-02 22:25:36
【问题描述】:

在我的 GUI 项目中有一个方法用于显示带有多个组件的 JOptionPane,其中 2 个组件是 ButtonGroups,每个组件有 2 个 JRadioButtons,在第一组中默认选择第一个按钮,在第二组中,默认选择第二个按钮,在第二组中,我希望禁用第一个按钮,直到选择第一组中的第二个按钮,即如果用户对 BG1 中的默认选择感到满意,那么他们可以不要在BG2中进行选择,只有在BG1中进行第二次选择才能在BG2中进行其他选择。

JOptionPane 可以实现这种行为吗?

一直在查看JDialogJOptionPane 的教程并进行其他研究,但这些都证明在这种情况下没有帮助。如果有人能给我一些指导,找到一个非常棒的可能解决方案......

【问题讨论】:

  • JOptionPane 可以实现这种类型的行为吗?” 当然。就像在JFrame 中那样做。如果您无法在框架中实现它,请发布您的最佳尝试SSCCE
  • JOptionPane 的消息参数采用Object。如果你传递一个ComponentJOptionPane 将把它用作“主”视图,在它周围添加图标和按钮
  • 如果您要回答自己的问题,请将您的答案作为答案发布并接受您的答案。 (这样人们就不会浪费时间阅读您的问题以期回答它)

标签: java swing joptionpane


【解决方案1】:

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);                     
        }
    }

【讨论】:

    【解决方案2】:

    JDialog 是最好的选择

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

    【讨论】:

      【解决方案3】:

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

      例子:

      当你打开对话框时,你可以使用命令 JFrame(这里你必须写你的窗口名称).enable(false)

      你可以让它在柜台上有关闭按钮 你可以有一个复选框 当复选框为真时。它会显示一个按钮,当你点击它可以使按钮不可见

      【讨论】:

        猜你喜欢
        • 2017-01-14
        • 2021-11-16
        • 2021-10-08
        • 2021-07-07
        • 2016-06-25
        • 2017-05-05
        • 1970-01-01
        • 2018-11-03
        • 2019-11-26
        相关资源
        最近更新 更多