JavaSwing_2.3: JRadioButton(单选按钮)
来源:互联网 发布:托福听力软件推荐 编辑:程序博客网 时间:2024/05/16 12:19
本文链接: http://blog.csdn.net/xietansheng/article/details/74363379
Java Swing 图形界面开发(目录)
1. 概述
官方JavaDocsApi: javax.swing.JRadioButton
JRadioButton
,单选按钮。
JButton 常用构造方法:
// 无文本,未选中JRadioButton()// 有文本,未选中JRadioButton(String text)// 有文本,并指定是否选中JRadioButton(String text, boolean selected)
JRadioButton 常用方法:
// 设置单选按钮的 文本、字体 和 字体颜色void setText(String text)void setFont(Font font)void setForeground(Color fg)/* 以下方法定义在 javax.swing.AbstractButton 基类中 */// 设置单选按钮是否选中状态void setSelected(boolean b)// 判断单选按钮是否选中boolean isSelected()// 设置单选按钮是否可用void setEnabled(boolean enable)// 设置单选按钮在 默认、被选中、不可用 时显示的图片void setIcon(Icon defaultIcon)void setPressedIcon(Icon pressedIcon)void setDisabledIcon(Icon disabledIcon)// 设置图片和文本的间距void setIconTextGap(int iconTextGap)
JRadioButton 常用监听器:
// 添加状态改变监听器void addChangeListener(ChangeListener l)
ButtonGroup(按钮组): 当有多个单选按钮时,一般只允许一个单选按钮选中,因此需要对同一类型的单选按钮进行分组,如下:
// 创建一个按钮组ButtonGroup btnGroup = new ButtonGroup();// 添加单选按钮到按钮组btnGroup.add(radioBtn01);btnGroup.add(radioBtn02);
2. 代码实例
package com.xiets.swing;import javax.swing.*;import java.awt.*;public class Main { public static void main(String[] args) throws AWTException { JFrame jf = new JFrame("测试窗口"); jf.setSize(200, 200); jf.setLocationRelativeTo(null); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JPanel panel = new JPanel(); // 创建两个单选按钮 JRadioButton radioBtn01 = new JRadioButton("男"); JRadioButton radioBtn02 = new JRadioButton("女"); // 创建按钮组,把两个单选按钮添加到该组 ButtonGroup btnGroup = new ButtonGroup(); btnGroup.add(radioBtn01); btnGroup.add(radioBtn02); // 设置第一个单选按钮选中 radioBtn01.setSelected(true); panel.add(radioBtn01); panel.add(radioBtn02); jf.setContentPane(panel); jf.setVisible(true); }}
结果展示:
阅读全文
1 0
- JavaSwing_2.3: JRadioButton(单选按钮)
- JRadioButton(单选按钮)添加事件监听
- JRadioButton单选按钮代码1
- JRadioButton单选按钮代码2
- JRadioButton(单选按钮)组件使用
- JavaSwing_2.2: JButton(按钮)
- 8、单选按钮(JRadioButton)和复选框(JCheckBox)
- swing 学习笔记四(单选按钮JRadioButton )
- Java Swing界面编程(26)---单选按钮:JRadioButton
- Java中JRadioButton单选按钮分组方法
- Java单选按钮JRadioButton取消选择状态的方法
- 单选组件JRadioButton
- JavaSwing_2.5: JToggleButton(开关按钮)
- JRadioButton如何变成单选
- Java Swing JRadioButton 给radioButton加图片不隐藏单选按钮
- Java 下拉框JComboBox和单选按钮JRadioButton简单举例使用
- Java复选框JCheckBox和单选按钮JRadioButton用法详解
- 单选与复选控件JRadioButton与JCheckBox的使用
- 《剑指offer》数值的整数次方
- 汉诺塔问题递归求解
- eclipse菜单栏windows/preferences下找不到web和javaEE选项
- JVM笔记整理(第2章)
- Eclipse启动项目时,删除workspaces无用的工作区间
- JavaSwing_2.3: JRadioButton(单选按钮)
- python中UnicodeEncodeError和ImportError
- spring的事务原理
- 文件输入/输出流
- 装饰者模式
- java鬼混笔记:压缩文件生成zip
- AngularJS-4-循环绑定
- JavaSwing_2.4: JCheckBox(复选框)
- 用终端删除掉忽略文件的前缀