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

可以為它添加ActionListener對(duì)象來(lái)響應(yīng)事件。這里有一個(gè)問(wèn)題,當(dāng)多個(gè)JRadioButton共用一個(gè)事件監(jiān)聽器時(shí),如何獲取產(chǎn)生事件的按鈕?
有4種方法:
1.遍歷這些按鈕并檢查是否選中,這種方法比較笨重。
2.使用事件的getActionCommand()方法,這需要事先為每個(gè)控件設(shè)置ActionCommand。
3.使用事件的getSource,并轉(zhuǎn)化為控件對(duì)象。
4.使用ButtonGroup的getSelection方法,它返回的并不是控件,而是那個(gè)控件的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ǔ)知識(shí)》 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ù)組存儲(chǔ)按鈕屬性,第一維是按鈕名稱,第二維是字體大小
private String[][] buttonAttributes = {
{ "Small", "Medium", "Large", "Extra" }, { "8", "12", "18", "36" } };
public JRadioButtonDemo() {
setTitle("JRadioButtonDemo - m.fzitv.net");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// 添加label
label = new JLabel("歡迎訪問(wèn)腳本之家 m.fzitv.net");
label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE));
add(label, BorderLayout.CENTER);
// 添加buttonPanel,它包含4個(gè)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]));
// 將按鈕名稱和字體大小添加為對(duì)應(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)造一個(gè)監(jiān)聽器,響應(yīng)checkBox事件
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 1.通過(guò)eActionCommand
String eActionCommand = e.getActionCommand();
System.out.printf("e.getActionCommand() is %s\n",
eActionCommand);
// 2.通過(guò)getSource()
Object sourceObject = e.getSource();
if (sourceObject instanceof JRadioButton) {
JRadioButton sourceButton = (JRadioButton) sourceObject;
System.out.printf("selected JRadioButton is %s\n",
sourceButton.getText());
}
// 3.通過(guò)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);
// 自動(dòng)適配所有控件大小
// frame.pack();
// 設(shè)置窗體位置在屏幕中央
frame.setLocationRelativeTo(null);
// 顯示窗體
frame.setVisible(true);
}
}
運(yùn)行效果如下:

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

