判断 JLabel 的文本是否超出标签大小

发布于 2024-12-13 07:04:36 字数 383 浏览 2 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(6

静若繁花 2024-12-20 07:04:36

省略号由标签的 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 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.

骄傲 2024-12-20 07:04:36

来自 Oracle - 测量文本

// get metrics from the graphics
FontMetrics metrics = graphics.getFontMetrics(font);
// get the height of a line of text in this font and render context
int hgt = metrics.getHeight();
// get the advance of my text in this font and render context
int adv = metrics.stringWidth(text);
// calculate the size of a box to hold the text with some padding.
Dimension size = new Dimension(adv+2, hgt+2);

比较大小 为 JLabel.getSize() 的大小;

From Oracle - Measuring Text:

// get metrics from the graphics
FontMetrics metrics = graphics.getFontMetrics(font);
// get the height of a line of text in this font and render context
int hgt = metrics.getHeight();
// get the advance of my text in this font and render context
int adv = metrics.stringWidth(text);
// calculate the size of a box to hold the text with some padding.
Dimension size = new Dimension(adv+2, hgt+2);

Compare size to the size of the JLabel.getSize();

霊感 2024-12-20 07:04:36

我想如果组件的首选大小大于其实际大小,那么您可以预期会被截断。当然,为了使其工作,组件必须已经实现

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.

尬尬 2024-12-20 07:04:36

检查并查看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.

Spring初心 2024-12-20 07:04:36

通常的方法是使用一种方法来计算文本在标签中显示的预期大小。如果您使用等宽字体,这很容易:

lengthOfChar * numChars

如果您不使用等宽字体,则显然要困难得多。我认为周围有一些实用程序会尝试计算这个。

获得显示字符串的大小后,您可以与 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:

lengthOfChar * numChars

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.

冧九 2024-12-20 07:04:36

要获取 html 文本的大小,请检查此 https://www.java.net/node/665691

View view = (View) javax.swing.plaf.basic.BasicHTML.createHTMLView(label, value.toString());
int width = (int) view.getPreferredSpan(View.X_AXIS);
int height = (int) view.getPreferredSpan(View.Y_AXIS);

唯一的小问题是它可能会出现非 html 文本的问题。因此,只需对非 html 字符串使用字体规格即可。以下对我来说非常有效:

if (value.toString().startsWith("<html>")) {
   View view = (View) javax.swing.plaf.basic.BasicHTML.createHTMLView(label, value.toString());
   width = (int) view.getPreferredSpan(View.X_AXIS);
} 
else {
   width =  (int) label.getFontMetrics(label.getFont()).stringWidth(value.toString());
}

To get sizes of html text check this https://www.java.net/node/665691.

View view = (View) javax.swing.plaf.basic.BasicHTML.createHTMLView(label, value.toString());
int width = (int) view.getPreferredSpan(View.X_AXIS);
int height = (int) view.getPreferredSpan(View.Y_AXIS);

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:

if (value.toString().startsWith("<html>")) {
   View view = (View) javax.swing.plaf.basic.BasicHTML.createHTMLView(label, value.toString());
   width = (int) view.getPreferredSpan(View.X_AXIS);
} 
else {
   width =  (int) label.getFontMetrics(label.getFont()).stringWidth(value.toString());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文