【发布时间】:2018-08-08 11:18:28
【问题描述】:
我使用 Swing 已经有一段时间了,我终于找到了学习 GridBadLayout 的自信。
我仍在学习它,在这种情况下,我无法理解为什么以下代码没有按预期响应:特别是我无法理解为什么布局会以这种方式显示列。
通过运行 sn-p,您将发现代表意大利国旗的面板位置不正确:最后一列(国旗的红色部分)与国旗的其余部分(国旗的白色部分)分离.那么,我做错了什么,我可以修复什么来代表正确的标志?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test extends JPanel {
private GridBagConstraints gbc;
private final int CELL_WIDTH = 90;
private final int CELL_HEIGHT = 110;
public test() {
setLayout(new GridBagLayout());
putBanner(0);
putFlagRow(1);
putFlagRow(2);
putFlagRow(3);
putFlagRow(4);
}
public JPanel getUserPanel(Color c) {
JPanel ol = new JPanel();
ol.setPreferredSize(new Dimension(CELL_WIDTH * 10, CELL_HEIGHT));
ol.setBackground(c);
return ol;
}
public JPanel gettestPanel() {
JPanel ol = new JPanel();
ol.setPreferredSize(new Dimension(CELL_WIDTH, CELL_HEIGHT));
ol.setBackground(Color.white);
return ol;
}
private void putFlagRow(int gridy) {
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = gridy;
JPanel p1=gettestPanel();
p1.setBackground(Color.green);
add(p1, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 4;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 5;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 6;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 7;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 8;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 9;
gbc.gridy = gridy;
JPanel p=gettestPanel();
p.setBackground(Color.red);
add(p, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 10;
gbc.gridy = gridy;
add(gettestPanel(), gbc);
}
private void putBanner(int gridy) {
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = gridy;
gbc.gridwidth = 1;
add(gettestPanel(), gbc);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = gridy;
gbc.gridwidth = 9;
add(getUserPanel(Color.black), gbc);
}
public static void main(String[] args) {
JFrame frame = new JFrame("GridBagLayoutTest");
frame.add(new test());
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
除了了解这个特定问题及其原因之外,我还希望了解这一点,这直接来自 GridBagLayout Oracle 文档:
weightx,有重量 指定权重是一门艺术,它可以对 GridBagLayout 控件的组件的外观产生重大影响。重量是 用于确定如何在列之间分配空间 (weightx) 和 行间(重量级);这对于指定调整大小很重要 行为。除非您为 weightx 指定至少一个非零值 或沉重,所有组件都聚集在它们的中心 容器。这是因为当权重为 0.0(默认值)时, GridBagLayout 在其单元格网格和 容器的边缘。通常权重指定为 0.0 和 1.0 作为极端:根据需要使用中间的数字。较大的数字表示组件的行或列应该得到 更多空间。对于每一列,权重与最高的相关 为该列中的组件指定的 weightx,每个 多列组件的权重在列之间以某种方式分配 组件在其中。类似地,每一行的权重都与 为该行中的组件指定的最高权重。额外空间 倾向于走向最右边的列和底行。
像
这样的表达是一门艺术
和
根据需要使用中间的数字
确实给我的印象是,即使是文档作者也不认为可以教授这种约束的用法。这也不好
【问题讨论】:
标签: java swing layout-manager gridbaglayout