【发布时间】:2021-11-10 08:25:50
【问题描述】:
我在这里遇到的唯一问题是,在我开始输入之前,文本指示器始终显示在左侧。
输入前:
输入后:
JTextArea textArea = new JTextArea();
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JScrollPane scroll = new JScrollPane(textArea);
scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
这里是完整的代码:
import java.awt.ComponentOrientation;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test extends JFrame {
public Test() {
this.setSize(300, 100);
JTextArea textArea = new JTextArea();
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JScrollPane scroll = new JScrollPane(textArea);
scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(scroll);
this.setVisible(true);
}
}
顺便说一句,我在 Windows 10 上使用 Java 11。
【问题讨论】:
-
我试验了你的代码。每次我暂停打字时,光标都会跳到该行的最左侧位置。当我继续打字时,光标又回到了右边。在我输入非空格字符之前,GUI 上不会显示空格。 setCaretPosition 方法只有在基础 Document 中至少有一个字符时才会起作用。有趣。
-
你使用的是什么操作系统?
-
Windows 10; Java 14.0.2 编译为 Java 8 标准。