如何在工具提示文本中添加带有斜体字体样式的彩色字符串?
实际上,我已经覆盖了 Core java 中 JComponent
中的 getToolTipText(MouseEvent e)
方法,并在其中添加了一些工具提示文本 Strings
,现在我想用 Italic font and Red color
添加一个 String
给它。你能告诉我如何在 Java 中向 ToolTipText 添加彩色字符串吗,我尝试使用 HTML 但没有得到结果。
String message= "<html><p style='font-style:italic;color:red;'> Minor </p> </html> ";
我在上面的 HTML
中尝试将 'Minor' 字符串添加到 ToolTipText 中,但它显示了 ToolTipText 中的所有 HTML 内容。
最佳答案
..overridden the
getToolTipText(Mouseevent e)
为什么不直接设置工具提示文本?一种似乎在这里运作良好的策略。例如
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);
}
}
关于java - 向工具提示文本添加带有斜体字体样式的彩色字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17399475/