[英]JavaFX - How to sync up TextArea scroll bar and ScrollPane scroll bar
[英]Why doesn't TextArea occupy entire area of ScrollPane in JavaFX? How to add scroll bars to TextArea in JavaFX?
在 Swing 中,我将JTextArea
添加到JScrollPane
中,以便JTextArea
具有滚动条。 当我在JavaFX
做同样的事情时,行为是不同的。
这个例子
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class SlideshowForSlipryPage2 extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SlideshowForSlipryPage");
primaryStage.setScene(
new Scene(
new ScrollPane(
new TextArea() {{
setPromptText("[PROMPT 1]");
}}
)
, 300, 250
)
);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
给
即文本区域比窗口宽,比窗口短。
为什么以及如何修复?
此问题与文本区域的首选项宽度无关。
您必须将 wrap 属性设置为 true,这样当光标到达容器边缘时,文本将换行成两行。 2种方式:
1)使用场景构建器是最简单的方法。 只需单击 TextArea 属性中的“Wrap Text”。
2)调用TextArea的如下方法:
textArea.setWrapText(true);
尝试这个:
textArea.setPrefSize( Double.MAX_VALUE, Double.MAX_VALUE );
并像这样设置与父级相同的滚动窗格大小:
scrollPane.prefWidthProperty().bind(<parentControl>.prefWidthProperty());
scrollPane.prefHeightProperty().bind(<parentConrol>.prefHeightProperty());
我希望它能解决您的疑问!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.