将带有斜体字体样式的彩色字符串添加到工具提示文本

时间:2013-07-01 07:37:05

标签: java html swing tooltip jcomponent

如何将带斜体字体样式的彩色字符串添加到工具提示文字?

实际上我已经从Core java中的getToolTipText(MouseEvent e)覆盖了JComponent方法,并在其中添加了一些tooltiptext Strings,现在我想再添加一个StringItalic font and Red color。你能告诉我如何在Java中将彩色字符串添加到ToolTipText中,我尝试使用HTML但是我没有得到结果。

String message=  "<html><p style='font-style:italic;color:red;'> Minor </p> </html> ";

我在上面HTML尝试将'Minor'字符串添加到ToolTipText中,但它在ToolTipText中显示所有 HTML内容

1 个答案:

答案 0 :(得分:4)

  

..重写了getToolTipText(Mouseevent e)

为什么不设置工具提示文字?这个策略似乎在这里工作得很好。 E.G。

Styled ToolTip

import javax.swing.*;

class StyledToolTip {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                String s=  "<html><p style='font-style:italic;color:red;'>" + 
                        "Minor</p></html> ";
                JLabel lbl = new JLabel(s);
                lbl.setToolTipText(s);

                JOptionPane.showMessageDialog(null, lbl);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}