java 设置 JLabel 的宽度并在悬停时添加工具提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17314542/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:39:56  来源:igfitidea点击:

Set width of a JLabel and add a tooltip when hovering

javaswinguser-interfacejlabel

提问by Goatcat

I have a JLabelwith unknown content and I have two things I want to do:

我有一个JLabel未知内容,我有两件事想做:

  • I want to set a maximum or perhaps even static width of the label. And if the text is larger than the label it would somply shorten it, like this:
  • 我想设置标签的最大或什至静态宽度。如果文本大于标签,它会简单地缩短它,如下所示:

Verylonglabel

超长标签

becomes

变成

Veryl

非常

Is it a bad idea to use static width on components in a gui? If that is the case, what is the alternative? Please give me advice!

在 gui 中的组件上使用静态宽度是一个坏主意吗?如果是这种情况,有什么替代方案?请给我建议!

  • When you hover over the label I want a tooltip with the full length string to appear. So in our case, if i hover over the label that says "Veryl", a tooltip displaying "Verylonglabel" would appear. However, it should display a tooltip with the full length string even if it was not shortened.
  • 当您将鼠标悬停在标签上时,我希望出现带有全长字符串的工具提示。所以在我们的例子中,如果我将鼠标悬停在标有“Veryl”的标签上,则会出现一个显示“Verylonglabel”的工具提示。但是,它应该显示带有全长字符串的工具提示,即使它没有被缩短。

Help with either of these is greatly appreciated.

非常感谢您对其中任何一项的帮助。

So far I've just messed around a bit and tried things like this without sucess. It doesn't seem to care about the size at all.

到目前为止,我只是搞砸了一点,尝试过这样的事情但没有成功。它似乎根本不在乎大小。

JLabel label = new JLabel("Verylonglabel");     
label.setSize(15, 5);

Best regards, Goatcat

最好的问候,山羊猫

回答by Robin

The size of your JLabelis determined by the LayoutManagerof the parent container. Consult the tutorialfor more information.

您的大小JLabelLayoutManager父容器的大小决定。有关更多信息,请参阅教程

Note that the JLabelhas already the behavior you are looking for

请注意,JLabel已经具有您正在寻找的行为

  1. When the text is too long, the text which is cut-off will be replaced by "..." . So in your example, the "Verylonglabel" would be replaced by e.g. "Verylo..."
  2. You can use the setToolTipTextmethod to specify the tooltip, which will be shown when hovering over the JLabel
  1. 当文本太长时,被截断的文本将被替换为 "..." 。因此,在您的示例中,“Verylonglabel”将被替换为例如“Verylo...”
  2. 您可以使用setToolTipText方法指定工具提示,当鼠标悬停在JLabel

回答by ColinWa

This is what you need:

这是你需要的:

JLabel label = new JLabel("Verylonglabel");

// Create tool tip.
label.setToolTipText(label2.getText());

// Set the size of the label
label.setPreferredSize(new Dimension(80,40));// Width, Height

回答by Josh Britton

setMaximumSizewill only be effective if the LayoutManageryou use honors components' desired/max sizes. You may want to check out BoxLayoutas a LayoutManager. Unlike several other Swing LayoutManagers, BoxLayouthonors the size settings of its components.

setMaximumSize仅当LayoutManager您使用荣誉组件的所需/最大尺寸时才有效。你可能想看看BoxLayoutLayoutManager。与其他几个 Swing LayoutManager 不同BoxLayout,它遵循其组件的大小设置。

This still leaves you the question of what size to set as the maximum size. The width will be whatever you are targeting as your fixed value, but the height should be big enough for whatever font is being used. Here is the code I would use to make the height flexible but the width fixed to 50 pixels:

这仍然给您留下了将什么尺寸设置为最大尺寸的问题。宽度将是您作为固定值的目标,但高度应该足够大以适应正在使用的任何字体。这是我用来使高度灵活但宽度固定为 50 像素的代码:

label.setMaximumSize(new Dimension(10000, 50));

Finally, the tooltip will show the full label text with a line like:

最后,工具提示将使用如下一行显示完整的标签文本:

label.setToolTipText(labelText);