Java – 将JTextArea与右对齐

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

|Left | | Centered | | Right| <- Like this 

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

提前致谢!

尝试使用JEditorPaneJTextPane而不是JTextArea

请查看我的另一篇文章JEdi​​torPane垂直对齐以获取完整的示例代码。

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

示例代码:

 JTextPane output = new JTextPane(); SimpleAttributeSet attribs = new SimpleAttributeSet(); StyleConstants.setAlignment(attribs, StyleConstants.ALIGN_RIGHT); output.setParagraphAttributes(attribs, true); 

编辑

你可以试试

 JTextArea jTextArea = new JTextArea(); jTextArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 

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