总所周知,Swing大多控件是可以应用HTML formatting特性的例如JButton button = new JButton("<html><font face='Serif' size='10' color='red'><b><u>B</u></b>utton</font></html>");或者对于 JLayeredPane :
JLayeredPane layeredPane = new JLayeredPane(); 
layeredPane.setBorder(BorderFactory.createTitledBorder("<html><b><i><u><font face='SansSerif' size='5' color='red'>layeredPane</font></u></i></b></html>"));它们都可以以HTML的特性显示在界面上。所以现在有个疑问,若是自己画图,比如自己在JButton上画图,怎样把String text = "<html><font face='Serif' size='10' color='red'><b><u>B</u></b>utton</font></html>"这样的String画出来,Graphics2D.drawString(String text)好像不能处理这样的带HTML的String,求高手解答!!!

解决方案 »

  1.   


    这个真心不熟悉,……Swing对html的处理,好像是集成在 LookAndFeel 机制之中的,藏的很深可以去翻一翻一些 LookAndFeel 的 某种控件的 UI 类的代码,或许能找到一些线索比如 javax.swing.plaf.basic.BasicLabelUI 的代码:http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/plaf/basic/BasicLabelUI.javaJComponent 绘图的时候,对UI类的调用是形似这样的: getUI().update(g, this);
      

  2.   

    那就不管html了。反正我找到了另一种方法重绘JButton时可以避免画其上面的Text:使用Nimbus的个性化组件方法。其效果和重写JButton的paintComponent()一样:
    重写JButton的paintComponent():
    (其HTML特性消失)使用Nimbus的个性化组件方法:
    (保留了Swing的HTML特性)详细请参看:Java 重新绘制JButton