Java BorderLayout.CENTER 上 GridBagLayout 面板的垂直对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4135680/
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
Vertical Align of GridBagLayout Panel on BorderLayout.CENTER
提问by Skyfe
What I'm trying to do is place a GridBagLayout Panel on the center of my BorderLayout and vertical align the GridBagLayout panel ( /and text on it ) to the TOP ( because it automaticly puts it in the middle, horizontally AND vertically ).
我想要做的是在我的 BorderLayout 的中心放置一个 GridBagLayout 面板,并将 GridBagLayout 面板(/和上面的文本)垂直对齐到顶部(因为它会自动将它放在中间,水平和垂直)。
So what I basicly tried ( but ended up having the text of the GridBagLayout still in the total middle of the page instead of in the middle x and top y):
所以我基本上尝试过(但最终使 GridBagLayout 的文本仍然在页面的中间,而不是在中间 x 和顶部 y):
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import javax.imageio.*;
import javax.swing.BorderFactory;
import javax.swing.border.*;
import java.awt.event.*;
public class Test extends JApplet implements MouseListener, ActionListener {
public void init() {
//create borderlayout
this.setLayout(new BorderLayout());
//create a GridBagLayout panel
JPanel gb = new JPanel(new GridBagLayout());
JLabel content = new JLabel("Some text");
//set GridBagConstraints (gridx,gridy,fill,anchor)
setGBC(0, 0, GridBagConstraints.VERTICAL, GridBagConstraints.NORTH);
gb.add(content, gbc); //gbc is containing the GridBagConstraints
this.add(gb, BorderLayout.CENTER);
}
}
So I tried to use the gridbagconstraints anchor to set the alignment vertically to the north, top, but that seems not to work. I also tried to resize the GridBagLayout panel itself ( to make it have full height of the layout, 100%, using panel.setSize and setPreferredSize ) and then vertically align the elements on it using the gridbagconstraints.anchor, but that didn't work either.
因此,我尝试使用 gridbagconstraints 锚点将对齐垂直设置为北、顶,但这似乎不起作用。我还尝试调整 GridBagLayout 面板本身的大小(使其具有布局的完整高度,100%,使用 panel.setSize 和 setPreferredSize ),然后使用 gridbagconstraints.anchor 垂直对齐其上的元素,但这不起作用任何一个。
Can anyone help me out on this?
任何人都可以帮我解决这个问题吗?
Thanks in advance,
提前致谢,
Best Regards, Skyfe.
最好的问候, Skyfe。
So my question is
所以我的问题是
采纳答案by Montecarlo
Take a careful look at the Javadoc of each property of the GridBagConstraints
class before using it.
使用前请仔细查看GridBagConstraints
该类的每个属性的Javadoc 。
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
public static void main(String arg[]) {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel gb = new JPanel(new GridBagLayout());
JLabel content = new JLabel("Some text");
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTH;
gbc.weighty = 1;
gb.add(content, gbc); // gbc is containing the GridBagConstraints
frame.add(gb, BorderLayout.CENTER);
frame.setVisible(true);
}
}
回答by Skyfe
Your
您的
setGBC(0, 0, GridBagConstraints.VERTICAL, GridBagConstraints.NORTH);
is making the component ("content") filling gb vertically. This may be why you think your component(content) is still in the middle. Try setting it to GridBagConstraints.NONE
.
This will align your content to the top inside the panel that has GridBagLayout.
正在使组件(“内容”)垂直填充 gb。这可能就是为什么您认为您的组件(内容)仍在中间的原因。尝试将其设置为GridBagConstraints.NONE
. 这会将您的内容与具有 GridBagLayout 的面板内的顶部对齐。
for the outer component, you can always use something like:
对于外部组件,您始终可以使用以下内容:
panel.add(button, BorderLayout.PAGE_START);
and see if it works.
看看它是否有效。
回答by jwaddell
In addition to setting fill
to NONE, also set weighty
to a non-0 value (e.g. 1.0). This tells the layout to allocate all extra space in the GridBag column to that particular cell, but since the label itself is anchored to the north and set to not fill, the extra space will be allocated beneath the label.
除了设置fill
为 NONE 之外,还要设置weighty
为非 0 值(例如 1.0)。这告诉布局将 GridBag 列中的所有额外空间分配给该特定单元格,但由于标签本身锚定在北方并设置为不填充,因此额外空间将分配在标签下方。