在 Java Swing 中从左上角启动 GridBagLayout
我是 Java Swing 的新手,我一直在努力从左上角启动 GridBagLayout,以便 c.gridx=0 c.gridy=0 将我的对象放在左上角。
如果您能告诉我在此之后需要做什么来帮助我,我将不胜感激:
JPanel panel = new JPanel(new GridBagLayout());
frame.add(panel);
GridBagConstraints c = new GridBagConstraints();
我知道我必须使用 NORTHWEST 或 FIRST_LINE_START 常量,但我不知道如何使用。我尝试这样做'但它没有意识到常数。
frame.getContentPane().add(panel, BorderLayout.NORTHWEST);
感谢您的帮助。
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();
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);
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
阅读 Swing 教程中关于如何使用 GridBagLayout 的部分。 “weightx,weighty”部分指出:
除非您为weightx 或weighty 指定至少一个非零值,否则所有组件都会在其容器的中心聚集在一起。
Read the section from the Swing tutorial on How to Use GridBagLayout. The secton on "weightx,weighty" states:
Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container.
您需要使用
GridBagConstraints
'anchor
属性。这应该适合您:我不保证您不必设置约束对象的其他属性来获得您想要的布局。特别是,您可能需要将
weightx
和weighty
设置为1
,以便面板占用为其提供的所有可用空间。You need to use your
GridBagConstraints
'anchor
property. This should do it for you: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
weightx
andweighty
to be1
so that the panel takes up all of the available space given to it.一种快速简单的方法:
在页面末尾添加一个空白的 JLabel:
a quick and simple way:
add a blank JLabel to end of page:
对于那些使用 IDE(例如 NetBeans)的人来说,我终于找到了一个不错的技巧:如果您想将组件添加到顶部并使用其首选尺寸:添加另一个空面板,权重 = 1.0。从自动生成的代码 (NetBeans) 复制:
For those, who use IDE (e.g. NetBeans), I finally found nice trick: if you want to add components to top and use their preferred sizes: add another empty panel with weighty = 1.0. Copied from auto-generated code (NetBeans):
有一个解决方法。您可以将 GridBagLayout 面板放入具有 BorderLayout.NORTH 的 BorderLayout 面板中。然后 GridBagLayout 面板中的组件将从顶部位置开始。
There's a workaround. You can put the GridBagLayout panel in a BorderLayout panel with BorderLayout.NORTH. Then Component in GridBagLayout panel will start from top position.
如果你想让网格一直到顶部,只需将所有的权重设置为0,直到最后一项,将其设置为任何大于0的数字,它将有效地将其余按钮推到顶部。
不要忘记增加按钮的网格值。
否则就会居中。
(如果您不使用 c.fill = GridBagConstraints.HORIZONTAL 属性,则可以使用 gridx 和 Weightx 值完成相同的操作。)
if you want the grid to go all the way to the top, simply set all your weighty = 0 until the last item, set it to any number greater than 0, it will effectively push the rest of the buttons to the top.
Don't forget to also increment your button's gridy value.
Otherwise it will be centered.
(the same can be done using gridx and weightx value if you arent using the c.fill = GridBagConstraints.HORIZONTAL property.)