【问题标题】:How can display text on multiple rows and align right? [closed]如何在多行上显示文本并右对齐? [关闭]
【发布时间】:2015-12-07 16:46:35
【问题描述】:

我想在多行中显示文本并且文本右对齐。

我尝试将其放在 JTextAreaComponentOrientation.RIGHT_TO_LEFT 中,但标点符号 (?!) 显示不正确。

【问题讨论】:

  • 你试过 setLineWrap(true) 吗?
  • 是的,文本有自动换行但右对齐的标点符号有这个问题。

标签: java swing jtextarea word-wrap right-align


【解决方案1】:

您不能为此使用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);

【讨论】:

  • 问题是文字没有自动换行img
  • @AdrianSimionescu,默认情况下 JTextPane 会自动换行。您正在代码中执行某些操作来关闭此功能。发布适当的SSCCE 来演示问题,而不是图片。请参阅No Wrap Text Pane 了解更多信息。
【解决方案2】:

为什么不直接在JTextArea 中使用html,Java 会正确显示它。 示例:

JLabel example = new JLabel();
example.setText("<html>This is the first line<br>This is the second</html>");

【讨论】:

  • 但这是一个聊天帖子。我不知道
    在哪里,因此我需要自动换行。文本输入是一个必须多次显示的段落。
猜你喜欢
  • 2017-03-10
  • 1970-01-01
  • 2013-08-25
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 2019-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多