java 找出 JLabel 的文本是否超过标签大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7971178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find out if text of JLabel exceeds label size
提问by jutky
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.
在 Java 中,当 JLabel 的文本由于空间不足而无法显示时,文本会被截断并在末尾添加“...”。
How can I easily find out if currently JLabel displays full text or the truncated?
如何轻松确定当前 JLabel 显示的是全文还是截断的?
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.
我看到有一种方法可以使用FontMetrics
. 然而,这个解决方案并没有完全回答这个问题。在 JLabel 的文本包含 HTML 装饰的metrics.stringWidth()
情况下,还将计算 HTML 标签的宽度。因此,结果metrics.stringWidth()
可能会大于 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.
有没有办法知道在显示文本时 JLabel 本身做出了什么决定。它是否决定截断文本。
采纳答案by trashgod
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.
省略号由标签的 UI 委托添加,通常是 的子类BasicLabelUI
,作为其布局和首选大小计算的一部分。layoutCL()
可以重写该方法以检查几何,如本例所示。
As a practical matter, I'd ignore the elision and show the full text in a tool tip.
实际上,我会忽略省略并在工具提示中显示全文。
回答by Gilbert Le Blanc
From Oracle - Measuring Text:
来自 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);
Compare size
to the size of the JLabel.getSize()
;
比较size
到的大小JLabel.getSize()
;
回答by mre
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.
我想如果组件的首选大小大于它的实际大小,那么你可以期待截断。当然,为了使其工作,组件必须已经实现。
回答by Jim S.
回答by Kane
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.
获得显示字符串的大小后,您可以与 JLabel 的长度进行比较,看看标签是否太小。
回答by Serge
To get sizes of html text check this https://www.java.net/node/665691.
要获取 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);
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:
唯一的小问题是它可能有非 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());
}