复选框组不会出现在面板中

时间:2014-01-10 21:01:16

标签: java swing checkbox jpanel

我做错了什么?我正在尝试将复选框组添加到jpanel1。但我真的不明白为什么不起作用? 附: GUI由NetBeans matisse制作,然后我手动编辑新类中的代码。 感谢

CheckboxGroup cbGroup = new CheckboxGroup();
  Checkbox importBox = new Checkbox("aaa", cbGroup, true);
  Checkbox specialBox = new Checkbox("bbb", cbGroup, false);


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

    jPanel1.setLayout(new FlowLayout());
    jPanel1.add(importBox);
    jPanel1.add(specialBox);

    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 210, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 100, Short.MAX_VALUE)
    );

1 个答案:

答案 0 :(得分:2)

将JCheckBox添加到容器中;或许像这样

public static void main(String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame();
            ButtonGroup bg = new ButtonGroup();
            JCheckBox importBox = new JCheckBox("aaa", true);
            JCheckBox specialBox = new JCheckBox("bbb", false);
            bg.add(importBox);
            bg.add(specialBox);
            JPanel panel = new JPanel();
            panel.add(importBox);
            panel.add(specialBox);
            frame.add(panel);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    });
}

看起来像这样

Using JCheckBox(s)