で使用JTextPane
してStyledDocument
いますが、段落の長さを設定できますか? 私の目標は、挿入されるテキストの長さがJTextPane
400px を超え、新しい行に移動することです。
編集:
以下のメソッドを使用して、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);