【问题标题】:Multiple radio buttons getting selected多个单选按钮被选中
【发布时间】:2013-07-27 20:19:36
【问题描述】:

我有两个用 java 制作的单选按钮。问题是它们没有链接在一起,即它们都可以同时被选中。我如何实现它们之间的链接?

【问题讨论】:

  • 您需要阅读有关单选按钮组的信息。顺便说一句,您使用的是什么 ide?​​span>
  • 嘿,想知道下面的答案是否对您有所帮助?

标签: java swing radio-button jradiobutton


【解决方案1】:

设置相同的name属性 Radio example

<input id="hello" type="radio" name="greet">
<label for="hello">hello</label>
<input id="hi" type="radio" name="greet">
<label for="hi">hi</label>
<hr />
<input id="all" type="radio" name="who">
<label for="all">world!</label>
<input id="one" type="radio" name="who">
<label for="one">you</label>

【讨论】:

【解决方案2】:

我认为你需要这个;

        //Create three radio buttons   
        JRadioButton aButton = new JRadioButton("A",true);
        JRadioButton bButton = new JRadioButton("B");
        JRadioButton cButton = new JRadioButton("C");

        //Create a ButtonGroup object, add buttons to the group
        ButtonGroup myButtonGroup = new ButtonGroup();
        myButtonGroup.add(aButton);
        myButtonGroup.add(bButton);
        myButtonGroup.add(cButton);

        //Display radio buttons
        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(aButton);
        getContentPane().add(bButton);
        getContentPane().add(cButton);
        setSize(250,100);
        setTitle("Swing Radio Buttons");
        setVisible(true);

如果有帮助,请告诉我。

【讨论】:

  • 当然,很高兴它有帮助。如果对您有帮助,如果您能接受答案,我将不胜感激。发送
猜你喜欢
  • 2023-02-03
  • 1970-01-01
  • 2019-10-04
  • 2016-06-09
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
相关资源
最近更新 更多