Java - 将JTextArea与右对齐

Ood*_*Ood 5 java swing alignment jtextarea

我可以将JTextArea中的文本对齐到右边(或者更改文本对齐)吗?

|Left         |
|  Centered   |
|        Right|    <- Like this
Run Code Online (Sandbox Code Playgroud)

我一直在寻找几个小时,似乎其他人之前已经问过这个问题,但没有好的答案(实际上有效).

提前致谢!

Bra*_*raj 8

尝试使用JEditorPaneJTextPane代替JTextArea.

请查看我的另一篇文章JEdi​​torPane垂直对齐,了解完整的示例代码.

有关更多信息,请查看此线程在JEditorPane中的文本垂直对齐方式

示例代码:

JTextPane output = new JTextPane();

SimpleAttributeSet attribs = new SimpleAttributeSet();
StyleConstants.setAlignment(attribs, StyleConstants.ALIGN_RIGHT);
output.setParagraphAttributes(attribs, true);
Run Code Online (Sandbox Code Playgroud)

编辑

你可以试试

JTextArea jTextArea = new JTextArea();
jTextArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
Run Code Online (Sandbox Code Playgroud)

了解有关如何从右到左设置JTextArea方向的更多信息