Swing-JRadioButton⽤法-⼊门
JRadioButton是Swing中的单选框。所谓单选框是指,在同⼀个组内虽然有多个单选框存在,然⽽同⼀时刻只能有⼀个单选框处于选中状态。它就像收⾳机的按钮,按下⼀个时此前被按下的会⾃动弹起,故因此得名。因此,在添加JRadioButton控件时,要记得将它们添加到同⼀个ButtonGroup中。
JRadioButton的常⽤⽅法如下图所⽰:
可以为它添加ActionListener对象来响应事件。这⾥有⼀个问题,当多个JRadioButton共⽤⼀个事件时,如何获取产⽣事件的按钮?有4种⽅法:
1.遍历这些按钮并检查是否选中这种⽅法⽐较笨重。
2.使⽤事件的getActionCommand()⽅法,这需要事先为每个控件设置ActionCommand。
3.使⽤事件的getSource,并转化为控件对象。
4.使⽤ButtonGroup的getSelection⽅法,它返回的并不是控件,⽽是那个控件的ButtonModel,需再次调⽤ButtonModel的getActionCommand⽅法。
使⽤demo如下:
JRadioButtonDemo.java
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
/*
* source code from 《java核⼼技术卷1 基础知识》 P329
*/
public class JRadioButtonDemo extends JFrame {
int DEFAULT_WIDTH = 600;
int DEFAULT_HEIGHT = 400;
private JLabel label;
private JPanel buttonPanel;
private ButtonGroup group;
private static final int DEFAULT_SIZE = 12;
private Map<String, Integer> actionCommandSizeMap = new HashMap<String, Integer>(); // ⼆维数组存储按钮属性,第⼀维是按钮名称,第⼆维是字体⼤⼩
private String[][] buttonAttributes = {
{ "Small", "Medium", "Large", "Extra" }, { "8", "12", "18", "36" } };
public JRadioButtonDemo() {
setTitle("JRadioButtonDemo");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// 添加label
label = new JLabel("The quick brown fox jumps over the lazy dog.");
label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE));
add(label, BorderLayout.CENTER);
// 添加buttonPanel,它包含4个radioButton
buttonPanel = new JPanel();
group = new ButtonGroup();
add(buttonPanel, BorderLayout.SOUTH);
// 添加radioButton
for (int i = 0; i < buttonAttributes[0].length; i++) {
addRadioButton(buttonAttributes[0][i],
Integer.parseInt(buttonAttributes[1][i]));
// 将按钮名称和字体⼤⼩添加为对应表,名称等同于actionCommand actionCommandSizeMap.put(buttonAttributes[0][i],
Integer.parseInt(buttonAttributes[1][i]));
}
}
public void addRadioButton(String name, final int size) {
boolean selected = size == DEFAULT_SIZE;
JRadioButton button = new JRadioButton(name, selected);
button.setActionCommand(name);// 设置name即为actionCommand
group.add(button);
buttonPanel.add(button);
// 构造⼀个,响应checkBox事件
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 1.通过eActionCommand
String eActionCommand = e.getActionCommand();
System.out.printf("e.getActionCommand() is %s\n",
eActionCommand);
// 2.通过getSource()
Object sourceObject = e.getSource();
if (sourceObject instanceof JRadioButton) {
JRadioButton sourceButton = (JRadioButton) sourceObject;
System.out.printf("selected JRadioButton is %s\n",
}
// 3.通过groupSelectionActionCommand
String groupSelectionActionCommand = Selection()
.getActionCommand();
System.out.printf("groupSelectionActionCommand is %s\n", groupSelectionActionCommand);
label.setFont(new Font("Serif", Font.PLAIN, (groupSelectionActionCommand))); }
};
button.addActionListener(actionListener);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// 创建窗体并指定标题
JRadioButtonDemo frame = new JRadioButtonDemo();
// 关闭窗体后退出程序
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ⾃动适配所有控件⼤⼩
// frame.pack();
checkbox和radiobutton的区别// 设置窗体位置在屏幕中央
frame.setLocationRelativeTo(null);
// 显⽰窗体
frame.setVisible(true);
}
}
运⾏效果如下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。