小编典典

Java-将JTextArea右对齐

java

我可以将JTextArea中的文本向右对齐(或通常更改文本对齐方式)吗?

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

我一直在搜索数小时,似乎其他人之前也曾问过这个问题,但没有好的答案(这确实有效)。

提前致谢!


阅读 768

收藏
2020-09-28

共1个答案

小编典典

尝试使用JEditorPaneJTextPane代替JTextArea

有关更多信息,请查看此线程在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);
2020-09-28