您的位置:首页 > 其它

Swing-JRadioButton用法-入门

2015-07-12 16:13 309 查看
JRadioButton是Swing中的单选框。所谓单选框是指,在同一个组内虽然有多个单选框存在,然而同一时刻只能有一个单选框处于选中状态。它就像收音机的按钮,按下一个时此前被按下的会自动弹起,故因此得名。因此,在添加JRadioButton控件时,要记得将它们添加到同一个ButtonGroup中。

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



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

有4种方法:

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

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

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

4.使用ButtonGroup的getSelection方法,它返回的并不是控件,而是那个控件的ButtonModel,需再次调用ButtonModel的getActionCommand方法。

使用demo如下:

JRadioButtonDemo.java


importjava.awt.BorderLayout;
importjava.awt.Font;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.HashMap;
importjava.util.Map;


importjavax.swing.ButtonGroup;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JRadioButton;


/*
*sourcecodefrom《java核心技术卷1基础知识》P329
*/


publicclassJRadioButtonDemoextendsJFrame{


intDEFAULT_WIDTH=600;
intDEFAULT_HEIGHT=400;
privateJLabellabel;
privateJPanelbuttonPanel;
privateButtonGroupgroup;
privatestaticfinalintDEFAULT_SIZE=12;
privateMap<String,Integer>actionCommandSizeMap=newHashMap<String,Integer>();


//二维数组存储按钮属性,第一维是按钮名称,第二维是字体大小
privateString[][]buttonAttributes={
{"Small","Medium","Large","Extra"},{"8","12","18","36"}};


publicJRadioButtonDemo(){
setTitle("JRadioButtonDemo");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);


//添加label
label=newJLabel("Thequickbrownfoxjumpsoverthelazydog.");
label.setFont(newFont("Serif",Font.PLAIN,DEFAULT_SIZE));
add(label,BorderLayout.CENTER);


//添加buttonPanel,它包含4个radioButton
buttonPanel=newJPanel();
group=newButtonGroup();
add(buttonPanel,BorderLayout.SOUTH);


//添加radioButton
for(inti=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]));
}
}


publicvoidaddRadioButton(Stringname,finalintsize){
booleanselected=size==DEFAULT_SIZE;
JRadioButtonbutton=newJRadioButton(name,selected);
button.setActionCommand(name);//设置name即为actionCommand
group.add(button);
buttonPanel.add(button);


//构造一个监听器,响应checkBox事件
ActionListeneractionListener=newActionListener(){
publicvoidactionPerformed(ActionEvente){
//1.通过eActionCommand
StringeActionCommand=e.getActionCommand();
System.out.printf("e.getActionCommand()is%s\n",
eActionCommand);


//2.通过getSource()
ObjectsourceObject=e.getSource();
if(sourceObjectinstanceofJRadioButton){
JRadioButtonsourceButton=(JRadioButton)sourceObject;
System.out.printf("selectedJRadioButtonis%s\n",
sourceButton.getText());
}


//3.通过groupSelectionActionCommand
StringgroupSelectionActionCommand=group.getSelection()
.getActionCommand();
System.out.printf("groupSelectionActionCommandis%s\n",
groupSelectionActionCommand);


label.setFont(newFont("Serif",Font.PLAIN,
actionCommandSizeMap.get(groupSelectionActionCommand)));
}
};


button.addActionListener(actionListener);
}


publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
//创建窗体并指定标题
JRadioButtonDemoframe=newJRadioButtonDemo();
//关闭窗体后退出程序
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//自动适配所有控件大小
//frame.pack();
//设置窗体位置在屏幕中央
frame.setLocationRelativeTo(null);
//显示窗体
frame.setVisible(true);
}


}



运行效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: