0

で使用JTextPaneしてStyledDocumentいますが、段落の長さを設定できますか? 私の目標は、挿入されるテキストの長さがJTextPane400px を超え、新しい行に移動することです。

編集:

以下のメソッドを使用して、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);
4

1 に答える 1

0

これを試して:

public static void main(String[] args) throws Exception {
    final JTextPane pane = new JTextPane();
    pane.setContentType("text/html");
    final JFrame frm = new JFrame("Text editor");
    pane.setText("<html><table><tr><td width=\"400\"></td></tr></table</html>");
    frm.add(new JScrollPane(pane));
    frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frm.setSize(500, 500);
    frm.setVisible(true);
}

テキストは 400px の後に改行されます

于 2014-08-08T08:24:14.470 に答える