- /*
- * 调查窗口
- */
- package com.test.swing;
- import java.awt.*;
- import javax.swing.*;
- public class Test2 extends JFrame{
- //定义组件
- JPanel jp1, jp2, jp3;
- JLabel jl1, jl2;
- JCheckBox jcb1, jcb2, jcb3; //复选框
- JRadioButton jrb1, jrb2; //单选按钮
- ButtonGroup bg;
- JButton jb1, jb2;
- public static void main(String[] args) {
- Test2 test = new Test2();
- }
- //构造函数
- public Test2(){
- //创建组件
- jp1 = new JPanel();
- jp2 = new JPanel();
- jp3 = new JPanel();
- jl1 = new JLabel("你喜欢的运动:");
- jl2 = new JLabel("你的性别:");
- jcb1 = new JCheckBox("瑜伽");
- jcb2 = new JCheckBox("足球");
- jcb3 = new JCheckBox("跑步");
- jrb1 = new JRadioButton("男");
- jrb2 = new JRadioButton("女");
- //将单选按键加入ButtonGroup,否则可以多选
- bg = new ButtonGroup();
- bg.add(jrb1);
- bg.add(jrb2);
- jb1 = new JButton("注册");
- jb2 = new JButton("取消");
- //设置布局管理
- this.setLayout(new GridLayout(3, 1));
- //添加组件
- jp1.add(jl1);
- jp1.add(jcb1);
- jp1.add(jcb2);
- jp1.add(jcb3);
- jp2.add(jl2);
- jp2.add(jrb1);
- jp2.add(jrb2);
- jp3.add(jb1);
- jp3.add(jb2);
- this.add(jp1);
- this.add(jp2);
- this.add(jp3);
- this.setSize(300, 150); //设置窗体大小
- this.setResizable(false);//固定窗体大小
- this.setVisible(true);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
Java图形界面——复选框与单选按钮
最新推荐文章于 2022-07-27 16:41:34 发布