1 import java.awt.Container;
2 import java.awt.GridLayout;
3
4 import javax.swing.*;
5
6
7 public class num_1v extends JFrame{
8 //声明控件和布局管理器
9 JRadioButton[] rb = new JRadioButton[5];
10 JCheckBox check[] = new JCheckBox[5];
11 JComboBox<String> jcb;
12 JTextArea jta;
13 JButton jb;
14 //构造方法
15 public num_1v(){
16 Container con = getContentPane();// 创建容器,默认边界布局
17 GridLayout gr = new GridLayout(2,1);// 声明5行1列的网格布局
18 con.setLayout(gr);
19 // 第一行
20 JPanel jp1 = new JPanel();// 创建面板
21 jp1.setLayout(new GridLayout(3,1));
22 //第一行 -- 第一子行
23 JPanel jp1_1 = new JPanel();
24 JLabel lb1 = new JLabel("年龄段");// 创建标签
25 jp1_1.add(lb1);
26 ButtonGroup bg = new ButtonGroup(); // 创建单选按钮组
27 String[] rbstr = {"5-15岁","16-25岁","26-35岁","36-45岁","46-55岁"};
28 for (int i = 0;i<rbstr.length;i++){
29 rb[i] = new JRadioButton(rbstr[i]);// 定义按钮组对象
30 bg.add(rb[i]);
31 jp1_1.add(rb[i]);
32 }
33 jp1.add(jp1_1);
34 //第一行 -- 第二子行
35 JPanel jp1_2 = new JPanel();// 新建面板2放置复选框
36 JLabel lb2 = new JLabel("兴趣爱好");
37 jp1_2.add(lb2);
38 String[] cbstr = {"交友","户外","购物","阅读","其它"};// 定义字符串数组,存放单选按钮名称
39 for (int i = 0;i < rb.length;i++){
40 check[i] = new JCheckBox(cbstr[i]);
41 jp1_2.add(check[i]);
42 }
43 jp1.add(jp1_2);
44 //第一行 -- 第三子行
45 JPanel jp1_3 = new JPanel();// 新建面板pan3放置下拉列表
46 JLabel lb3 = new JLabel("院系");// 新建标签
47 jp1_3.add(lb3);
48 String[] deNames = {"电子信息学院","商学院","人文艺术学院",};
49 jcb = new JComboBox<String>(deNames);
50 jp1_3.add(jcb);// 把控件添加到面板3
51 jb = new JButton("提交");
52 jp1_3.add(jb);
53 jp1.add(jp1_3); // 把面板添加到容器
54 con.add(jp1);
55 //第二行
56 jta = new JTextArea(3,3);
57 jta.setLineWrap(true);
58 JScrollPane jsp = new JScrollPane(jta);
59 con.add(jsp);
60 setSize(500, 350);// 设置窗体大小
61 setTitle("单选框复选框测试"); // 设置窗体标题
62 // setResizable(false); //窗体不可以调整大小
63 setLocationRelativeTo(null);// 设置窗体在中央
64 setDefaultCloseOperation(EXIT_ON_CLOSE);
65 setVisible(true);// 设置窗体可见
66 }
67 public static void main(String[] args) {
68 new num_1v();
69 }
70 }
来源:https://www.cnblogs.com/WuYangdan-5201314/p/10910200.html