代码之家  ›  专栏  ›  技术社区  ›  Flat Eric

锚烷中标签的改性

  •  1
  • Flat Eric  · 技术社区  · 6 年前

    我有一个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

    1 回复  |  直到 6 年前
        1
  •  1
  •   SedJ601    6 年前

    你也许应该 添加 VBox 作为 ScrollPane . 我这么说是因为 SceneBuilder 让我来吧。在本例中,我将根节点 AnchorPane . 然后我加上 VBox软件 还有它的孩子们。我把 Label's 宽度到 Stage .

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.VBox;
    import javafx.scene.text.TextAlignment;
    import javafx.stage.Stage;
    
    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);
    
            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);
    
            lbl.setTextAlignment(TextAlignment.JUSTIFY);
            lbl.setMaxHeight(Double.MAX_VALUE);
    
            VBox vBox = new VBox(ap);
            AnchorPane rootAP = new AnchorPane(vBox);
            root.setContent(rootAP);
            Scene scene = new Scene(root, 300, 250);
    
            lbl.prefWidthProperty().bind(scene.widthProperty());//This should bind the Label width to the Scene width
    
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    }