GridBagLayout 填充
我正在使用 GridBagLayout(当前)显示两行。我知道这种布局对于这项任务来说太过分了,但我正在尝试学习如何使用它。问题是我已将两个面板添加到两个单独的行中,并且内容周围存在巨大间隙(请参见下面的图像和代码): 替代文本 http://www.imagechicken.com/uploads/1264533379009864500.png
Image background;
public Table(){
super();
ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTableV2.png"));
background = ii.getImage();
setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT);
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
JButton button = new JButton("hello world");
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(800,100));
panel1.add(button, BorderLayout.CENTER);
panel1.setBackground(Color.yellow);
add(panel1, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
JPanel middlePanel = new JPanel();
middlePanel.setPreferredSize(new Dimension(800,350));
middlePanel.add(button, BorderLayout.CENTER);
middlePanel.setBackground(Color.blue);
add(middlePanel, constraints);
}
I am using a GridBagLayout
to (currently) display two rows. I am aware this layout is overkill for this task, but am trying to learn how to use it. The problem is that I have added the two panels to the two separate rows and there is a huge gap around the content (see image and code below):
alt text http://www.imagechicken.com/uploads/1264533379009864500.png
Image background;
public Table(){
super();
ImageIcon ii = new ImageIcon(this.getClass().getResource("pokerTableV2.png"));
background = ii.getImage();
setSize(Constants.FRAME_WIDTH, Constants.TABLE_HEIGHT);
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
JButton button = new JButton("hello world");
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(800,100));
panel1.add(button, BorderLayout.CENTER);
panel1.setBackground(Color.yellow);
add(panel1, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
JPanel middlePanel = new JPanel();
middlePanel.setPreferredSize(new Dimension(800,350));
middlePanel.add(button, BorderLayout.CENTER);
middlePanel.setBackground(Color.blue);
add(middlePanel, constraints);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
JavaDoc 表示
weightx
/重量级
说:JavaDoc for
fill< /代码>:
Use
JavaDoc for
weightx
/weighty
says:JavaDoc for
fill
:不幸的是,对于 GridBagLayout,如果内容没有填满其所在的整个容器,它会自动将其所有内容在其容器内居中。这就是为什么你会出现很大的差距。
本质上有两种方法可以解决这个问题:
GridBagLayout
放在FlowLayout
中,然后将FlowLayout
的对齐方式设置为左上角,或者任何你想要的。上周我自己问过并回答了这个问题。
因此,在您的情况下,您将
panel1
和middlePanel
直接添加到 JFrame (?),并使用GridBagLayout
我建议使用这种替代结构,消除所有多余的空间(如果您愿意,也可以中心对齐)。
华泰
Unfortunately, with
GridBagLayout
, if the contents do not fill the entire container that it is in, it will automatically center all its contents within its container. That is why you are getting a really large gap.There are essentially two ways to fix this:
GridBagConstraints
. I had limited success with this when trying to avoid the centre-ing behaviour.GridBagLayout
inside of aFlowLayout
, and then set the alignment of theFlowLayout
to top-left, or whatever you wish.I have asked, and answered, this question myself sometime last week.
So in your case you are adding your
panel1
andmiddlePanel
directly to the JFrame (?), with aGridBagLayout
I would suggest this alternative structure instead, to get rid of all the extra space (and centre alignment as well, if you like).
HTH