Java 无法使GridBagLayout按需要工作

Java 无法使GridBagLayout按需要工作,java,swing,Java,Swing,有什么线索吗 关于,Francesc您需要在约束上设置gridx和gridy。查看本教程:您尚未为元素指定gridx和gridy设置 /** * I'd like to achive following layout: * +----------+----------+ * | Button 1 | | * +----------| Button 2 | * | Button 3 | | * +----------+-----

有什么线索吗


关于,Francesc

您需要在约束上设置gridx和gridy。查看本教程:

您尚未为元素指定gridx和gridy设置

/**
 *  I'd like to achive following layout:
 *   +----------+----------+
 *   | Button 1 |          |
 *   +----------| Button 2 |
 *   | Button 3 |          |
 *   +----------+----------+
 *  with following code:
 */

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;

button = new JButton("Button 1");
c.weightx = 0.5;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
panel.add(button, c);

button = new JButton("Button 2");
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 2;
panel.add(button, c);

button = new JButton("Button 3");
c.gridwidth = 1;
panel.add(button, c);

/**
 *  but what I achive is:
 *   +----------+----------+
 *   | Button 1 | Button 2 |
 *   +----------+----------+|
 *   | Button 3 |
 *   +----------+
 */

/**
 *  However layout:
 *   +----------+----------+
 *   |          | Button 2 |
 *   + Button 1 +----------+
 *   |          | Button 3 |
 *   +----------+----------+
 *  is easily achieved as:
 */

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;

button = new JButton("Button 1");
c.weightx = 0.5;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.gridheight = 2;
panel.add(button, c);

button = new JButton("Button 2");
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 1;
panel.add(button, c);

button = new JButton("Button 3");
panel.add(button, c);

注意:gridx和gridy的默认值为0,但最好始终设置该值。它更清晰,并且在复制和粘贴等时依赖默认值可能会导致问题。因此,这种方式更清晰、更安全(尽管稍微详细一些)。

众所周知,GridBagLayout很困难(请参阅):

的标准布局管理器 JavaSDK,GridBagLayout是 最有用的一个,但它的复杂性 人们也知道它会引起人们的恐惧 程序员的心。部分 复杂性在于工作的单调乏味 设置网格包约束


我会阅读这些文章并在论坛上询问(例如)

您需要指定gridx和gridy,默认值为0

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;

button = new JButton("Button 1");
c.weightx = 0.5;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 0;
panel.add(button, c);

button = new JButton("Button 2");
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 2;
c.gridx = 1;
c.gridy = 0;
panel.add(button, c);

button = new JButton("Button 3");
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
panel.add(button, c);

必须为最后一个按钮设置c.gridx。默认情况下,零部件彼此相对放置。如果将按钮3的gridx设置为0,则会将其强制到第一列

    button = new JButton("Button 1");
    c.weightx = 0.5;
    c.weighty = 0.5;
    c.fill = GridBagConstraints.BOTH;
    panel.add(button, c);

    button = new JButton("Button 3");
    c.gridy = 1;
    c.gridwidth = 1;
    panel.add(button, c);

    button = new JButton("Button 2");
    c.gridx = 1; c.gridy = 0;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.weighty = 1;c.gridheight = 2;
    panel.add(button, c);

为什么不改为使用使用GridLayouts的嵌套JPanel,例如:

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;

button = new JButton("Button 1");
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
panel.add(button, c);

button = new JButton("Button 2");
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = GridBagConstraints.REMAINDER;        
panel.add(button, c);

button = new JButton("Button 3");
c.gridx = 0;
c.gridwidth = 1;
c.gridheight = 1;
panel.add(button, c);

或者我们还需要知道更多的故事吗?

是的,其他人是对的。您必须手动设置GridX和GridY

此外,我建议为每个组件使用一个新的
GridBagConstraints
实例,因为在更复杂的形式中,您可以轻松了解在您使用的一个实例中实际设置了哪些值。此外,如果将构造函数与许多参数一起使用,则不必关心默认值,因为每次都必须指定它们。习惯需要时间,但以后会有回报

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

public class GridLayoutEg {
    public static void main(String[] args) {
        JPanel leftPanel = new JPanel(new GridLayout(0, 1));
        leftPanel.add(new JButton("Button 1"));
        leftPanel.add(new JButton("Button 3"));

        JPanel mainPanel = new JPanel(new GridLayout(1, 0));
        mainPanel.add(leftPanel);
        mainPanel.add(new JButton("Button 2"));

        JFrame frame = new JFrame("Foo");
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

好的,您的代码可以工作,但是因为按钮2有c.gridwidth=gridbagsconstraints.rements;按钮3应开始一个新行,即第一列,无需指定

    /**
     *   +----------+----------+
     *   | Button 1 |          |
     *   +----------| Button 2 |
     *   | Button 3 |          |
     *   +----------+----------+
     */

    JPanel panel = new JPanel(new GridBagLayout());

    JButton b1 = new JButton("Button 1");
    panel.add(b1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0,
            0), 0, 0));

    JButton b2 = new JButton("Button 2");
    panel.add(b2, new GridBagConstraints(1, 0, 1, 2, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0,
            0), 0, 0));

    JButton b3 = new JButton("Button 3");
    panel.add(b3, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
        GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0,
            0), 0, 0));