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

FlowPane不占其外部HBox宽度的100%

  •  0
  • parsecer  · 技术社区  · 4 年前

    enter image description here

    这是调整到全屏时的外观:

    enter image description here

    如何使流窗格的跨度达到HBox宽度的100%,同时保持HBox在网格内居中,流窗格在HBox内居中,并且当窗口调整大小时,流窗格和HBox会自动调整大小?所以字母C和D至少适合小窗口中的第一个raw,所有字母适合大窗口。

    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    
    public class TestFlowPane extends Application {
        private GridPane gridPane;
        private Scene scene;
    
        @Override
        public void start(Stage applicationStage) {
            gridPane = new GridPane();
            scene = new Scene(gridPane, 500, 400);
            ColumnConstraints column = new ColumnConstraints();
            column = new ColumnConstraints();
            column.setPercentWidth(100);
            gridPane.setAlignment(Pos.CENTER);
            gridPane.getColumnConstraints().add(column);
    
            gridPane.setPrefSize(500, 400);
            gridPane.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
    
            gridPane.setPadding(new Insets(5, 20, 20, 20));
            gridPane.setHgap(10);
            gridPane.setVgap(10);
    
            HBox panelBox = new HBox();
            panelBox.setStyle( "-fx-background-color:   red; ");
            panelBox.setSpacing(20);
            panelBox.setAlignment(Pos.CENTER);
            FlowPane flowPane = new FlowPane();
            flowPane.setStyle( "-fx-background-color:   blue; ");
    
            String[] names = {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "BBBBBBBBBBBBB", "C", "D", "E"};
            for (String n: names) {
                Label label = new Label(n);
                label.setStyle( "-fx-text-fill:   white; ");
                flowPane.getChildren().add(label);
            }
            flowPane.setHgap(5);
            flowPane.setVgap(10);
            panelBox.getChildren().add(flowPane);
            gridPane.add(panelBox, 0, 0);
    
            applicationStage.setScene(scene);
            applicationStage.show();
        }
    }
    
    0 回复  |  直到 4 年前
        1
  •  2
  •   aziz k'h    4 年前

    您需要添加:

    HBox.setHgrow(flowPane, Priority.ALWAYS);
    

    之后

    panelBox.getChildren().add(flowPane);