我正在使用JTextPane
with StyledDocument
,是否可以设置其段落的长度?我的目标是当插入的文本JTextPane
长度超过 400 像素时,超过转到新行。
编辑:
我使用下面的方法为 textPane 设置样式
public Style getstyle(String message){
Style style = context.addStyle("mystyle", null);
StyleConstants.setForeground(style, Color.GRAY);
StyleConstants.setBackground(style, new Color(162, 234, 167));
SimpleAttributeSet mystyle = new SimpleAttributeSet();
StyleConstants.setFontFamily(style, "SansSerif");
document.setParagraphAttributes(document.getLength(), message.length(), mystyle, false);
return style;
}
并使用以下将文本插入到我的 JTextPane:
try {
document.insertString(document.getLength(), " "+message+"\n", getStyle(message));
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我想设置段落的长度。
编辑2:
我使用不同的样式,用于不同类型的文本。
编辑3:
我用滚动条添加我JTextPane
的。JPanel
context = new StyleContext();
document = new DefaultStyledDocument(context);
Pane = new JTextPane(document);
Pane.setEditable(false);
Panel.setLayout(new BoxLayout(Panel, BoxLayout.PAGE_AXIS));
JScrollPane scroll = new JScrollPane(Pane);
Panel.add(scroll , BorderLayout.CENTER);