简体   繁体   English

确定是否已切断JLabel文本?

[英]Determine Whether JLabel Text is Being Cut Off?

I have a JLabel, and as the JFrame window expands, the font size automatically changes. 我有一个JLabel,随着JFrame窗口的扩展,字体大小会自动更改。 The text that is in the JLabel can either be abbreviated or put in full text like so: JLabel中的文本可以缩写或以全文形式输入,如下所示:

大小窗口示例

Because I am using Eclipse's "AbsoluteLayout" for Swing ( frame.getContentPane().setLayout(null) ), JLabels won't automatically resize to the size of the texts and are fixed values. 因为我在Swing中使用Eclipse的“ AbsoluteLayout”( frame.getContentPane().setLayout(null) ), frame.getContentPane().setLayout(null)不会自动将其大小调整为文本大小,并且是固定值。 Note that I do change those fixed values as the window changes size. 请注意,随着窗口更改大小,我确实更改了这些固定值。 I already have an algorithm for this. 我已经有一个算法了。

Instead of doing a large calculation determining whether the window is large enough to display the un-abreviated text, I would like to know if there is a way to determine whether my text would appear like this: 我想进行大量的计算,而不是确定窗口是否足够大以显示未缩写的文本,而是想知道是否有一种方法可以确定我的文本是否显示如下:

剪下JLabel文字

If I could know that the text would be cut off (due to long text in a small JLabel), I could abbreviate the text, otherwise display it in full. 如果我知道文本会被截断(由于在小JLabel中的长文本),则可以缩写文本,否则将其完整显示。

Is there any way to do what I'm asking while still using the AbsoluteLayout? 在仍然使用AbsoluteLayout的同时,有什么方法可以做我要问的吗?

Note: It is necessary that I use AbsoluteLayout, so don't bother commenting about it even though it is typically regarded as bad practice. 注意:我必须使用AbsoluteLayout,因此即使通常认为它是不好的做法,也不要理会它。

If I could know that the text would be cut off (due to long text in a small JLabel), I could abbreviate the text, otherwise display it in full. 如果我知道文本会被截断(由于在小JLabel中的长文本),则可以缩写文本,否则将其完整显示。

Compare the width of getPreferredSize() and getSize() . 比较getPreferredSize()getSize()的宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM