ButtonGroup
组件The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The ButtonGroup
component manages the selected/unselected state for a set of buttons. ButtonGroup
组件管理一组按钮的选定/未选定状态。For the group, the 对于组,ButtonGroup
instance guarantees that only one button can be selected at a time.ButtonGroup
实例保证一次只能选择一个按钮。
Initially, all buttons managed by a 最初,ButtonGroup
instance are unselected.ButtonGroup
实例管理的所有按钮都未选中。
You can use 您可以将ButtonGroup
with any set of objects that inherit from AbstractButton
. ButtongGroup
与从AbstractButton
继承的任何对象集一起使用。Typically a button group contains instances of 通常,按钮组包含JRadioButton, JRadioButtonMenuItem
, or JToggleButton
. JRadioButton
、JRadioButtonMenuItem
或JToggleButton
的实例。It would not make sense to put an instance of 将JButton
or JMenuItem
in a button group because JButton
and JMenuItem
do not implement the select/deselect button state.JButton
或JMenuItem
的实例放在按钮组中是没有意义的,因为JButton
和JMenuItem
不实现选择/取消选择按钮状态。
In general, you will typically follow these steps to write code that uses a 通常,您将按照以下步骤编写使用ButtonGroup
component.ButtongGroup
组件的代码。
JFrame
ContextPane
together with a layout managerContextPane
ButtonGroup
objectButtongGroup
对象add
method on that buttongroup object in order to add each button to the group.buttongroup
对象上的add
方法,以便将每个按钮添加到组中。For details and a code example, see How to Use Radio Buttons. 有关详细信息和代码示例,请参阅如何使用单选按钮。It shows how to use a 它展示了如何使用ButtonGroup
component to group a set of RadioButtons set into a JPanel.ButtongGroup
组件将一组RadioButtons
集合分组到JPanel
中。
ButtonGroup()
|
ButtonGroup instance.ButtongGroup 实例。 |
void add(AbstractButton)
void remove(AbstractButton) |
|
public ButtonGroup getGroup() (in DefaultButtonModel ) |
ButtonGroup , if any, that controls a button. ButtonGroup (如果有)。ButtonGroup group = ((DefaultButtonModel)button.getModel()).getGroup(); |
public ButtonGroup clearSelection() |
The following demonstration application uses the ButtonGroup component to group radio buttons displaying on a Window.下面的演示应用程序使用ButtongGroup组件对窗口上显示的单选按钮进行分组。
RadioButtonDemo |