我有一个JavaFX
Label
在一个
AnchorPane
,包含在
VBox
. 这个在一个
ScrollPane
. 我希望文本自动换行并适应窗口的宽度。
下面是我的代码的一个简化示例:
public class MainApp extends Application {
public static void main(String[] args) {
Application.launch(MainApp.class);
}
@Override
public void start(Stage primaryStage) {
final ScrollPane root = new ScrollPane();
root.setFitToWidth(true);
VBox vbx = new VBox();
vbx.setStyle("-fx-background-color: green");
vbx.setSpacing(20);
root.setContent(vbx);
Label lbl = new Label(
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " +
"sed diam nonumy eirmod tempor invidunt ut labore et dolore " +
"magna aliquyam erat, sed diam voluptua. At vero eos et " +
"accusam et justo duo dolores et ea rebum.");
lbl.setStyle("-fx-background-color: yellow");
lbl.setWrapText(true);
AnchorPane ap = new AnchorPane();
ap.setStyle("-fx-background-color: blue");
ap.getChildren().add(lbl);
vbx.getChildren().add(ap);
lbl.maxWidthProperty().bind(ap.widthProperty());
lbl.setTextAlignment(TextAlignment.JUSTIFY);
AnchorPane.setLeftAnchor(lbl, 0.0);
AnchorPane.setTopAnchor(lbl, 0.0);
AnchorPane.setRightAnchor(lbl, 0.0);
AnchorPane.setBottomAnchor(lbl, 0.0);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
}
问题是
:当我启动程序时
标签
比可用高度高得多,文本在内部垂直居中。当我调整窗口宽度时,文本会跳到顶部,其高度会减小到正确的大小。但是
VBox软件
仍然很大。
我需要做什么改变,文字最初是在正确的位置(在顶部),而不必调整窗口的大小,以及如何使VBox不大于必要的大小?
请注意
:
锚烷
有什么原因需要很长时间才能解释,所以我不能删除它。我也不能改变
标签
到
Text
因为它必须至少保留任何来自
Region