为什么GridBagLayout将我的组件放在中心而不是放在角落? [英] Why does GridBagLayout center my components instead of putting it in the corner?

查看:177
本文介绍了为什么GridBagLayout将我的组件放在中心而不是放在角落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我设法尽可能避免使用 GridBagLayout (手动编码),但这次我无法避免,我正在阅读SUN的教程 GridBagLayout
到目前为止还不顺利。我想我错了理解。

例如我尝试下面的代码(类似于SUN的帖子中的代码):

So far I managed to avoid using the GridBagLayout (by hand code) as much as possible, but I could not avoid it this time and I am reading the SUN's tutorial GridBagLayout So far it is not going well. I think I am missunderstanding something.
For example I try the following code (similar to the one in SUN's post):

public class MainFrame extends JFrame { 


    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame
     */
    public MainFrame() {
        super();
        setBounds(100, 100, 500, 375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container mainContainer = getContentPane();

        mainContainer.setLayout(new GridBagLayout());       

        //add label
        JLabel someLabel = new JLabel("Label 1:");
        GridBagConstraints constraints = new GridBagConstraints();

        constraints.gridx = 0;
        constraints.gridy = 0;
        //constraints.anchor = GridBagConstraints.FIRST_LINE_START;
        //constraints.weightx = 0.5;
        mainContainer.add(someLabel, constraints);      

        JTextField someText = new JTextField(30);

        constraints = new GridBagConstraints();

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.weightx = 0.5;
        mainContainer.add(someText, constraints);

        //
    }

}

我在框架的中心中将标签和文本字段放在另一个旁边。

但我希望它们会显示在左上角因为标签的gridx和gridy是0。

即使我设置 constraints.anchor = GridBagConstraints.FIRST_LINE_START; 仍然是相同的结果。 >
我错了吗?

来自SUN的帖子:

I get the label and the textfield one next to the other in the center of the frame.
But I was expecting that they would show up in the top left corner since the gridx and gridy is 0 for the label.
Even if I set constraints.anchor = GridBagConstraints.FIRST_LINE_START; still the same result.
Am I wrong here?
From the SUN's post:


指定行和列组件的左上角。最左边的
列的地址为gridx = 0,顶行的地址为
gridy = 0.

Specify the row and column at the upper left of the component. The leftmost column has address gridx=0 and the top row has address gridy=0.


推荐答案

constraints.weighty = 1; 添加到JLabel约束和 constraints.anchor = GridBagConstraints.NORTHWEST; 到TextField约束。

Add constraints.weighty = 1; to the JLabel constraints and constraints.anchor = GridBagConstraints.NORTHWEST; to the TextField constraints.

编辑:

来自Oracle的 GridBagLayout指南


更大的数字表示组件的行或列应该获得更多空间。对于每列,权重与为该列中的组件指定的最高权重x相关,每个多列组件的权重在组件所在的列之间以某种方式分割。类似地,每行的权重与为a指定的最高权重相关。该行中的组件。额外的空间倾向于最右边的列和底行。

Larger numbers indicate that the component's row or column should get more space. For each column, the weight is related to the highest weightx specified for a component within that column, with each multicolumn component's weight being split somehow between the columns the component is in. Similarly, each row's weight is related to the highest weighty specified for a component within that row. Extra space tends to go toward the rightmost column and bottom row.

这篇关于为什么GridBagLayout将我的组件放在中心而不是放在角落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆