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",
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
// 創建窗體並指定標題
JRadioButtonDemo frame = new JRadioButtonDemo();
// 關閉窗體后退出程序
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 自動適配所有控件大小
// frame.pack();
// 設置窗體位置在屏幕中央
frame.setLocationRelativeTo(null);
// 顯示窗體
frame.setVisible(true);
}

 
         

}

 

 運行效果如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM