JLabel 可以有多小? [英] How small can that JLabel be?

查看:28
本文介绍了JLabel 可以有多小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JLabel,我需要在屏幕外绘制一些应用到 Graphics 对象的变换.

I have a JLabel that I need to draw off screen with some Transforms applied to the Graphics object.

我正在尝试根据文本内容将 JLabel 做得尽可能小,而没有省略号.

I'm trying to make the JLabel as small as it can be without ellipsis depending on its text content.

我想做的是:

setBounds(0, 0, label.getMinimumSize().width, label.getMinimumSize().height);

有时,这会产生省略号,有时会产生省略号.这实际上取决于所应用的变换(通常是动画中的任意缩放).

Sometimes, this yields ellipsis and sometimes now. It really depends on the transform being applied (usually arbitrary scaling in an animation).

我是否遗漏了一些我应该考虑的最小尺寸"的明显内容,我是否遇到了一些四舍五入的四舍五入错误等?

Am I missing something obvious with what I should consider "Minimum Size", am I getting some rounding errors that are rounding down, etc?

请指出正确的方向.

推荐答案

label.getMinimumSize() 由 UI 委托计算,更多地与布局及其镶边有关,而不是与它的内容有关.也许您可以尝试根据文本计算大小应该是多少?

The label.getMinimumSize() is computed by the UI delegate and has more to do with the layout and its chrome rather than its content. Maybe you can try computing what the size should be based upon the text?

有点像……

JLabel label = new JLabel("xx");
int minWidth = ((int) (label.getFontMetrics(label.getFont()).getStringBounds(label.getText(),label.getGraphics()).getWidth()) + label.getInsets().left + label.getInsets().right);

高度相似.

这篇关于JLabel 可以有多小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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