本文介绍了Java - 将JTextArea与右对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I it possible to align the text inside a JTextArea to the right (or change the text alignment in general)?

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

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

I've been searching for hours and it seems others have asked this question before but there are no good answers (that actually work).

提前致谢!

推荐答案

尝试使用 JEditorPane JTextPane 而不是 JTextArea

请查看我的另一篇文章以获取完整的示例代码。

Please have a look at my another post JEditorPane vertical aligment for complete sample code.

有关详细信息,请查看此主题

For more info have a look at this thread Vertical Alignment of Text in JEditorPane

示例代码:

JTextPane output = new JTextPane();

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






编辑



你可以尝试


EDIT

You can try

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

了解更多关于

这篇关于Java - 将JTextArea与右对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 00:07