本文實例講述了java 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
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.ythuaji.com.cn" ); setsize(default_width, default_height); // 添加label label = new jlabel( "歡迎訪問服務器之家 www.ythuaji.com.cn" ); 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 ); } } |
運行效果如下:
希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://www.cnblogs.com/pzy4447/p/4641101.html