【发布时间】:2015-12-07 16:46:35
【问题描述】:
【问题讨论】:
-
你试过 setLineWrap(true) 吗?
-
是的,文本有自动换行但右对齐的标点符号有这个问题。
标签: java swing jtextarea word-wrap right-align
【问题讨论】:
标签: java swing jtextarea word-wrap right-align
您不能为此使用JTextArea。
相反,您需要使用带有自定义段落属性的JTextPane。
这是一个将每行文本居中的示例。
JTextPane textPane = new JTextPane();
textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );
StyledDocument doc = textPane.getStyledDocument();
// Set alignment to be centered for all paragraphs
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
【讨论】:
为什么不直接在JTextArea 中使用html,Java 会正确显示它。 示例:
JLabel example = new JLabel();
example.setText("<html>This is the first line<br>This is the second</html>");
【讨论】: