我有包含html的JTextPane,我定义了自己的JTextPane类,如下所示:
public class MyPane extends JTextPane {
static SimpleAttributeSet Format2 = new SimpleAttributeSet();
static SimpleAttributeSet Format1 = new SimpleAttributeSet();
static {
StyleConstants.setForeground(Format1, Color.black);
StyleConstants.setLeftIndent(Format1, 5);
StyleConstants.setRightIndent(Format1, 5);
StyleConstants.setSpaceAbove(Format1, 0);
StyleConstants.setSpaceBelow(Format15);
StyleConstants.setFontFamily(Format1, "Helvetica");
StyleConstants.setAlignment(Format1, StyleConstants.ALIGN_LEFT);
StyleConstants.setFontSize(Format1, 14);
}
static {
StyleConstants.setForeground(Format2, Color.gray);
StyleConstants.setFontFamily(Format2, "Helvetica");
StyleConstants.setFontSize(Format2, 5);
StyleConstants.setSpaceAbove(Format2, 0);
StyleConstants.setAlignment(Format2, StyleConstants.ALIGN_RIGHT);
}
HTMLDocument doc;
HTMLEditorKit kit;
Style text1;
Style text2;
public MyPane() {
setContentType("text/html");
setOpaque(false);
setEditorKit(kit);
setText("<html><body><div id='content'></div></body></html>");
doc = (HTMLDocument) getStyledDocument();
chatdiv = doc.getElement("content");
text1 = addStyle("text", null);
text1.addAttributes(format1);
text2 = addStyle("time", null);
text2.addAttributes(format2);
...
} ...}
我将文字插入其中,如下所示:
int Place;
try {
doc.insertString(doc.getLength(), "text one", text1);
doc.setParagraphAttributes(0, doc.getLength(), Format1, false);
Place = doc.getLength();
doc.insertString(doc.getLength(), "\n text two", text2);
doc.setParagraphAttributes(Place, doc.getLength(), Format2,
false);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我的问题是:两个文本具有相同的对齐方式,而颜色,大小则没有!(两者都有正确对齐!) 我的问题:是否有可能在jtextpane中定义不同的对齐方式,如果是的话如何?