在 Java Swing 中从左上角启动 GridBagLayout
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6364280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Starting GridBagLayout from top left corner in Java Swing
提问by Emir
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.
我是 Java Swing 的新手,我一直在努力从左上角开始 GridBagLayout,以便 c.gridx=0 c.gridy=0 将我的对象放在左上角。
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.
我知道我必须使用 NORTHWEST 或 FIRST_LINE_START 常量,但我不知道如何使用。我试图这样做'但它没有意识到常数。
frame.getContentPane().add(panel, BorderLayout.NORTHWEST);
Thanks for your help.
谢谢你的帮助。
采纳答案by Mark Peters
You need to use your GridBagConstraints
' anchor
property. This should do it for you:
您需要使用您的GridBagConstraints
'anchor
财产。这应该为你做:
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
frame.add(panel, gbc);
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
and weighty
to be 1
so that the panel takes up all of the available space given to it.
我不保证您不必设置约束对象的其他属性来获得您想要的布局。特别是,你可能需要设置weightx
并weighty
以1
使面板占用了所有给它的可用空间。
回答by camickr
Read the section from the Swing tutorial on How to Use GridBagLayout. The secton on "weightx,weighty" states:
阅读 Swing 教程中关于如何使用 GridBagLayout 的部分。“weightx,weighty”部分指出:
Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container.
除非您为 weightx 或 weighty 指定至少一个非零值,否则所有组件都会聚集在其容器的中心。
回答by peenut
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):
对于那些使用 IDE(例如 NetBeans)的人,我终于找到了一个不错的技巧:如果您想将组件添加到顶部并使用它们的首选大小:添加另一个 weighty = 1.0 的空面板。从自动生成的代码 (NetBeans) 复制:
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.weighty = 1.0;
jPanelOptions.add(jPanelFiller, gridBagConstraints);
回答by Jeremie D
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.
如果您希望网格一直到顶部,只需将所有 weighty = 0 设置为最后一项,将其设置为大于 0 的任何数字,它将有效地将其余按钮推到顶部。
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.)
否则会居中。(如果您不使用 c.fill = GridBagConstraints.HORIZONTAL 属性,则可以使用 gridx 和 weightx 值完成相同的操作。)
回答by mrd abd
a quick and simple way:
一种快速而简单的方法:
add a blank JLabel to end of page:
将空白 JLabel 添加到页面末尾:
// your code goes here
gbc.weightx = 1;
gbc.weighty = 1;
bg.add(new JLabel(" "), gbc); // blank JLabel
回答by AlpacaMan
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.
有一个解决方法。您可以使用 BorderLayout.NORTH 将 GridBagLayout 面板放在 BorderLayout 面板中。然后 GridBagLayout 面板中的组件将从顶部位置开始。
static void test4(){
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(480, 360);
JPanel borderLayoutPanel=new JPanel(new BorderLayout());
JPanel gridBagLayoutPanel = new JPanel(new GridBagLayout());
borderLayoutPanel.add(gridBagLayoutPanel, BorderLayout.NORTH);
frame.add(borderLayoutPanel);
JButton testButton=new JButton("test button");
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;
c.gridy=0;
gridBagLayoutPanel.add(testButton, c);
frame.setVisible(true);
}