Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在多行上显示文本并向右对齐?_Java_Swing_Jtextarea_Word Wrap_Right Align - Fatal编程技术网

Java 如何在多行上显示文本并向右对齐?

Java 如何在多行上显示文本并向右对齐?,java,swing,jtextarea,word-wrap,right-align,Java,Swing,Jtextarea,Word Wrap,Right Align,我想在多示例行中显示文本,并右对齐文本 我试着用ComponentOrientation将它放在JTextArea中。右至左T,但标点(?!)显示不正确 您不能为此使用JTextArea 相反,您需要使用带有自定义段落属性的JTextPane 下面是一个将文本置于每行中心的示例 JTextPane textPane = new JTextPane(); textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" ); St

我想在多示例行中显示文本,并右对齐文本

我试着用
ComponentOrientation将它放在
JTextArea
中。右至左
T,但标点(?!)显示不正确


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

相反,您需要使用带有自定义段落属性的
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示例=新的JLabel();
setText(“这是第一行
这是第二行”);
为什么不在JTextArea中使用html,Java将正确显示它。 例如:

JLabel示例=新的JLabel();
setText(“这是第一行
这是第二行”);
您是否尝试过setLineWrap(true)?是的,文本具有自动换行功能,但右对齐有标点符号问题。您是否尝试过setLineWrap(true)?是的,文本具有自动换行功能,但右对齐有标点符号问题。问题是文本没有自动换行@AdrianSimionescu,JTextPane默认情况下会换行。您正在代码中执行某些操作以关闭此功能。张贴一张合适的图片来演示问题,而不是图片。有关更多信息,请参阅。问题是文本没有自动换行@AdrianSimionescu,JTextPane默认情况下会换行。您正在代码中执行某些操作以关闭此功能。张贴一张合适的图片来演示问题,而不是图片。有关详细信息,请参阅。但这是一篇聊天帖子。我不知道在哪里,因此我需要自动包装。文本输入是一段必须多次显示的段落,但它是一篇聊天文章。我不知道在哪里,因此我需要自动包装。文本输入是必须多次显示的段落。