简体   繁体   中英

Make JPanels look the same with GridBagLayout

I do have multiple JPanels all of which are based on the assumption they are three columns wide. And one or more columns high. At the moment I'm using GridBagLayout for them.

I plan adding them underneath each other in another JPanel. The problem is now, that the columns don't line up.

Is there something I can do about this while keeping the single JPanels separate from each other? Without falling back to some more fixed layout. I could give the JPanels the parent container and add the elements to it while keeping the JPanel itself empty (when not in "stand alone mode"). But I think this approach is disgusting.

Thanks for your help!

EDIT: My Layout class looks like this:

public class Layout extends GridBagLayout {
    public void collocate_line(List<? extends JComponent> compos, final Container container) {
        int currentCol = 0;

        for (final JComponent compo: compos) {
            final GridBagConstraints gbc = new GridBagConstraints(
                    currentCol, this.currentLine, GRID_WIDTH, GRID_HEIGHT,
                    DEFAULT_WEIGHT_X, DEFAULT_WEIGHT_Y, GridBagConstraints.CENTER,
                    GridBagConstraints.BOTH, DEFAULT_INSETS, 0, 0);
            container.add(compo, gbc);
            currentCol++;
        }

        currentLine++;
    }
}

A containing JPanel looks like this:

public class ScopePanel extends JPanel implements ActionListener {
    private final ChooseClassPath classPath;
    private final ChooseStubsJava stubsJava;

    public ScopePanel(final IPreparedCommands exec, final ResourceBundle messages) {
        super();

        final Layout gbl = new Layout();
        this.setLayout(gbl);

        this.classPath = new ChooseClassPath(exec, messages, gbl);
        this.add(this.classPath, GUIUtil.mkgbc_fillx(0, 0, GridBagConstraints.REMAINDER, 1));

        this.stubsJava = new ChooseStubsJava(exec, messages, gbl);
        this.add(this.stubsJava, GUIUtil.mkgbc_fillx(0, 2, GridBagConstraints.REMAINDER, 1));
    }
}

And an element looks like:

public class ChooseClassPath extends JPanel {
    public ChooseClassPath(final IPreparedCommands exec, final ResourceBundle messages) {
        this(exec, messages, new Layout());
    }

    public ChooseClassPath(final IPreparedCommands exec, final ResourceBundle messages, final Layout layout) {
        super();

        { // Line 1
            final List<JComponent> line = new ArrayList<JComponent>();

            final JLabel label = new JLabel(messages.getString("label_classpath"));
            line.add(label);

            //...  
            layout.collocate_line(line, this);
        }
    }
}

The result is: 在此处输入图片说明

Make sure you pass the GirdBagConstraints properties set identical to all the JPanels. esp Properties like gridwidth,fill,weightx ect. Also the content inside your JPanel, if they vary for each JPanel instances could affect the individual size of the grids. Consider using GridLayout and go for GridBagLayout only if your requirement cannot be met by GridLayout or any other simpler layouts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM