如何在 Java 中使用 Button Group Swing 控件?

发布于 2024-08-21 21:31:56 字数 65 浏览 2 评论 0原文

如何使用 NetBeans 将单选按钮添加到按钮组?

添加它们后,如何从按钮组中选择选定的单选按钮?

How do I add radio buttons to a button group using NetBeans?

Once I add them, how do I get selected radio button from the button group?

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

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

发布评论

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

评论(6

蓝咒 2024-08-28 21:31:56
  1. 从面板中拖动一个 ButtonGroup 并将其放到 GUI 上。
    它将显示在检查器面板中的其他组件下。
  2. 右键单击它并更改变量名称为有意义的名称。
  3. 现在在 GUI 中选择一个单选按钮。
  4. 属性面板中查找buttonGroup属性。
  5. 单击旁边的组合框并选择您的按钮组。
  1. Drag a ButtonGroup from the palette and drop it on your GUI.
    It will show up under Other Components in the Inspector panel.
  2. Right-click on it and Change variable name to something meaningful.
  3. Now select a radio button in your GUI.
  4. In the Properties panel look for the buttonGroup property.
  5. Click the combo box next to it and select your button group.
臻嫒无言 2024-08-28 21:31:56

我强烈建议阅读这篇优秀的教程。以下是文章中的代码摘录,它满足了您有关如何创建按钮并将按钮添加到 ButtonGroup 的问题:

JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setSelected(true);

JRadioButton catButton = new JRadioButton(catString);

   //Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);

至于获取选择的项目,您基本上需要 迭代调用 isSelected 的组中的项目。

I highly recommend reading this excellent tutorial. Here's an excerpt of code from the article that satisfies your question on how to create and add buttons to a ButtonGroup:

JRadioButton birdButton = new JRadioButton(birdString);
birdButton.setSelected(true);

JRadioButton catButton = new JRadioButton(catString);

   //Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(birdButton);
group.add(catButton);

As far as getting which item is selected, you basically need to iterate through the items in the group calling isSelected.

陌路终见情 2024-08-28 21:31:56

要以编程方式选择单选按钮,请尝试以下操作:

private final ButtonGroup buttonGroup = new ButtonGroup();

JRadioButton btn01 = new JRadioButton("btn 1");
buttonGroup.add(btn01);
JRadioButton btn02 = new JRadioButton("btn 2");
buttonGroup.add(btn02);
JRadioButton btn03 = new JRadioButton("btn 3");
buttonGroup.add(btn03);
// gets the selected radio button
if(buttonGroup.getSelection().equals(btn01.getModel())) {
 // code
}

// similarly for the other radio buttons as well.

To select a radio button programmatically, try these:

private final ButtonGroup buttonGroup = new ButtonGroup();

JRadioButton btn01 = new JRadioButton("btn 1");
buttonGroup.add(btn01);
JRadioButton btn02 = new JRadioButton("btn 2");
buttonGroup.add(btn02);
JRadioButton btn03 = new JRadioButton("btn 3");
buttonGroup.add(btn03);
// gets the selected radio button
if(buttonGroup.getSelection().equals(btn01.getModel())) {
 // code
}

// similarly for the other radio buttons as well.
胡大本事 2024-08-28 21:31:56

如何使用按钮、复选框和单选按钮

ButtonGroup group = new ButtonGroup();
group.add(new JRadioButton("one"));
group.add(new JRadioButton("two"));
//TO FIND SELECTED
//use a loop on group.getElements();
//and check isSelected() and add them
//to some sort of data structure

How to Use Buttons, Check Boxes, and Radio Buttons

ButtonGroup group = new ButtonGroup();
group.add(new JRadioButton("one"));
group.add(new JRadioButton("two"));
//TO FIND SELECTED
//use a loop on group.getElements();
//and check isSelected() and add them
//to some sort of data structure
随波逐流 2024-08-28 21:31:56
private final ButtonGroup agreeDisagree = new ButtonGroup();

    JToggleButton tglbtnAgree = new JToggleButton("Agree");
    tglbtnAgree.setSelected(true);
    tglbtnAgree.setBounds(227, 127, 75, 23);
    agreeDisagree.add(tglbtnAgree);
    contentPane.add(tglbtnAgree);

    JToggleButton tglbtnDisagree = newJToggleButton("Disagree");
    tglbtnDisagree.setBounds(307, 127, 75, 23);
    agreeDisagree.add(tglbtnDisagree);
    contentPane.add(tglbtnDisagree);
private final ButtonGroup agreeDisagree = new ButtonGroup();

    JToggleButton tglbtnAgree = new JToggleButton("Agree");
    tglbtnAgree.setSelected(true);
    tglbtnAgree.setBounds(227, 127, 75, 23);
    agreeDisagree.add(tglbtnAgree);
    contentPane.add(tglbtnAgree);

    JToggleButton tglbtnDisagree = newJToggleButton("Disagree");
    tglbtnDisagree.setBounds(307, 127, 75, 23);
    agreeDisagree.add(tglbtnDisagree);
    contentPane.add(tglbtnDisagree);
活雷疯 2024-08-28 21:31:56

在导航器窗格中的“其他组件”下,选择您的按钮组。然后在“属性”窗格中选择“代码”选项卡。选择省略号 (...) 以编辑“After-All-Set Code”部分。输入代码以将按钮添加到按钮组,如上所述。

例如:

attemptGroup.add(attemptRadio1);
tryGroup.add(attemptRadio2);
attemptsGroup.add(attemptRadio3);

In your Navigator Pane, under "Other Components", select your button group. Then select the Code tab in the Properties pane. Select the ellipses (...) to edit the section "After-All-Set Code". Enter your code to add buttons to the button group as previously explained above.

For example:

attemptGroup.add(attemptRadio1);
attemptGroup.add(attemptRadio2);
attemptGroup.add(attemptRadio3);

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