如果将它们设置为只读模式,则JavaFX文本字段不显示文本插入符号。下面是一个示例:JavaFX-8使文本插入符在只读文本区域中可见
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class TextAreaReadOnly extends Application {
public TextAreaReadOnly() {
}
@Override
public void start(Stage primaryStage) throws Exception {
TextArea textarea = new TextArea();
textarea.setText("This is all\nreadonly text\nin here.");
textarea.setEditable(false);
Scene scene = new Scene(textarea, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
虽然仍然可以使用Shift +光标键选择文本,但不会显示插入符号。有没有人知道这个解决方法?
为什么在'ReadOnly'文本控件上需要'TextCaret'?当它悬停在文本上时,是否要设置鼠标指针的样式? – ItachiUchiha 2014-12-04 10:46:57
我不想让鼠标指针改变。我希望textarea显示TextInputControl.getCaretPosition()的位置。 我猜TextArea背后没有显示插入符号的基本原理是你不需要它,因为无论如何,只要它是只读的,你都不能插入任何文本。但这是错误的(IMO),因为你仍然可以用光标键选择文本,因此你绝对需要一个插入符号。 – rli 2014-12-04 10:53:13