繁体   English   中英

Java Swing GridBagLayout组件定位

[英]Java Swing GridBagLayout Component Positioning

我第一次使用SwingGridBagLayout ,到目前为止我只在容器中添加了两个组件,尽管我打算在垂直方向下添加更多组件。 到目前为止,第一个组件(一个JLabel )正确定位在PAGE_START ,我记得为组件的相应GridBagConstraints设置权重属性。 然而,第二个组件(一个JTextField )没有按照我的意图定位,它在容器中居中而不是在JLabel下面向上移动。 我试图使用多个锚定常量,包括FIRST_LINE_STARTPAGE_STARTNORTHNORTHWEST但到目前为止还没有任何工作。

因此,我再次呼吁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);

图片如下;

在此输入图像描述

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

尝试将addressLine1的权weighty addressLine1更大的值。 我做了一个快速测试,将其设置为1000:

addressLine1GC.weighty = 1000.0;

并将addressLine1字段向上推到标签下面,下面是空格。

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();
            }
        });
    }
}

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

答案似乎是通过向GridBagLayout中的最低组件添加一个weighty属性,剩下的剩余空间被分配到最低组件的下方,因此将所有组件推送到顶部。 默认情况下,似乎当剩余空间根据其权重分配给组件时,它会下方分配(至少在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);

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

曾经有一篇在线文章有相同的信息,但它似乎被甲骨文吞没了。

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

吉姆·S

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM