我可以将 JTextArea 中的文本右对齐(或更改一般的文本对齐方式)吗?
|Left |
| Centered |
| Right| <- Like this
我已经搜索了几个小时,似乎其他人以前也问过这个问题,但没有好的答案(实际上有效)。
提前致谢!
最佳答案
尝试使用 JEditorPane
或 JTextPane
而不是 JTextArea
。
请看我的另一篇文章JEditorPane vertical aligment获取完整的示例代码。
有关更多信息,请查看此线程 Vertical Alignment of Text in 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);
阅读更多关于 How to set the orientation of JTextArea from right to left 的信息
关于Java - 将 JTextArea 右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24315757/