从Java Swing的左上角开始GridBagLayout [英] Starting GridBagLayout from top left corner in Java Swing

查看:124
本文介绍了从Java Swing的左上角开始GridBagLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java Swing的新手,我一直在努力从左上角开始GridBagLayout,这样c.gridx = 0 c.gridy = 0会将我的对象放在左上角。

I'm new to Java Swing and I have been struggling to start the GridBagLayout from top left corner so that c.gridx=0 c.gridy=0 will put my object on the top left corner.

如果您能在此之后告诉我需要做什么,我将不胜感激:

I'd appreciate if you could help me by telling what I need to do after this point:

    JPanel panel = new JPanel(new GridBagLayout());
    frame.add(panel);
    GridBagConstraints c = new GridBagConstraints();

我知道我必须使用NORTHWEST或FIRST_LINE_START常量,但我不知道如何。我尝试这样做'但它没有意识到常量。

I know that I have to use NORTHWEST or FIRST_LINE_START constants, but I don't know how. I tried to do it this way' but it did not realize the constants.

    frame.getContentPane().add(panel, BorderLayout.NORTHWEST);

感谢您的帮助。

推荐答案

您需要使用 GridBagConstraints '属性。这应该适合你:

You need to use your GridBagConstraints' anchor property. This should do it for you:

frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
frame.add(panel, gbc);

我不保证你不必设置约束对象的其他属性来获取你想要的布局。特别是,您可能需要将 widthx widthy 设置为 1 以便面板占用给它的所有可用空间。

I'm not guaranteeing that you won't have to set other properties of the constraints object to get the layout you desire. In particular, you may need to set widthx and widthy to be 1 so that the panel takes up all of the available space given to it.

这篇关于从Java Swing的左上角开始GridBagLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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