How to use the button group swing control in Java?

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

Once they are added, how do I select radio buttons from the button group?

Solution

I strongly recommend reading this excellent tutorial This is a code excerpt from an article that meets your question of how to create and add buttons to 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 for which item to select, you basically need to iterate through the items in the group calling isselected

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>