使用 JLabel 和 JPanel 时出现问题

发布于 2025-01-20 04:06:38 字数 1468 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2025-01-27 04:06:38

让我们查看标签与sesion面板之间的关系...

JLabel label = new JLabel("TEXT");
label.setFont(customFont.deriveFont(70f));
c.weighty = 0.4;
c.insets = new Insets(20, 20, 70, 20);
c.anchor = GridBagConstraints.PAGE_END;
right.add(label, c);

c = new GridBagConstraints();
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(0, 100, 150, 100);
right.add(sesion, c);

因此,标签使用flowsy0.4,并且面板为使用1.0的heighty,这表明您需要140%的可用空间

Lets look at the relationship between the label and the sesion panel...

JLabel label = new JLabel("TEXT");
label.setFont(customFont.deriveFont(70f));
c.weighty = 0.4;
c.insets = new Insets(20, 20, 70, 20);
c.anchor = GridBagConstraints.PAGE_END;
right.add(label, c);

c = new GridBagConstraints();
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(0, 100, 150, 100);
right.add(sesion, c);

So, the label is using a weighty of 0.4 and the panel is using weighty of 1.0, which is suggesting that you need 140% of the available space ????

So, what you actually want to do, is balance the weighty value of both components, for example weighty = 0.5, for example...

JLabel label = new JLabel("TEXT");
label.setFont(customFont.deriveFont(70f));
c.weighty = 0.5;
c.insets = new Insets(20, 20, 70, 20);
c.anchor = GridBagConstraints.PAGE_END;
right.add(label, c);

c = new GridBagConstraints();
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(0, 100, 150, 100);
right.add(sesion, c);

So, if we modify the existing code, removing the insets, for example...

JLabel label = new JLabel("TEXT");
c.weighty = 0.5;
c.anchor = GridBagConstraints.PAGE_END;
right.add(label, c);

JPanel sesion = new JPanel();
sesion.setBackground(new Color(174, 182, 112));

c = new GridBagConstraints();
c.gridy = 1;
c.weightx = 1;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
right.add(sesion, c);

We can end up with a screen looking something more like...

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文