Java中的JRadioButton组件
在Java的Swing界面库中,JRadioButton是一种可供用户选择的单选按钮组件。它允许用户从一组互斥的选项中选择一个。本文将介绍JRadioButton的用法,并提供相应的源代码示例。
JRadioButton的基本使用方法如下所示:
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class JRadioButtonExample {
public static void main(String[] args) {
// 创建 JFrame 实例
JFrame frame = new JFrame("JRadioButton Example");
// 设置窗口的宽度和高度
frame.setSize(300, 200);
// 当关闭窗口时终止程序
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建 JPanel 实例
JPanel panel = new JPanel();
// 创建 JRadioButton 实例
JRadioButton option1 = new JRadioButton("Option 1"