BorderLayout.CENTER 上 GridBagLayout 面板的垂直对齐
我想做的是将 GridBagLayout 面板放在 BorderLayout 的中心,并将 GridBagLayout 面板(/和上面的文本)垂直对齐到顶部(因为它会自动将其水平和垂直地放在中间)。
所以我基本上尝试过(但最终 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);
}
}
所以我尝试使用 gridbagconstraints 锚点将对齐方式设置为垂直向北,顶部,但这似乎不起作用。我还尝试调整 GridBagLayout 面板本身的大小(使用 panel.setSize 和 setPreferredSize 使其具有布局的完整高度,100%),然后使用 gridbagconstraints.anchor 垂直对齐其上的元素,但这不起作用任何一个。
谁能帮我解决这个问题吗?
预先致谢,
最诚挚的问候, 斯凯夫。
所以我的问题是
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 ).
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):
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.
Can anyone help me out on this?
Thanks in advance,
Best Regards,
Skyfe.
So my question is
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 GridBagConstraints 类的每个属性之前,请仔细查看其 Javadoc。
Take a careful look at the Javadoc of each property of the
GridBagConstraints
class before using it.除了将
fill
设置为NONE 之外,还将weighty
设置为非0 值(例如1.0)。这告诉布局将 GridBag 列中的所有额外空间分配给该特定单元格,但由于标签本身锚定在北方并设置为不填充,因此额外空间将分配在标签下方。In addition to setting
fill
to NONE, also setweighty
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.您
正在使组件(“内容”)垂直填充 GB 。这可能就是为什么您认为您的组件(内容)仍在中间。尝试将其设置为 GridBagConstraints.NONE 。
这会将您的内容与具有 GridBagLayout 的面板内的顶部对齐。
对于外部组件,您始终可以使用类似以下内容:
并查看它是否有效。
Your
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.
for the outer component, you can always use something like:
and see if it works.