自定义JPanel类不会显示在Container的BoxLayout中

时间:2016-01-11 07:09:26

标签: java swing intellij-idea

SCMain.java中的选定代码:

public JPanel createContentPane() {
    //Create the content-pane-to-be.
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setOpaque(true);

    return contentPane;
}

public JPanel populateContentPane() {
    JPanel container = new JPanel();
    container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
    JPanel panel1 = new AddAccountForm(this, this.getInputSet());;
    JPanel panel2 = new JPanel();

    container.add(Box.createRigidArea(new Dimension(0, 5)));
    container.add(panel1);
    container.add(Box.createRigidArea(new Dimension(0, 5)));
    container.add(panel2);
    container.add(Box.createGlue());
    return container;
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread.
 */
private void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Sole Commando v1.0");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    frame.setJMenuBar(this.createMenuBar());
    frame.setContentPane(this.createContentPane());

    // Add split panels
    frame.add(populateContentPane(), BorderLayout.CENTER);

    frame.pack();
    //Display the window.
    frame.setSize(1080, 1080);
    frame.setVisible(true);
}

AddAccountForm.java中的选定代码:

public class AddAccountForm extends JPanel implements ActionListener{
    AddAccountForm(SCMain main, Set<String> InputSet) {
        //Combobox setup

        setSize(300, 300);
        System.out.println(Arrays.toString(storeNameList.toArray()));
        submitButton.addActionListener(this);
    }

    public JPanel getAddAccountRoot() {
        return addAccountRoot;
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
        storeNames = new JComboBox();
    }
}

我使用AddAccountForm.java作为JFrame进行测试(扩展JFrame而不是扩展JPanel,并将pack(),setContentPane(addAccountRoot)添加到AddAccountForm.java),如果我这样做,它会调出正确的AddAccountForm GUI: / p>

SCMain new1 = new SCMain();
AddAccountForm new2 = new AddAccountForm(new1, new1.getInputSet());

但是,当将它用作JPanel(上面的SCMain.java代码中的panel1)并运行SCMain时,AddAccountForm GUI根本不显示。 注意:JPanel AddAccountForm是在IntelliJ GUI Builder中创建的,但正如我之前所说的,它可以作为JFrame使用,因此代码必须有点正确。

0 个答案:

没有答案