• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

java 搞代码 4年前 (2022-01-05) 32次浏览 已收录 0个评论

这篇文章主要介绍了Java Swing组件单选框JRadioButton用法,结合具体实例形式分析了Swing单选框JRadioButton的使用方法及相关操作注意事项,需要的朋友可以参考下

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

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

来源gaodai.ma#com搞#代!码网

oButton控件时,要记得将它们添加到同一个ButtonGroup中。

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

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

有4种方法:

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

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

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

4.使用ButtonGroupgetSelection方法,它返回的并不是控件,而是那个控件的ButtonModel,需再次调用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核心技术 卷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.gaodaima.com"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // 添加label label = new JLabel("欢迎访问脚本之家 www.gaodaima.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相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java字符与字符串操作技巧总结》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

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

以上就是Java Swing组件单选框JRadioButton用法示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java Swing组件单选框JRadioButton用法示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址