Java Swing組件單選框JRadioButton用法示例
本文實例講述了Java Swing組件單選框JRadioButton用法。分享給大家供大家參考,具體如下:
JRadioButton是Swing中的單選框。所謂單選框是指,在同一個組內雖然有多個單選框存在,然而同一時刻只能有一個單選框處于選中狀態(tài)。它就像收音機的按鈕,按下一個時此前被按下的會自動彈起,故因此得名。因此,在添加JRadioButton控件時,要記得將它們添加到同一個ButtonGroup中。
JRadioButton的常用方法如下圖所示:

可以為它添加ActionListener對象來響應事件。這里有一個問題,當多個JRadioButton共用一個事件監(jiān)聽器時,如何獲取產生事件的按鈕?
有4種方法:
1.遍歷這些按鈕并檢查是否選中,這種方法比較笨重。
2.使用事件的getActionCommand()方法,這需要事先為每個控件設置ActionCommand。
3.使用事件的getSource,并轉化為控件對象。
4.使用ButtonGroup的getSelection方法,它返回的并不是控件,而是那個控件的ButtonModel,需再次調用ButtonModel的getActionCommand方法。
使用demo如下:
JRadioButtonDemo.java
package awtDemo;
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
*/
@SuppressWarnings("serial")
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 actionCommandSizeMap = new HashMap();
// 二維數組存儲按鈕屬性,第一維是按鈕名稱,第二維是字體大小
private String[][] buttonAttributes = {
{ "Small", "Medium", "Large", "Extra" }, { "8", "12", "18", "36" } };
public JRadioButtonDemo() {
setTitle("JRadioButtonDemo - www.fzitv.net");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// 添加label
label = new JLabel("歡迎訪問腳本之家 www.fzitv.net");
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);
// 構造一個監(jiān)聽器,響應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",
sourceButton.getText());
}
// 3.通過groupSelectionActionCommand
String groupSelectionActionCommand = group.getSelection()
.getActionCommand();
System.out.printf("groupSelectionActionCommand is %s\n",
groupSelectionActionCommand);
label.setFont(new Font("Serif", Font.PLAIN,
actionCommandSizeMap.get(groupSelectionActionCommand)));
}
};
button.addActionListener(actionListener);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// 創(chuàng)建窗體并指定標題
JRadioButtonDemo frame = new JRadioButtonDemo();
// 關閉窗體后退出程序
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 自動適配所有控件大小
// frame.pack();
// 設置窗體位置在屏幕中央
frame.setLocationRelativeTo(null);
// 顯示窗體
frame.setVisible(true);
}
}
運行效果如下:

更多關于java相關內容感興趣的讀者可查看本站專題:《Java數據結構與算法教程》、《Java字符與字符串操作技巧總結》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
- Java Swing組件下拉菜單控件JComboBox用法示例
- Java Swing組件編程之JTable表格用法實例詳解
- Java Swing組件JFileChooser用法實例分析
- Java Swing組件布局管理器之FlowLayout(流式布局)入門教程
- Java Swing組件復選框JCheckBox用法示例
- java Swing組件setBounds()簡單用法實例分析
- java實現的計算器功能示例【基于swing組件】
- Java實現的可選擇及拖拽圖片的面板功能【基于swing組件】
- Java編程使用箱式布局管理器示例【基于swing組件】
- Java Swing組件定制CheckBox示例
- Java Swing組件定制RadioButton示例
相關文章
Spring事務Transaction配置的五種注入方式詳解
這篇文章主要介紹了Spring事務Transaction配置的五種注入方式詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04

