3

我第一次使用Swing's GridBagLayout,到目前为止,我只在容器中添加了两个组件,尽管我打算在垂直下方添加更多组件。到目前为止,第一个组件 (a JLabel) 正确定位在PAGE_START,我记得为组件对应的GridBagConstraints. 然而,第二个组件 (a JTextField) 没有按照我的预期定位,它位于容器的中心,而不是在JLabel. 我尝试使用多个锚常量,包括FIRST_LINE_START, PAGE_START, NORTH&NORTHWEST但到目前为止没有任何效果。

因此,我再次呼吁 stackoverflow 的天才编码员寻求帮助。下面是代码片段,下面是问题的图形图像。

    // Instantiate components and configure their corresponding GridBagConstraints attributes
    // refPlusType properties
    refPlusType = new JLabel("<html><h3>"+"Reference"+" - "+"Job Type"+" </h3><hr /></html>");
    refPlusTypeGC = new GridBagConstraints();
    refPlusTypeGC.gridx = 0; // Grid position 
    refPlusTypeGC.gridy = 0;
    refPlusTypeGC.gridwidth = 2; // Number of colums occupied by component
    refPlusTypeGC.insets = new Insets(5, 10, 5, 10); // Specifies margin
    refPlusTypeGC.weightx = 0.1; // Required for anchor to work. 
    refPlusTypeGC.weighty = 0.1; // Required for anchor to work. 
    refPlusTypeGC.anchor = GridBagConstraints.PAGE_START; // Position in container

    // addressLine1 properties
    addressLine1 = new JTextField();
    addressLine1GC = new GridBagConstraints();
    addressLine1GC.gridx = 0; 
    addressLine1GC.gridy = 1;
    addressLine1GC.gridwidth = 2; 
    addressLine1GC.insets = new Insets(0, 10, 0, 10); 
    addressLine1GC.fill = GridBagConstraints.HORIZONTAL; // Specifies component fill Horizontal space
    addressLine1GC.weightx = 0.1; 
    addressLine1GC.weighty = 0.1; 
    addressLine1GC.anchor = GridBagConstraints.FIRST_LINE_START;

    // Add components to this HALLogisticsDetailsPanel
    this.add(refPlusType, refPlusTypeGC);
    this.add(addressLine1, addressLine1GC);

下图;

在此处输入图像描述

感谢大家提供的任何帮助。

4

4 回答 4

4

尝试将weightyforaddressLine1增加到更大的值。我做了一个快速测试,将其设置为 1000:

addressLine1GC.weighty = 1000.0;

并将该addressLine1字段推到标签下方,并在下方带有空格。

于 2013-08-21T13:23:46.617 回答
2

anchor = NORTH奇迹般有效:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestJPanels {

    private JLabel refPlusType;
    private JTextField addressLine1;

    protected void initUI() {
        final JFrame frame = new JFrame(TestJPanels.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new GridBagLayout());
        // Instantiate components and configure their corresponding GridBagConstraints attributes
        // refPlusType properties
        refPlusType = new JLabel("<html><h3>" + "Reference" + " - " + "Job Type" + " </h3><hr /></html>");
        GridBagConstraints refPlusTypeGC = new GridBagConstraints();
        refPlusTypeGC.gridwidth = GridBagConstraints.REMAINDER; // Number of colums occupied by component
        refPlusTypeGC.insets = new Insets(5, 10, 5, 10); // Specifies margin
        refPlusTypeGC.weightx = 1; // Required for anchor to work.
        refPlusTypeGC.anchor = GridBagConstraints.PAGE_START; // Position in container

        // addressLine1 properties
        addressLine1 = new JTextField(20);
        GridBagConstraints addressLine1GC = new GridBagConstraints();
        addressLine1GC.insets = new Insets(0, 10, 0, 10);
        addressLine1GC.fill = GridBagConstraints.HORIZONTAL; // Specifies component fill Horizontal space
        addressLine1GC.weightx = 1;
        addressLine1GC.weighty = 1;
        addressLine1GC.anchor = GridBagConstraints.NORTH;

        // Add components to this HALLogisticsDetailsPanel
        panel.add(refPlusType, refPlusTypeGC);
        panel.add(addressLine1, addressLine1GC);
        frame.add(panel);
        frame.setSize(600, 600);
        frame.setVisible(true);
    }

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

            @Override
            public void run() {
                new TestJPanels().initUI();
            }
        });
    }
}
于 2013-08-21T13:56:35.993 回答
1

好的,我明白了。我不喜欢接受我自己的答案,但我们开始吧。在一天结束的时候,我试图让所有的东西在容器的顶部正确地聚集在一起。为所有组件单独添加weighty属性意味着容器中的剩余空间分布在所有组件之间(相对于它们的weighty属性),因此将它们分开。

答案似乎是通过weighty在 GridBagLayout 中的最低组件添加一个属性,剩余的剩余空间分配到最低组件下方,从而将所有组件推到顶部。默认情况下,当剩余空间根据组件的权重分配给组件时,它会分配在组件下方(至少在 y 轴上)。

这是带有三个组件的新代码以帮助演示;

    // Instantiate components and configure their corresponding GridBagConstraints attributes
    // refPlusType properties
    refPlusType = new JLabel("<html><h3>"+"Reference"+" - "+"Job Type"+" </h3><hr /></html>");
    refPlusTypeGC = new GridBagConstraints();
    refPlusTypeGC.gridx = 0; // Grid position 
    refPlusTypeGC.gridy = 0;
    refPlusTypeGC.gridwidth = 2; // Number of colums occupied by component
    refPlusTypeGC.insets = new Insets(5, 10, 5, 10); // Specifies margin

    // addressLine1 properties
    addressLine1 = new JTextField();
    addressLine1GC = new GridBagConstraints();
    addressLine1GC.gridx = 0; 
    addressLine1GC.gridy = 1;
    addressLine1GC.gridwidth = 2; 
    addressLine1GC.insets = new Insets(0, 10, 0, 10); 
    addressLine1GC.fill = GridBagConstraints.HORIZONTAL; // Specifies component fill Horizontal space

    // addressLine2 properties
    addressLine2 = new JTextField();
    addressLine2GC = new GridBagConstraints();
    addressLine2GC.gridx = 0; 
    addressLine2GC.gridy = 2;
    addressLine2GC.gridwidth = 2; 
    addressLine2GC.insets = new Insets(0, 10, 0, 10); 
    addressLine2GC.fill = GridBagConstraints.HORIZONTAL; // Specifies component fill Horizontal space 
    addressLine2GC.weighty = 1; // Required for anchor to work.
    addressLine2GC.anchor = GridBagConstraints.NORTH; // Position in container

    // Add components to this HALLogisticsDetailsPanel
    this.add(refPlusType, refPlusTypeGC);
    this.add(addressLine1, addressLine1GC);
    this.add(addressLine2, addressLine2GC);
于 2013-08-21T14:10:52.913 回答
1

我见过的 GridBagLayout 最好的“教程”是由 Scott Stanchfield 创建的。您可以在此处找到他在 JavaOne 2001 上提供的 PowerPoint 演示文稿的链接

以前网上有一篇文章有​​同样的信息,不过好像被甲骨文吞了。

请阅读所有关于为什么 GBL 不理想的警告。如果您决定使用它,Scott 会通过创建一个简单的 GUI 草图来提供关于如何确定所有约束的精彩视觉课程。

吉姆·S。

于 2013-08-21T16:08:51.830 回答