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

可以為它添加ActionListener對象來響應(yīng)事件。這里有一個問題,當(dāng)多個JRadioButton共用一個事件監(jiān)聽器時,如何獲取產(chǎn)生事件的按鈕?
有4種方法:
1.遍歷這些按鈕并檢查是否選中,這種方法比較笨重。
2.使用事件的getActionCommand()方法,這需要事先為每個控件設(shè)置ActionCommand。
3.使用事件的getSource,并轉(zhuǎn)化為控件對象。
4.使用ButtonGroup的getSelection方法,它返回的并不是控件,而是那個控件的ButtonModel,需再次調(diào)用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核心技術(shù) 卷1 基礎(chǔ)知識》 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();
// 二維數(shù)組存儲按鈕屬性,第一維是按鈕名稱,第二維是字體大小
private String[][] buttonAttributes = {
{ "Small", "Medium", "Large", "Extra" }, { "8", "12", "18", "36" } };
public JRadioButtonDemo() {
setTitle("JRadioButtonDemo - www.dhdzp.com");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// 添加label
label = new JLabel("歡迎訪問腳本之家 www.dhdzp.com");
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]));
// 將按鈕名稱和字體大小添加為對應(yīng)表,名稱等同于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);// 設(shè)置name即為actionCommand
group.add(button);
buttonPanel.add(button);
// 構(gòu)造一個監(jiān)聽器,響應(yīng)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)建窗體并指定標(biāo)題
JRadioButtonDemo frame = new JRadioButtonDemo();
// 關(guān)閉窗體后退出程序
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 自動適配所有控件大小
// frame.pack();
// 設(shè)置窗體位置在屏幕中央
frame.setLocationRelativeTo(null);
// 顯示窗體
frame.setVisible(true);
}
}
運行效果如下:

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
- Java Swing組件下拉菜單控件JComboBox用法示例
- Java Swing組件編程之JTable表格用法實例詳解
- Java Swing組件JFileChooser用法實例分析
- Java Swing組件布局管理器之FlowLayout(流式布局)入門教程
- Java Swing組件復(fù)選框JCheckBox用法示例
- java Swing組件setBounds()簡單用法實例分析
- java實現(xiàn)的計算器功能示例【基于swing組件】
- Java實現(xiàn)的可選擇及拖拽圖片的面板功能【基于swing組件】
- Java編程使用箱式布局管理器示例【基于swing組件】
- Java Swing組件定制CheckBox示例
- Java Swing組件定制RadioButton示例
相關(guān)文章
Java使用ArrayList實現(xiàn)撲克牌的示例代碼
學(xué)習(xí)了關(guān)于集合類的知識,我們可以做一個小項目來加深對集合類知識的學(xué)習(xí)!本文就來利用ArrayList實現(xiàn)撲克牌發(fā)牌洗牌效果,需要的可以參考一下2022-10-10
基于SpringBoot項目遇到的坑--Date入?yún)栴}
這篇文章主要介紹了SpringBoot項目遇到的坑--Date入?yún)栴},具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
Java連接MYSQL數(shù)據(jù)庫的實現(xiàn)步驟
以下的文章主要描述的是java連接MYSQL數(shù)據(jù)庫的正確操作步驟,在此篇文章里我們主要是以實例列舉的方式來引出其具體介紹2013-06-06
Spring事務(wù)Transaction配置的五種注入方式詳解
這篇文章主要介紹了Spring事務(wù)Transaction配置的五種注入方式詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04

