我做错了什么?我正在尝试向jpanel1添加一个复选框组。但是我真的不明白为什么不起作用?
ps。 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)
    );

最佳答案

您将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);
        }
    });
}


看起来像这样

关于java - 复选框组未显示在面板上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21054330/

10-09 13:19