Java Swing JRadioButton
Java Swing教程 - Java Swing JRadioButton
JRadioButton有两种状态:选中和未选中。当我们需要用户进行单选时使用一组JRadioButtons。
我们可以使用一个Action对象,一个字符串标签,图标和布尔标志,以指示其是否被选择默认创建JRadioButton。
创建没有标签和没有图像的JRadioButton
JRadioButton rb1 = new JRadioButton();
创建JRadioButton,文本为“我的选择"
JRadioButton rb2 = new JRadioButton("My Choice");
创建JRadioButton,文本为“我的选择"并默认选择
JRadioButton rb3 = new JRadioButton("My Choice", true);
要选择/取消选择JRadioButton,请调用setSelected()方法。
要检查是否被选中,请使用它们的isSelected()方法。
以下代码显示如何使用这些方法:
tb3.setSelected(true); // Select tb3 boolean b1 = tb3.isSelected(); // will store true in b1 tb3.setSelected(false); // Unselect tb3 boolean b2 = tb3.isSelected(); // will store false in b2
如果选择是互斥的,我们必须在按钮组中分组所有选择。ButtonGroup类的一个实例表示一个按钮组。
我们可以通过使用它的add()和remove()方法分别向按钮组添加和删除JRadioButton。
最初,按钮组的所有成员都未选中。
要形成一个按钮组,我们需要将所有互斥选择组件添加到ButtonGroup类的一个对象。 我们不会向容器添加ButtonGroup对象。
我们必须向容器中添加所有选择的组件。
下面的代码显示了一组三个相互排斥的JRadioButton。
import java.awt.BorderLayout; import java.awt.Container; /* w ww. j a va 2 s . c o m*/ import javax.swing.Box; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JRadioButton; public class Main extends JFrame { ButtonGroup genderGroup = new ButtonGroup(); JRadioButton genderMale = new JRadioButton("Male"); JRadioButton genderFemale = new JRadioButton("Female"); JRadioButton genderUnknown = new JRadioButton("Unknown"); public Main() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); genderGroup.add(genderMale); genderGroup.add(genderFemale); genderGroup.add(genderUnknown); Box b1 = Box.createVerticalBox(); b1.add(genderMale); b1.add(genderFemale); b1.add(genderUnknown); Container contentPane = this.getContentPane(); contentPane.add(b1, BorderLayout.CENTER); } public static void main(String[] args) { Main bf = new Main(); bf.pack(); bf.setVisible(true); } }
动作侦听器
下面的代码显示了如何使用ActionListener监听JRadioButton事件。
import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; // ww w .j av a 2s. co m import javax.swing.AbstractButton; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Grouping Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton = new JRadioButton("A"); JRadioButton bRadioButton = new JRadioButton("B"); ActionListener sliceActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton aButton = (AbstractButton) actionEvent.getSource(); System.out.println("Selected: " + aButton.getText()); } }; panel.add(aRadioButton); group.add(aRadioButton); panel.add(bRadioButton); group.add(bRadioButton); aRadioButton.addActionListener(sliceActionListener); bRadioButton.addActionListener(sliceActionListener); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); } }
更改侦听器
下面的代码显示了如何使用ChangeListener监听JRadioButton事件。
它告诉你当所选单选按钮被布防,按下,选择或释放时。
import java.awt.GridLayout; //w w w . ja v a 2s .c o m import javax.swing.AbstractButton; import javax.swing.ButtonGroup; import javax.swing.ButtonModel; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Grouping Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton = new JRadioButton("A"); JRadioButton bRadioButton = new JRadioButton("B"); ChangeListener changeListener = new ChangeListener() { public void stateChanged(ChangeEvent changEvent) { AbstractButton aButton = (AbstractButton)changEvent.getSource(); ButtonModel aModel = aButton.getModel(); boolean armed = aModel.isArmed(); boolean pressed = aModel.isPressed(); boolean selected = aModel.isSelected(); System.out.println("Changed: " + armed + "/" + pressed + "/" + selected); } }; panel.add(aRadioButton); group.add(aRadioButton); panel.add(bRadioButton); group.add(bRadioButton); aRadioButton.addChangeListener(changeListener); bRadioButton.addChangeListener(changeListener); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); } }
项目侦听器
下面的代码显示了如何使用ItemListener监听JRadioButton事件。
import java.awt.GridLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; // w ww . j a v a2 s . co m import javax.swing.AbstractButton; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; public class Main { public static void main(String args[]) { JFrame frame = new JFrame("Grouping Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridLayout(0, 1)); ButtonGroup group = new ButtonGroup(); JRadioButton aRadioButton = new JRadioButton("A"); JRadioButton bRadioButton = new JRadioButton("B"); ItemListener itemListener = new ItemListener() { String lastSelected; public void itemStateChanged(ItemEvent itemEvent) { AbstractButton aButton = (AbstractButton)itemEvent.getSource(); int state = itemEvent.getStateChange(); String label = aButton.getText(); String msgStart; if (state == ItemEvent.SELECTED) { if (label.equals(lastSelected)) { msgStart = "Reselected -> "; } else { msgStart = "Selected -> "; } lastSelected = label; } else { msgStart = "Deselected -> "; } System.out.println(msgStart + label); } }; panel.add(aRadioButton); group.add(aRadioButton); panel.add(bRadioButton); group.add(bRadioButton); aRadioButton.addItemListener(itemListener); bRadioButton.addItemListener(itemListener); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); } }
更多建议: