当前位置:编程学习 > JAVA >>

JTextPane 中如何设置背景颜色并得到html代码?

我在敲一个文本编辑器,用的是JTextPane,然后发现StyledEditorKit下面并没有设置背景色的方法。 

后来我用这样的方法来设置背景色 

SimpleAttributeSet aSet = new SimpleAttributeSet();  
StyleConstants.setBackground(aSet, color);  
StyledDocument doc = textPane.getStyledDocument();  
doc.setCharacterAttributes(textPane.getSelectionStart(),textPane.getSelectionEnd()- textPane.getSelectionStart(), aSet, false);  


这样的是能够显示出来效果,但是并不能通过textPane.getText();的方式得到html代码。 

本来我还看到一个方式说是可以得到: 

class bgAction extends StyledEditorKit.StyledTextAction {  
  
    public bgAction(String arg0) {  
        super(arg0);  
        // TODO Auto-generated constructor stub  
    }  
  
    @Override  
    public void actionPerformed(ActionEvent arg0) {  
        JEditorPane editor = getEditor(arg0);  
        try {  
              
            String selectedText = editor.getSelectedText();  
              
            HTMLDocument document = (HTMLDocument) this.getStyledDocument(editor);  
              
            System.out.println(document == TextView.this.document);  
              
            document.remove(editor.getSelectionStart(),selectedText.length());  
              
            HTMLEditorKit et = (HTMLEditorKit) this.getStyledEditorKit(editor);  
              
            et.insertHTML(document, editor.getSelectionStart(), ""+ selectedText + "", 0, 0, HTML.Tag.SPAN);  
              
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
    }  
  
}  

但是insertHTML方法好像没有用似的,难道我哪里出错了吗? 


所以我想在这里问下,能不能有个方式设置JTextPane中的背景色并且得到html代码。  Java String JTextPane  背景色
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,