package com.SwingIn51; import java.awt.*; import javax.swing.*;; public class CheckBoxDemo extends JFrame { JPanel panel1,panel2,panel3; JButton an1,an2; JLabel label1,label2; JCheckBox box1,box2;//设置复选框 JRadioButton rb1,rb2;//设置单选 ButtonGroup bg;//设置组,即把rb1,rb2放在这里 public CheckBoxDemo() { panel1=new JPanel(); panel2=new JPanel(); panel3=new JPanel(); an1=new JButton("登录"); an2=new JButton("取消"); label1=new JLabel("语种"); label2=new JLabel("性别"); box1=new JCheckBox("中文"); box2=new JCheckBox("英语"); rb1=new JRadioButton("男"); rb2=new JRadioButton("女"); bg=new ButtonGroup();// bg.add(rb1);//设置组,即把rb1,rb2放在这里 bg.add(rb2);//设置组,即把rb1,rb2放在这里 this.setLayout(new GridLayout(3,1)); panel1.add(label1); panel1.add(box1); panel1.add(box2); panel2.add(label2); panel2.add(rb1); panel2.add(rb2); panel3.add(an1);//添加还是要添加一个个按钮,不能直接添加一个组
panel3.add(an2); this.add(panel1); this.add(panel2); this.add(panel3); this.setTitle("用户注册"); this.setSize(240, 150); this.setLocation(300,280); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { CheckBoxDemo d=new CheckBoxDemo(); } }