症状:
1. 在eclipse调试运行时的一个JTextPane显示的是部分红色,部分黑色,现在在命令行用JDK运行后,不显示红色了,所有东西都是黑色。2. 我把程序拿到另一个同事的机子去运行,也是在eclipse下,也是黑色。是了,我的界面是从数据库定时刷新得到信息的,用的是一个Timer控件,程序刚启动的时候显示颜色正常,但刷新后就都是黑色了,究竟是为什么呢?请各位帮帮忙,先谢谢了。

解决方案 »

  1.   

    不好意思,一直没机会上网,请各位高手帮忙看看,
    以下是我使用的TextPane,每次我都调用addText方法,指定好颜色。
    在我的开发机器的eclipse上调试运行很正常,但是自己用JDK编译或者是部署到别的机器上时就全部都是黑色了,public class MyTextPane extends JTextPane{
    StyleContext sc;
    DefaultStyledDocument doc; public MyTextPane(){
    super();
    sc=StyleContext.getDefaultStyleContext();
    doc=new DefaultStyledDocument(sc);
    this.setDocument(doc);
    }

    public int addText(Color c,String s,int startIndex){
    AttributeSet aSet=sc.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.Foreground,c);
    setSelectionStart(startIndex);
    setSelectionEnd(doc.getLength());
    setCharacterAttributes(aSet,true);
    replaceSelection(s); return doc.getLength();
    }
    }