Java图形界面——复选框与单选按钮

来源:互联网 发布:采购流程优化四手法 编辑:程序博客网 时间:2024/05/22 05:31
  1. /* 
  2.  * 调查窗口 
  3.  */  
  4. package com.test.swing;  
  5. import java.awt.*;  
  6. import javax.swing.*;  
  7.   
  8. public class Test2 extends JFrame{  
  9.         //定义组件  
  10.     JPanel jp1, jp2, jp3;  
  11.     JLabel jl1, jl2;  
  12.     JCheckBox jcb1, jcb2, jcb3; //复选框  
  13.     JRadioButton jrb1, jrb2;    //单选按钮  
  14.     ButtonGroup bg;  
  15.     JButton jb1, jb2;  
  16.     public static void main(String[] args) {  
  17.         Test2 test = new Test2();  
  18.   
  19.     }  
  20.         //构造函数  
  21.     public Test2(){  
  22.         //创建组件  
  23.         jp1 = new JPanel();  
  24.         jp2 = new JPanel();  
  25.         jp3 = new JPanel();  
  26.           
  27.         jl1 = new JLabel("你喜欢的运动:");  
  28.         jl2 = new JLabel("你的性别:");  
  29.           
  30.         jcb1 = new JCheckBox("瑜伽");  
  31.         jcb2 = new JCheckBox("足球");  
  32.         jcb3 = new JCheckBox("跑步");  
  33.           
  34.         jrb1 = new JRadioButton("男");  
  35.         jrb2 = new JRadioButton("女");  
  36.             //将单选按键加入ButtonGroup,否则可以多选  
  37.         bg = new ButtonGroup();  
  38.         bg.add(jrb1);  
  39.         bg.add(jrb2);  
  40.           
  41.         jb1 = new JButton("注册");  
  42.         jb2 = new JButton("取消");  
  43.           
  44.             //设置布局管理  
  45.         this.setLayout(new GridLayout(31));  
  46.             //添加组件  
  47.         jp1.add(jl1);  
  48.         jp1.add(jcb1);  
  49.         jp1.add(jcb2);  
  50.         jp1.add(jcb3);  
  51.           
  52.         jp2.add(jl2);  
  53.         jp2.add(jrb1);  
  54.         jp2.add(jrb2);  
  55.           
  56.         jp3.add(jb1);  
  57.         jp3.add(jb2);  
  58.           
  59.         this.add(jp1);  
  60.         this.add(jp2);  
  61.         this.add(jp3);  
  62.           
  63.         this.setSize(300150); //设置窗体大小  
  64.         this.setResizable(false);//固定窗体大小  
  65.         this.setVisible(true);  
  66.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  67.     }  
  68. }  
阅读全文
0 0