重新定位整个GridBagLayout

Ora*_*Pot 1 java swing layout-manager gridbaglayout

我有一个GridBagLayout在里面JPanel,但是在它的中间死了.

整个区域是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
Run Code Online (Sandbox Code Playgroud)

Hov*_*els 7

此问题几乎总是由于未正确设置GridBagConstraints.weightx和GridBagConstraints.weighty属性引起的.它们的默认值为0,它告诉组件在中心聚集一个near 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;
Run Code Online (Sandbox Code Playgroud)

这表示如果网格包单元格有扩展空间,则布局将在x和y方向上扩展.

如果这没有帮助,那么您将需要创建并发布一个Minimal,Complete和Verifiable示例.

还有一件事:如果让所有组件都努力达到他们的preferredSize,通常会获得最令人满意的布局,这最好通过避免调用setSize(...)或setPreferredSize(...)大多数组件来完成,而只需pack()在显示它之前调用顶级窗口.


编辑
评论你说:

我之前尝试过这个.它确实将布局对齐到右上角,但它也会在Panel中分散组件.我希望保持相同的聚类.我希望所有的标签和无线电广播都在附近(如图中所示)

那么你想要做的是使用并嵌套至少两个 JPanels,第一个使用GridBagLayout并保持你的GUI组件,如上所示,第二个使用JPanel保存第一个GridBagLayout.第二个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();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)