我正在为我的 Java 项目使用 gridbaglayout,由于某种原因,当我启动程序时,所有 JLabel 似乎都非常小,即使我已经使用百分比调整了它们的大小(因此大小应该是相对的)
这是我用来设置首选大小的代码:
int stswidth = (int)(((float)66/100) * getWidth()), stsheight = (int)(((float)65/100) * getHeight());
int navwidth = (int)(((float)21.5f/100) * getWidth()), navheight = (int)(((float)10/100) * getHeight());
int clwidth = (int)(((float)25/100) * getWidth()), clheight = (int)(((float)80/100) * getHeight());
int hwidth = (int)(((float)100/100) * getWidth()), hheight = (int)(((float)20/100) * getHeight());
这是使用网格包约束设置每个标签位置的代码:
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
core.add(sts[1], c);
//nav
c.gridx = 0;
c.gridy = 0;
nav.add(navb[0], c);
c.gridx = 1;
c.gridy = 0;
nav.add(navb[1], c);
c.gridx = 2;
c.gridy = 0;
nav.add(navb[2], c);
//cl
c.gridx = 0;
c.gridy = 0;
clock.add(cl, c);
head.add(header, c);