“`
package lianxi;
import java.awt.*;
import javax.swing.*;
public class Lianxi {
public static void main(String[] args) {
Lxi l=new Lxi();
}
}
class Lxi extends JFrame
{
private JButton j1,j2;
private JLabel jl1,jl2;
private JCheckBox jb1,jb2,jb3;
private JRadioButton jrb1,jrb2;
//由于单选按钮,每次只能选中一个,所以需要将单选按钮加入到group中
private ButtonGroup btnGroup;
private JPanel pane11,pane12,pane13;
public Lxi()
{
this.j1=new JButton("注册用户");
this.j2=new JButton("取消注册");
this.jl1=new JLabel("你最喜欢的运动");
this.jl2=new JLabel("你的性别");
this.jb1=new JCheckBox("足球");
this.jb2=new JCheckBox("篮球");
this.jb3=new JCheckBox("网球");
this.btnGroup=new ButtonGroup();
this.jrb1=new JRadioButton("男");
this.jrb2=new JRadioButton("女");
this.btnGroup.add(jrb1);
this.btnGroup.add(jrb2);
//实例化Panel,为Panel添加组件
this.pane11=new JPanel();
this.pane12=new JPanel();
this.pane13=new JPanel();
this.pane11.add(this.jl1);
this.pane11.add(this.jb1);
this.pane11.add(this.jb2);
this.pane11.add(this.jb3);
this.pane12.add(this.jl2);
this.pane12.add(this.jrb1);
this.pane12.add(this.jrb2);
this.pane13.add(this.j1);
this.pane13.add(this.j2);
//为JFrame设置布局方式
this.setLayout(new GridLayout(3,1));
//添加组件
this.add(pane11);
this.add(pane12);
this.add(pane13);
this.setSize(300,300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
“`