找出JLabel的文本是否超过标签大小 [英] find out if text of JLabel exceeds label size

查看:389
本文介绍了找出JLabel的文本是否超过标签大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,当由于空间不足而无法显示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.

如何轻松查明当前JLabel是显示全文还是截断?

How can I easily find out if currently JLabel displays full text or the truncated?

编辑:

我看到有一种方法可以通过 FontMetrics 找出文本的大小。然而,该解决方案并未完全回答这个问题。在JLabel文本包含HTML装饰的情况下, metrics.stringWidth()也会计算HTML标记的宽度。因此,可能会发生 metrics.stringWidth()的结果比JLabel的宽度更大,但文本仍会正确显示。

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.

有没有办法知道在显示文本时JLabel本身的决定。它是否决定截断文本。

Is there a way know what decision took the JLabel itself while displaying the text. Has it decided to truncate the text or not.

推荐答案

省略号由标签的UI委托添加,通常是<的子类a href =http://download.oracle.com/javase/7/docs/api/javax/swing/plaf/basic/BasicLabelUI.html\"rel =nofollow noreferrer> BasicLabelUI ,作为其布局和首选尺寸计算的一部分。可以重写方法 layoutCL()来检查几何体,如 example

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 method layoutCL() 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.

这篇关于找出JLabel的文本是否超过标签大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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