1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| package com.mingrisoft;
import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.GridLayout;
import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.EmptyBorder;
public class CheckBoxArray extends JFrame {
private static final long serialVersionUID = -5338362310060106193L; private JPanel contentPane; private JPanel panel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() { public void run() { try { CheckBoxArray frame = new CheckBoxArray(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }
public CheckBoxArray() { setTitle("通过复选框控件数组实现添加多个复选框控件"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 409, 331); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane);
JLabel label = new JLabel("你的爱好有哪些:"); contentPane.add(label, BorderLayout.NORTH); contentPane.add(getPanel(), BorderLayout.CENTER); }
private JPanel getPanel() { if (panel == null) { panel = new JPanel(); panel.setLayout(new GridLayout(0, 4)); String[] labels = { "足球", "篮球", "魔术", "乒乓球", "看电影", "魔兽世界", "CS战队", "羽毛球", "游泳", "旅游", "爬山", "唱歌", "写博客", "动物世界", "拍照", "弹吉他", "读报纸", "飙车", "逛街", "逛商场", "麻将", "看书", "上网看资料", "新闻", "军事", "八卦", "养生", "饮茶" }; JCheckBox[] boxs = new JCheckBox[labels.length]; for (int i = 0; i < boxs.length; i++) { boxs[i] = new JCheckBox(labels[i]); panel.add(boxs[i]); } } return panel; } }
|