代码之家  ›  专栏  ›  技术社区  ›  navy1978

将Hbox与桩板内的底部中心对齐

  •  0
  • navy1978  · 技术社区  · 6 年前

    enter image description here

    HBox在一个窗格的底部中心,我尝试了以下代码,但我不能做到这一点

    你能解释一下我做错了什么吗?如何做到这一点?

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    
    
    public class TestLayout extends Application {
    
        @Override
        public void start(Stage stage) {
            initUI(stage);
        }
    
        private void initUI(Stage stage) {
            Scene scene =null;
            HBox hbox = new HBox();
            hbox.setStyle("-fx-background-color: #FFFAAA;");
            StackPane pane = new StackPane();
            hbox.setStyle("-fx-background-color: #AAFAAA;");
            hbox.prefWidthProperty().bind(pane.widthProperty().divide(4));
            hbox.prefHeightProperty().bind(pane.heightProperty().divide(10));
    
            pane.getChildren().add(hbox);
    
            scene = new Scene(pane, 600, 600);
            pane.prefWidthProperty().bind(scene.widthProperty());
            pane.prefHeightProperty().bind(scene.heightProperty());
    
            stage.setTitle("Test");
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    
    
    }
    
    class AppLauncherTest {
        public static void main(String[] args) {
            TestLayout.main(args);
        }
    }
    
    0 回复  |  直到 6 年前
        1
  •  3
  •   SedJ601    6 年前

    我会用一个 VBox BorderPane 作为根节点。在这个例子中,我使用 VBox公司 . 我假设会有更多的节点进入,所以我使用了 StackPane Pane 可能需要添加到此。这取决于你想用最终产品做什么。对于底部,我使用了 HBox Margins .

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.scene.layout.Priority;
    import javafx.geometry.Insets;
    
    /**
     * JavaFX App
     */
    public class App extends Application {
    
        @Override
        public void start(Stage stage) {        
            StackPane subRootTop = new StackPane();     
            VBox.setVgrow(subRootTop, Priority.ALWAYS); 
            //subRootTop.setStyle("-fx-background-color: yellow;");
    
            HBox subRootBotton = new HBox();
            VBox.setVgrow(subRootBotton, Priority.ALWAYS);      
            subRootBotton.setStyle("-fx-background-color: green;");
            subRootBotton.setMaxHeight(150);
            VBox.setMargin(subRootBotton, new Insets(40, 40, 40, 40));
    
            VBox root = new VBox(subRootTop, subRootBotton);
            root.setStyle("-fx-background-color: red;");
            Scene scene = new Scene(root, 600, 600);;
    
            stage.setTitle("Test");
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            launch();
        }
    
    }
    

    enter image description here

        2
  •  3
  •   fabian    6 年前

    StackPane 不是大小不应该做。场景会自动调整其根的大小以填充整个可用区域。假设需要相对大小,使用 堆叠窗格 不是个好选择。

    如果你想让孩子有一个固定的距离,从左到右,从下到下 ,则可以指定页边距和对齐方式。通过设置 maxHeight 要使用首选高度:

    private void initUI(Stage stage) {
        HBox hbox = new HBox();
        hbox.setStyle("-fx-background-color: #FFFAAA;");
        hbox.setMaxHeight(Region.USE_PREF_SIZE);
    
        // could be calculated based on children instead of assigning
        // an absolute value
        hbox.setPrefHeight(30);
    
        StackPane.setAlignment(hbox, Pos.BOTTOM_CENTER);
        StackPane.setMargin(hbox, new Insets(20));
    
        StackPane pane = new StackPane(hbox);
        pane.setStyle("-fx-background-color: #AAFAAA;");
    
        Scene scene = new Scene(pane, 600, 600);
    
        stage.setTitle("Test");
        stage.setScene(scene);
        stage.show();
    }
    
        3
  •  1
  •   anko    6 年前

    也许你可以用一个 AnchorPane StacKPane StackPane 作为基础布局,只需将 锚烷 HBox 但你必须使用 为了你的计划。

    package sample;
    
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
        private void initUI(Stage stage) {
    
            // Create an anchor pane as base layout and set a color:
            AnchorPane root = new AnchorPane();
            root.setStyle("-fx-background-color: #FFFAAA;");
    
            // Create a second container and set a minimum height and a color;
            HBox hbox = new HBox();
            hbox.setMinHeight(100d);
            hbox.setStyle("-fx-background-color: #AAFAAA;");
    
            // Give the child container a fixed location:
            AnchorPane.setBottomAnchor(hbox, 50d);
            AnchorPane.setLeftAnchor(hbox, 75d);
            AnchorPane.setRightAnchor(hbox, 75d);
    
            // Add the horizontal box to the base anchor pane:
            root.getChildren().add(hbox);
    
            stage.setScene(new Scene(root, 600, 600));
            stage.show();
        }
    
        @Override
        public void start(Stage stage) throws Exception {
            initUI(stage);
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    预览:

    Preview

    推荐文章