Java Swing组件单选框JRadioButton用法示例

2025-05-29 0 51

本文实例讲述了java swing组件单选框jradiobutton用法。分享给大家供大家参考,具体如下:

jradiobutton是swing中的单选框。所谓单选框是指,在同一个组内虽然有多个单选框存在,然而同一时刻只能有一个单选框处于选中状态。它就像收音机的按钮,按下一个时此前被按下的会自动弹起,故因此得名。因此,在添加jradiobutton控件时,要记得将它们添加到同一个buttongroup中。

jradiobutton的常用方法如下图所示:

Java Swing组件单选框JRadioButton用法示例

可以为它添加actionlistener对象来响应事件。这里有一个问题,当多个jradiobutton共用一个事件监听器时,如何获取产生事件的按钮?

有4种方法:

1.遍历这些按钮并检查是否选中,这种方法比较笨重。

2.使用事件的getactioncommand()方法,这需要事先为每个控件设置actioncommand

3.使用事件的getsource,并转化为控件对象。

4.使用buttongroupgetselection方法,它返回的并不是控件,而是那个控件的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.zzvips.com");

setsize(default_width, default_height);

// 添加label

label = new jlabel("欢迎访问快网idc www.zzvips.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]));

// 将按钮名称和字体大小添加为对应表,名称等同于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 Swing组件单选框JRadioButton用法示例

希望本文所述对大家java程序设计有所帮助。

原文链接:http://www.cnblogs.com/pzy4447/p/4641101.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java Swing组件单选框JRadioButton用法示例 https://www.kuaiidc.com/114040.html

相关文章

发表评论
暂无评论