使用NetBeans创建GUI时出现问题:GroupLayout一次只能与一个Container一起使用?

时间:2013-05-26 11:56:10

标签: java swing netbeans layout-manager grouplayout

我在JAVA中使用Netbeans制作GUI。 我的想法是这样的:

JFrame -> contains a JPanel -> call a JDialog.

                            -> call a second JDialog

我的想法是,我想在第一个JDialog中读取内容,当用户完成这个JDialog时,他会按下一个Button。这个JDialog dispose()和JPanel调用另一个JDialog。

我的想法类似于Netbeans中的项目向导或类似的内容。

问题是:GroupLayout一次只能与一个Container一起使用。

这是我的代码:

public class SubjectsPanel extends javax.swing.JPanel {
        private AddSubject addsubject; //Is an own class that inherites from JDialog.
        private AddGroups addgroups; //Is another own class that inherites from JDialog.
        private JFrame jf;




    public SubjectsPanel(JFrame fr) {
        jf = fr;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

    }

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
        addsubject = new AddSubject(jf,true);
        addsubject.setSize(700, 350);
        addsubject.setLocationRelativeTo(this);
        addsubject.setTitle("Adding New Subject Wizard");
        addsubject.setVisible(true);
        addsubject.addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent we) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void windowClosing(WindowEvent we) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void windowClosed(WindowEvent we) {
                if(addsubject.getBool()){                    
                    addgroup = new AddGroup(jf,true);
                    addgroup.setSize(700, 350);
                    addgroup.setLocationRelativeTo(this);
                    addgroup.setTitle("Adding New Group Wizard");
                    addgroup.setVisible(true);
                }
            }

            @Override
            public void windowIconified(WindowEvent we) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void windowDeiconified(WindowEvent we) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void windowActivated(WindowEvent we) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

            @Override
            public void windowDeactivated(WindowEvent we) {
            }
        });
    }                                                
}

当然我省略了一些代码,我认为它是无关紧要的。问题出在WindowListener中,完全在这里:

addgroup = new AddGroup(jf,true);
addgroup.setSize(700, 350);
addgroup.setLocationRelativeTo(this);
addgroup.setTitle("Adding New Group Wizard");
addgroup.setVisible(true);

我怎么能做得好?

抱歉我的英文。 此外,我试图阅读有关GroupLayouts的其他问题,但我无法用它们来解决。

1 个答案:

答案 0 :(得分:0)

GroupLayout一次只能与一个Container一起使用,但您可以拥有任意数量的Container个实例,每个实例都有自己的GroupLayout。在此example中,尝试添加GroupPanel的第三个实例。

    f.add(new GroupPanel(3));

image