我有以下几行内容来获取JList项目的工具提示文本:
JList aList=new JList(aData)
{
public String getToolTipText(MouseEvent evt) // This method is called as the cursor moves within the list.
{
String tooltipText="Some tooltip";
int tooltipWidth= ?
return tooltipText;
}
}
在getToolTipText()中,如何获取tooltipText宽度?
解决方法:
I form my tooltip in html like this :
"<html>first line<Br>========<Br>second line</html>"
, I want the separator line"====="
to match the length of the first line, so it can look nicer, ..
考虑替代方案2和3,两者都不需要计算,并且看上去比“等号行”更好.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class HtmlToolTip {
HtmlToolTip() {
String attempt1 = "<html>first line 1<Br>========<Br>second line</html>";
JLabel label1 = new JLabel(attempt1);
label1.setBorder(new LineBorder(Color.BLACK));
String attempt2 = "<html><u>first line 2</u><br>second line</html>";
JLabel label2 = new JLabel(attempt2);
label2.setBorder(new LineBorder(Color.BLACK));
String attempt3 = "<html>first line 3<hr>second line</html>";
JLabel label3 = new JLabel(attempt3);
label3.setBorder(new LineBorder(Color.BLACK));
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEADING,5,5));
p.add(label1);
p.add(label2);
p.add(label3);
JOptionPane.showMessageDialog(null, p);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new HtmlToolTip();
}
});
}
}