重新定位整个 GridBagLayout

Reposition whole GridBagLayout

我有一个 GridBagLayout,它位于 JPanel 的内部,但居中死在它的中间。

本质上,我想做的是保持排列不变,但将其移动到红色箭头指向的位置(甚至更低一点)。这是我写的一些代码:

setLayout(new GridBagLayout());

...make all the JLabels/RadioBtns..

GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.anchor=GridBagConstraints.FIRST_LINE_START;
gbc1.insets = new Insets(5,5,5,5);

gbc1.gridy=0;
gbc1.gridx=0;
add(title, gbc1);

gbc1.gridx=0;
gbc1.gridy=1;
add(block, gbc1);

..add more components

这个问题几乎总是由于没有正确设置 GridBagConstraints.weightx 和 GridBagConstraints.weighty 属性引起的。它们的默认值为 0,这告诉组件以接近 preferredSize 的大小聚集在中心。在所有组件上都给它们 1.0,看看会发生什么。

GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.anchor=GridBagConstraints.FIRST_LINE_START;
gbc1.insets = new Insets(5,5,5,5);

//  ***** add: *****
gbc1.weightx = 1.0;
gbc1.weighty = 1.0;

这会告诉布局在 x 和 y 方向上扩展,如果网格包单元格有扩展空间。

如果这没有帮助,那么您需要创建 post 一个 Minimal, Complete, and Verifiable example.

还有一件事:如果让所有组件努力达到它们的 preferredSize,通常会实现最好的最令人愉悦的布局,最好通过避免在大多数组件上调用 setSize(...)setPreferredSize(...) 来实现, 而不是在显示它之前简单地在顶层 window 上调用 pack()


编辑
在评论中您声明:

I have tried this before. It does align the layout into the top right corner, but it also scatters the components across the Panel. Im looking to keep the same clustering. I want all the labels and radiobtns to be close by (like in the pic)

那么你要做的是使用并嵌套至少 两个 JPanels,第一个使用 GridBagLayout 并持有你的 GUI 组件,如上所示,第二个持有第一个 GridBagLayout - 使用 JPanel。第二个 JPanel 可以使用 BoxLayout 或 FlowLayout(FlowLayout.LEFT),或一堆其他可能的布局或布局组合。

例如:

import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class GridBagLayoutEg extends JPanel {
    private static final Insets INSETS = new Insets(5, 5, 5, 5);
    private static final int PREF_W = 800;
    private static final int PREF_H = 600;

    public GridBagLayoutEg() {
        JPanel innerPanel = new JPanel(new GridBagLayout());
        innerPanel.add(new JLabel("Sequence name: abcdc"), createGbc(0, 0));
        GridBagConstraints gbc = createGbc(1, 0);
        gbc.gridwidth = 3;
        innerPanel.add(new JLabel(), gbc);

        innerPanel.add(new JLabel("Block making alighment tool:", SwingConstants.LEFT), createGbc(0, 1));
        innerPanel.add(new JRadioButton("Mafft"), createGbc(1, 1));
        innerPanel.add(new JRadioButton("Muscle"), createGbc(2, 1));
        innerPanel.add(new JRadioButton("ClusteIO"), createGbc(3, 1));

        innerPanel.add(new JLabel("Select Codon Table:", SwingConstants.LEFT), createGbc(0, 2));
        innerPanel.add(new JRadioButton("Standard"), createGbc(1, 2));
        innerPanel.add(new JRadioButton("Custom"), createGbc(2, 2));
        innerPanel.add(new JLabel(), createGbc(3, 2));

        innerPanel.add(new JLabel("Strictness:", SwingConstants.LEFT), createGbc(0, 3));
        innerPanel.add(new JTextField(2), createGbc(1, 3));

        innerPanel.add(new JLabel("Degeneracy:", SwingConstants.LEFT), createGbc(0, 4));
        innerPanel.add(new JTextField(2), createGbc(1, 4));

        setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
        add(innerPanel);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private GridBagConstraints createGbc(int x, int y) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.insets = INSETS;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        return gbc;
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("GridBagLayoutEg");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new GridBagLayoutEg());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}