取消选择 JRadioButton

发布于 2024-12-28 08:33:32 字数 609 浏览 5 评论 0原文

我正在创建的代码涉及 JRadioButton 和 JComboBox。我希望在选择 JRadioButton 时启用 JComboBox,在未选择或取消选择时禁用 JComboBox。我的问题是,如果我取消选择 JRadioButton,则 JComboBox 将不会被禁用。我该怎么做?这是我的代码

    LouisClub=new JComboBox();
    LouisClub.setEnabled(false);

    LouisClub.addItem("Writer");
    LouisClub.addItem("Photojournalist");
    LouisClub.addItem("Cartoonist");
    LouisClub.addItem("Layout Artist");

    Louis=new JRadioButton("The Louisian");

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

The code I'm creating involves a JRadioButton and a JComboBox. I want the JComboBox to be enabled if the JRadioButton is selected and disabled if it's not selected or deselected. My problem is that the JComboBox won't be disabled if I deselect the JRadioButton. How can I do this? Here's my code

    LouisClub=new JComboBox();
    LouisClub.setEnabled(false);

    LouisClub.addItem("Writer");
    LouisClub.addItem("Photojournalist");
    LouisClub.addItem("Cartoonist");
    LouisClub.addItem("Layout Artist");

    Louis=new JRadioButton("The Louisian");

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2025-01-04 08:33:32

对于此类事情,您应该使用 JCheckBox 而不是 JRadioButton,然后您需要在 actionPerformed() 方法中检查复选框状态并基于该启用/禁用组合框。另外

Louis=new JCheckBox();
Louis.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       LouisClub.setEnabled(((JCheckBox)e.getSource()).isSeleted());
    }
}

,使用 ChangeListener 而不是 ActionListener 可能会更好(不确定)。

    Louis.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent ce) {
             LouisClub.setEnabled(((JCheckBox)ce.getSource()).isSeleted());
        }
    });

You should JCheckBox instead of JRadioButton for such things and then you need check for checkBox status in actionPerformed() method and based on that enable/disable comboBox. Something like

Louis=new JCheckBox();
Louis.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
       LouisClub.setEnabled(((JCheckBox)e.getSource()).isSeleted());
    }
}

Also it might be good (Not sure) to use ChangeListener instead of ActionListener.

    Louis.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent ce) {
             LouisClub.setEnabled(((JCheckBox)ce.getSource()).isSeleted());
        }
    });
几味少女 2025-01-04 08:33:32

您应该使用 ItemListener 而不是 ActionListener。
下面是代码:

public class Toto extends JPanel {

    private JComboBox LouisClub;
    private JRadioButton Louis;

    /**
     * Create the panel.
     */
    public Toto() {
        LouisClub = new JComboBox();
        LouisClub.setEnabled(false);

        LouisClub.addItem("Writer");
        LouisClub.addItem("Photojournalist");
        LouisClub.addItem("Cartoonist");
        LouisClub.addItem("Layout Artist");

        Louis = new JRadioButton("The Louisian");

        Louis.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                boolean ok = e.getStateChange()==ItemEvent.SELECTED;
                LouisClub.setEnabled(ok);
            }
        });

        add(Louis);
        add(LouisClub);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setContentPane(new Toto());
        frame.pack();
        frame.setVisible(true);
    }
}

我还有两点意见要说:

  1. Java 开发人员通常不使用大写字母
    他们的变量的开头(他们更喜欢 louisClub 而不是 LouisClub)
  2. 我的主要方法正在工作,但不是创建
    窗口(有关详细信息,请参阅 java 教程)。

再见,

让-马克

You should use an ItemListener instead of an ActionListener.
Here is the code :

public class Toto extends JPanel {

    private JComboBox LouisClub;
    private JRadioButton Louis;

    /**
     * Create the panel.
     */
    public Toto() {
        LouisClub = new JComboBox();
        LouisClub.setEnabled(false);

        LouisClub.addItem("Writer");
        LouisClub.addItem("Photojournalist");
        LouisClub.addItem("Cartoonist");
        LouisClub.addItem("Layout Artist");

        Louis = new JRadioButton("The Louisian");

        Louis.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                boolean ok = e.getStateChange()==ItemEvent.SELECTED;
                LouisClub.setEnabled(ok);
            }
        });

        add(Louis);
        add(LouisClub);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setContentPane(new Toto());
        frame.pack();
        frame.setVisible(true);
    }
}

I have two more comments to make :

  1. Java developpers use to not use uppercase letters at the
    beginning of their variables (they prefer louisClub to LouisClub)
  2. My main method is working, but is not the best way to create a
    window (see the java tutorial for more information).

bye,

Jean-Marc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文