Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重新定位整个网格布局 我有一个 GridBagLayout ,它在 jPoCTs/COD>中,但在中间是死掉的。_Java_Swing_Layout Manager_Gridbaglayout - Fatal编程技术网

Java 重新定位整个网格布局 我有一个 GridBagLayout ,它在 jPoCTs/COD>中,但在中间是死掉的。

Java 重新定位整个网格布局 我有一个 GridBagLayout ,它在 jPoCTs/COD>中,但在中间是死掉的。,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,基本上,我想做的是保持布局不变,但将其移动到红色箭头所指的位置(甚至更低一点)。以下是我编写的一些代码: setLayout(new GridBagLayout()); ...make all the JLabels/RadioBtns.. GridBagConstraints gbc1 = new GridBagConstraints(); gbc1.anchor=GridBagConstraints.FIRST_LINE_START; gbc1.insets = new Insets(5

基本上,我想做的是保持布局不变,但将其移动到红色箭头所指的位置(甚至更低一点)。以下是我编写的一些代码:

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方向都展开,如果网格袋单元有展开的空间

如果这没有帮助,那么您需要创建并发布一个

还有一件事:如果让所有组件都努力达到它们的首选大小,通常可以获得最佳的布局,最好是避免在大多数组件上调用
setSize(…)
setPreferredSize(…)
,而不是简单地调用
pack()
在顶层窗口上显示,然后再显示


编辑
在评论中,您声明:

我以前试过这个。它会将布局对齐到右上角,但也会将零部件分散到面板上。我希望保持相同的集群。我希望所有的标签和无线基站都在附近(如图所示)

然后,您要做的是使用并嵌套至少两个JPanel,第一个使用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();
            }
        });
    }
}

此问题几乎总是由未正确设置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方向都展开,如果网格袋单元有展开的空间

如果这没有帮助,那么您需要创建并发布一个

还有一件事:如果让所有组件都努力达到它们的首选大小,通常可以获得最佳的布局,最好是避免在大多数组件上调用
setSize(…)
setPreferredSize(…)
,而不是简单地调用
pack()
在顶层窗口上显示,然后再显示


编辑
在评论中,您声明:

我以前试过这个。它会将布局对齐到右上角,但也会将零部件分散到面板上。我希望保持相同的集群。我希望所有的标签和无线基站都在附近(如图所示)

然后,您要做的是使用并嵌套至少两个JPanel,第一个使用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();
            }
        });
    }
}

默认情况下,
GridBagLayout
将所有组件集中在容器的中间。您可以使用
weightx
weighty
更改任何给定组件使用容器中剩余空间填充其单元格的方式,并使用位于其他组件右/下方的简单“空”填充组件将产生所需效果。默认情况下,不带任何文本的
JLabel
可以在此处正常工作
GridBagLayout
将所有组件集中在容器的中间。您可以使用
weightx
weighty
更改任何给定组件使用容器中剩余空间填充其单元格的方式,并使用位于其他组件右/下方的简单“空”填充组件将产生所需效果。没有任何文本的
JLabel
在这里可以很好地工作,谢谢。我以前试过这个。它会将布局对齐到右上角,但也会将零部件分散到面板上。我希望保持相同的集群。我希望所有的标签和无线基站都在附近(如图所示)@OrangePot:啊,那么你需要嵌套jpanel来实现这一点。请参见“编辑以回答”。@OrangePot:例如,请参见“编辑”中添加的代码。谢谢。我以前试过这个。它会将布局对齐到右上角,但也会将零部件分散到面板上。我希望保持相同的集群。我希望所有的标签和无线基站都在附近(如图所示)@OrangePot:啊,那么你需要嵌套jpanel来实现这一点。请参见“编辑以回答”。@OrangePot:例如,请参见“编辑”中添加的代码。