JLabels变得非常小

时间:2013-04-02 11:06:56

标签: java swing user-interface gridbaglayout

我正在使用gridbaglayout作为我的Java项目,出于某种原因,当我启动程序时,所有JLabel似乎都非常小,即使我使用百分比调整它们的大小(因此大小应该是相对的)< / p>

以下是我用来设置首选尺寸的代码:

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);

2 个答案:

答案 0 :(得分:4)

我没有看到你在哪里设置任何约束权重和重量。尝试给它们默认值1.0。

c = new GridBagConstraints();

c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;

这有助于防止所有组件在中心碾压 是的,如果这没有帮助,那么创建并发布您的sscce

答案 1 :(得分:2)

不要设置首选宽度。使用weightxweighty

相关问题