【问题标题】:Java: Can Radio Buttons of the same Button Group be in different Panels?Java:同一个按钮组的单选按钮可以在不同的面板中吗?
【发布时间】:2015-01-25 13:54:36
【问题描述】:

我已经将单选按钮添加到按钮组,但问题是可以同时选择两个单选按钮!我不知道该怎么办。请帮忙。 这是我当前代码的样子:

JRadioButton rEncrypt, rDecrypt;

ButtonGroup bgroup = new ButtonGroup();
bgroup.add(rEncrypt);   bgroup.add(rDecrypt);


    rEncrypt = new JRadioButton("Encryption");
    rEncrypt.setBackground(bgColor);
    rEncrypt.setSelected(true);
    pEncrypt = new JPanel();
    pEncrypt.setBackground(bgColor);
    pEncrypt.add(rEncrypt);


    rDecrypt = new JRadioButton("Decryption");
    rDecrypt.setBackground(bgColor);
    pDecrypt = new JPanel();
    pDecrypt.setBackground(bgColor);
    pDecrypt.add(rDecrypt);

【问题讨论】:

  • 我不太了解 swing,但我很确定它不支持每个组件超过 1 个父级。如果你想让不同位置的按钮表现得像一个组,请手动执行。

标签: java swing radio-button buttongroup


【解决方案1】:

没有看到一个完全可运行的例子......

这样做...

JRadioButton rEncrypt, rDecrypt;

ButtonGroup bgroup = new ButtonGroup();
bgroup.add(rEncrypt);   bgroup.add(rDecrypt);

在此之前...

rEncrypt = new JRadioButton("Encryption");
//...
rDecrypt = new JRadioButton("Decryption");

错了……

您需要先创建按钮,然后再将它们添加到ButtonGroup...

【讨论】:

    猜你喜欢
    • 2011-08-09
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2017-07-24
    • 2013-05-29
    • 1970-01-01
    相关资源
    最近更新 更多