最佳答案
您不能为此使用 JTextArea
。
相反,您需要使用具有自定义段落属性的 JTextPane
。
下面是一个将每行文本居中的示例。
JTextPane textPane = new JTextPane();
textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );
StyledDocument doc = textPane.getStyledDocument();
// Set alignment to be centered for all paragraphs
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
关于java - 如何在多行上显示文本并右对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34138787/