复选框,单选框

来源:互联网 发布:淘宝怎么买岛国资源 编辑:程序博客网 时间:2024/06/06 04:42

import java.awt.GridLayout;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JRadioButton;/** * Frame *  * @author wangzhen *  *         JcheckBox 复选框 JredioButton 单选框 */public class Test1 { public static void main(String args[]) {  new MyFrame(); }}class MyFrame extends JFrame { JPanel jp1 = null, jp2 = null, jp3 = null; JLabel jl1 = null, jl2 = null; JButton jb1 = null, jb2 = null; JCheckBox jcb1 = null, jcb2 = null, jcb3 = null; JRadioButton jrb1 = null, jrb2 = null; ButtonGroup bg = null; public MyFrame() {  // 创建组建  jp1 = new JPanel();  jp2 = new JPanel();  jp3 = new JPanel();  jl1 = new JLabel("你喜欢的运动");  jl2 = new JLabel("你的性别");  jb1 = new JButton("注册用户");  jb2 = new JButton("取消注册");    jcb1 = new JCheckBox("足球");  jcb2 = new JCheckBox("篮球");  jcb3 = new JCheckBox("网球");    jrb1= new JRadioButton("男");  jrb2= new JRadioButton("女");  //一定要把jrb1,jrb2放到一个ButtonGroup中  ButtonGroup bg = new ButtonGroup();  bg.add(jrb1);  bg.add(jrb2);    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.setTitle("注册");  this.setBounds(400,400,300,150);  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  this.setVisible(true); }}

0 0