fer*_*rck 7 html java swing tooltip
我想在我的swing组件中添加多行工具提示消息.基本的解决方案是使用这样的html标签:
label.setToolTipText("<html><p width=\"350px\">" + text + "</p></html>");
Run Code Online (Sandbox Code Playgroud)
它适用于长文本.但是,如果文本,包括只包含一个单词,它也将固定350px宽度,并且有很多空白空间.
max-width是否有任何特殊的html属性可以使用setToolTipText()方法正常工作?我尝试了下一个解决方案,但它不起作用:
"<p style=\"max-width: 350px\">"
Run Code Online (Sandbox Code Playgroud)
<p>
如果宽度小于350 像素,我应该以像素为单位计算文字宽度并更改开始标记吗?
Luk*_*uke -2
我建议如下:
设置固定宽度后,您可以使用以下方法获得首选尺寸:
Dimension d = someToolTip.getPreferredSize();
if (d.width < 350) {
// Insert code to make tooltip-size smaller, by setting it to d.width;
}
Run Code Online (Sandbox Code Playgroud)