问题
I have JTextPane which contains html, I define my own JTextPane class as follow:
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);
...
} ...}
I insert my text into it as follow :
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();
}
My problem is : both text have the same alignment while the color, size are not!(both of them have right alignment!) My question: is it possible to define different alignment in jtextpane, if yes how?
来源:https://stackoverflow.com/questions/25682219/java-is-it-possible-to-define-different-alignment-in-jtextpane