如何在调整大小时更改JLabel字体大小以填充JPanel可用空间? [英] How to change JLabel font size to fill JPanel free space while resizing?

查看:302
本文介绍了如何在调整大小时更改JLabel字体大小以填充JPanel可用空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个类似的问题:
如何更改JLabel字体的大小以获取最大大小

There is a similar question here: How to change the size of the font of a JLabel to take the maximum size

但是它不能向后工作。

所以如果你让 JPanel 更大,字体就会增长,但如果你使它变小 JLabel 大小和字体保持不变

So if you make JPanel bigger, font is growing, but if you make it smaller JLabel size and font remain as before

查看此图片

如何制作 Jlabel 在调整大小时根据 JPanel 字体增长字体?

How to make Jlabel font growing according to JPanel size while resizing?

推荐答案

<好的,这是答案。

让我们从这里获取代码:如何更改JLabel字体的大小以获取最大值ze ,来自 coobird的回答

Let's take the code from here: How to change the size of the font of a JLabel to take the maximum size, answer from coobird

int componentWidth = label.getWidth();

我们需要从 JFrame 获取宽度组件,而不是来自 JLabel ,因为代码不会让机会改变JLabel的大小。

We need to get width from the JFrame component, NOT from JLabel, because the code will no let opportunity to change size for the JLabel.

这将是修复它:

int componentWidth = this.getWidth()-20; // '20' - according width of Jlabel to JFrame

这篇关于如何在调整大小时更改JLabel字体大小以填充JPanel可用空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆