【问题标题】:JLabels becoming really smallJLabels 变得非常小
【发布时间】:2013-03-23 15:42:06
【问题描述】:

我正在为我的 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);

【问题讨论】:

  • 为了获得更好的帮助,请尽快发布SSCCE,短可运行,可编译,几乎是问题,问题,为什么在这个世界上你要计算维度,在 LayoutManagers 中实现的 Swing 逻辑是 JComponents 返回自己的容器大小

标签: java swing user-interface gridbaglayout


【解决方案1】:

我看不到您在哪里设置任何约束 weightx 和 weighty。尝试给它们默认值 1.0。

c = new GridBagConstraints();

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

这将有助于防止所有组件在中心处起皱。
是的,如果这没有帮助,请创建并发布您的 sscce

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-13
      相关资源
      最近更新 更多