吴志勇的博客 吴志勇的博客
  • h5

    • HTML5&CSS3
  • scss

    • css预处理语言
  • JavaScript

    • JavaScript教程
    • Ajax
    • ES6教程
    • NodeJS
    • Typescript
  • 框架

    • Jquery
    • VUE
    • React
  • Swing专题
  • java基础
  • javaweb
  • 框架
  • 数据库
  • netty
  • 设计模式
  • 微服务及架构
  • 云原生
  • maven
  • 单元测试
工具
我的
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

吴志勇

......
  • h5

    • HTML5&CSS3
  • scss

    • css预处理语言
  • JavaScript

    • JavaScript教程
    • Ajax
    • ES6教程
    • NodeJS
    • Typescript
  • 框架

    • Jquery
    • VUE
    • React
  • Swing专题
  • java基础
  • javaweb
  • 框架
  • 数据库
  • netty
  • 设计模式
  • 微服务及架构
  • 云原生
  • maven
  • 单元测试
工具
我的
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Swing专题

    • swing图形化界面简介

    • JavaSwing布局管理器

    • JavaSwing基本组件

      • JLabel(标签)
      • JButton(按钮)
      • JRadioButton(单选按钮)
      • JCheckBox(复选框)
      • JToggleButton(开关按钮)
      • JTextField(文本框)
      • JPasswordField(密码框)
      • JTextArea(文本区域)
      • JComboBox(下拉列表框)
      • JList(列表框)
      • JProgressBar(进度条)
      • JSlider(滑块)
    • JavaSwing面板组件

    • JavaSwing其他组件

    • JavaSwing相关特性

    • 扩展:JavaAWTSwing其他相关

  • java基础

  • javaweb

  • 框架

  • Maven
  • 单元测试
  • 动态代理
  • 数据库

  • netty

  • 设计模式

  • 微服务及架构

  • 云原生

  • Velocity模板引擎
  • 后端
  • Swing专题
  • JavaSwing基本组件
wuzhiyong
2024-09-18

JRadioButton(单选按钮)

# 1. 概述

官方JavaDocsApi: javax.swing.JRadioButton (opens new window)

JRadioButton,单选按钮。

JButton 常用构造方法:

// 无文本,未选中
JRadioButton()

// 有文本,未选中
JRadioButton(String text)

// 有文本,并指定是否选中
JRadioButton(String text, boolean selected)
```java

>JRadioButton 常用方法:
```java
// 设置单选按钮的 文本、字体 和 字体颜色
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)
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

JRadioButton 常用监听器:

// 添加状态改变监听器
void addChangeListener(ChangeListener l)
1
2

ButtonGroup(按钮组): 当有多个单选按钮时,一般只允许一个单选按钮选中,因此需要对同一类型的单选按钮进行分组,如下:

// 创建一个按钮组
ButtonGroup btnGroup = new ButtonGroup();

// 添加单选按钮到按钮组
btnGroup.add(radioBtn01);
btnGroup.add(radioBtn02);
1
2
3
4
5
6

# 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
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

结果展示:

#swing
上次更新: 2024-09-19 12:17:39

← JButton(按钮) JCheckBox(复选框)→

Copyright © 2020-2025 wuzhiyong
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式