[英]GridBag Panel not Taking up All the Space
我正在尝试使用列中的标签创建行列表。 我正在尝试使用gridbaglayout,但我遇到了一个问题。 当我展开窗口时,它不会扩展。 这是发生的事情:
我真正想要的是布局中的单元格占据空间的1/5,标签移动到单元格的最左侧部分。
public static JPanel createLayout(int rows) {
JPanel product = new JPanel(new GridBagLayout());
String[] lables = {"School", "Advanced #", "Novice #"};
double weight = 1/(lables.length);
for (int i = 0; i < rows; i++) {
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(3, 3, 3, 3);
c.anchor = GridBagConstraints.WEST;
c.gridx = i;
c.weightx = weight;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.NORTHWEST;
for (int j = 0; j < lables.length; j++) {
JLabel l = new JLabel(lables[j]);
product.add(l, c);
}
}
return product;
}
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Debate Calculator");
JPanel debates = new JPanel();
frame.add(createLayout(5), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
我相信问题在于你的体重计算...
double weight = 1/(lables.length);
因为1
和lables.length
都是int值,所以Java会自动将结果转换为int
(为0
)。
相反,尝试像......
double weight = 1d/(double)lables.length;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.