Documentation

The Java™ Tutorials
Hide TOC
How to Use the ButtonGroup Component如何使用ButtonGroup组件
Trail: Creating a GUI With Swing
Lesson: Using Swing Components
Section: How to Use Various Components

How to Use the ButtonGroup Component如何使用按钮组组件

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实例管理的所有按钮都未选中。

How to Use ButtonGroup Features如何使用按钮组功能

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. 通常,按钮组包含JRadioButtonJRadioButtonMenuItemJToggleButton的实例。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.JButtonJMenuItem的实例放在按钮组中是没有意义的,因为JButtonJMenuItem不实现选择/取消选择按钮状态。

In general, you will typically follow these steps to write code that uses a ButtonGroup component.通常,您将按照以下步骤编写使用ButtongGroup组件的代码。

  1. Subclass 子类JFrame
  2. Call ContextPane together with a layout manager与布局管理器一起调用ContextPane
  3. Declare and configure a set of radio buttons or toggle buttons声明和配置一组单选按钮或切换按钮
  4. Instantiate a ButtonGroup object实例化ButtongGroup对象
  5. Call the 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中。

The ButtonGroup API按钮组API

 

Commonly Used Button Group Constructors/Methods常用按钮组构造函数/方法
Constructor or Method构造函数或方法 Purpose目的
ButtonGroup() Create a ButtonGroup instance.创建ButtongGroup实例。
void add(AbstractButton)
void remove(AbstractButton)
Add a button to the group, or remove a button from the group.向组中添加按钮,或从组中删除按钮。
public ButtonGroup getGroup()
(in DefaultButtonModel)
Get the ButtonGroup, if any, that controls a button. 获取控制按钮的ButtonGroup(如果有)。For example:例如:
ButtonGroup group = ((DefaultButtonModel)button.getModel()).getGroup();
public ButtonGroup clearSelection() Clears the state of selected buttons in the ButtonGroup. 清除按钮组中选定按钮的状态。None of the buttons in the ButtonGroup are selected .未选择按钮组中的任何按钮。

ButtonGroup Examples按钮组示例

The following demonstration application uses the ButtonGroup component to group radio buttons displaying on a Window.下面的演示应用程序使用ButtongGroup组件对窗口上显示的单选按钮进行分组。

Example示例 Where Described描述位置 Notes备注
RadioButtonDemo How to Use Radio Buttons如何使用单选按钮 Uses radio buttons to determine which of five images it should display.使用单选按钮确定应显示五个图像中的哪一个。

Previous page: How to Use Buttons, Check Boxes, and Radio Buttons
Next page: How to Use Color Choosers