判断 JLabel 的文本是否超出标签大小
在 Java 中,当 JLabel 的文本由于空间不足而无法显示时,文本会被截断并在末尾添加“...”。
如何轻松了解 JLabel 当前显示的是全文还是截断内容?
编辑:
我发现有一种方法可以使用 FontMetrics
找出文本的大小。然而这个解决方案并没有完全回答这个问题。如果 JLabel 的文本包含 HTML 装饰,则 metrics.stringWidth()
还将计算 HTML 标签的宽度。因此,metrics.stringWidth()
的结果可能会大于 JLabel 的宽度,但文本仍会正确显示。
有没有办法知道 JLabel 在显示文本时做出了什么决定。它是否决定截断文本?
In Java when the text of the JLabel could not be displayed due to lack of space the text is truncated and "..." is added in the end.
How can I easily find out if currently JLabel displays full text or the truncated?
EDIT:
I see that there is a way to find out the size of the text by using FontMetrics
. However this solution doesn't fully answers the question. In the case the text of JLabel contains HTML decorations the metrics.stringWidth()
would also calculate width of HTML tags. So it could happen that result of metrics.stringWidth()
would be grater than JLabel's width but still the text would be displayed correctly.
Is there a way know what decision took the JLabel itself while displaying the text. Has it decided to truncate the text or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
省略号由标签的 UI 委托添加,通常是
BasicLabelUI
,作为其布局和首选尺寸计算的一部分。可以重写方法layoutCL()
来检查几何形状,如 示例。实际上,我会忽略省略并在工具提示中显示全文。
The ellipsis is added by the label's UI delegate, typically a subclass of
BasicLabelUI
, as part of it's layout and preferred size calculation. The methodlayoutCL()
may be overridden to examine the geometry, as shown on this example.As a practical matter, I'd ignore the elision and show the full text in a tool tip.
来自 Oracle - 测量文本:
比较
大小 为
JLabel.getSize()
的大小;From Oracle - Measuring Text:
Compare
size
to the size of theJLabel.getSize()
;我想如果组件的首选大小大于其实际大小,那么您可以预期会被截断。当然,为了使其工作,组件必须已经实现。
I suppose if the component's preferred size is greater than it's actual size, then you can expect truncation. In order for this to work, of course, the component must already be realized.
检查此并查看
layoutCompoundLabel ()
方法。它返回一个表示标签文本的字符串。您可以将其与原始图像进行比较,以确定是否会被剪辑。吉姆·S。
Check this and see the
layoutCompoundLabel()
method. It returns a String representing the text of the label. You can compare it to the original to determine if it will be clipped.Jim S.
通常的方法是使用一种方法来计算文本在标签中显示的预期大小。如果您使用等宽字体,这很容易:
如果您不使用等宽字体,则显然要困难得多。我认为周围有一些实用程序会尝试计算这个。
获得显示字符串的大小后,您可以与 JLabel 的长度进行比较,看看标签是否太小。
The usual way to do this is to use a method that calculates the expected size of the text as it will be displayed in the label. If you're using a monospaced font, this is easy:
If you're not using a monospaced font, it's obviously much harder. I think there are some utilities around that will attempted to calculate this.
Once you have the size of the displayed string, you can compare to the length of the JLabel and see if the label is too small.
要获取 html 文本的大小,请检查此 https://www.java.net/node/665691 。
唯一的小问题是它可能会出现非 html 文本的问题。因此,只需对非 html 字符串使用字体规格即可。以下对我来说非常有效:
To get sizes of html text check this https://www.java.net/node/665691.
The only small problem is that it might have issues with non-html text. So, just use font metrics for non-html strings. The following worked perfectly for me: