首页 > 开发 > Java > 正文

Java Swing JRadioButton单选按钮具体使用

2020-07-28 13:49:16
字体:
来源:转载
供稿:网友

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);  }}

结果展示:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持错新网之家。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表