【问题标题】:JavaFX: How to center the center node on a borderpane according to the Stage?JavaFX:如何根据舞台将中心节点放在边框上?
【发布时间】:2016-11-02 16:06:52
【问题描述】:

我在将边框窗格中的按钮居中以根据舞台居中时遇到问题。它根据边框居中,但我希望它位于窗口的确切中心。

我指的是图片的底部,有播放按钮和难度选择框。

我正在使用边框让难度部分位于右侧,播放按钮位于中间。

如您所见,播放不在图片中心。

我想这样做,但我不知道该怎么做。我认为我可以将左节点的宽度设置为等于我的右节点,但我的左节点没有任何东西。

// bottom section for positioning
    BorderPane settings = new BorderPane();
    settings.setPadding(new Insets(10));

    // play button
    Button playBtn = new Button("Play");
    settings.setCenter(playBtn);

    // difficulty options
    Label diffLabel = new Label("Difficulty: ");
    ChoiceBox<String> diffBox = new ChoiceBox<String>(FXCollections.observableArrayList(
            "Easy", "Standard", "Hard"
    ));
    diffBox.getSelectionModel().select(1);

    // for wrapping the diffuclty.
    HBox difficulty = new HBox(diffLabel, diffBox);
    difficulty.setAlignment(Pos.CENTER);
    difficulty.setSpacing(10);

    settings.setRight(difficulty);

【问题讨论】:

    标签: java javafx borderpane


    【解决方案1】:

    其中一种可能性是在BorderPane 的左侧添加一个Region 以充当分隔符。

    来自doc of BorderPane

    左右孩子将被调整为他们喜欢的宽度 并延长顶部和底部节点之间的长度。和中心 节点将被调整大小以填充中间的可用空间。

    因此添加一个与右侧HBox 宽度相同的垫片应该可以解决问题:

    Region padderRegion = new Region();
    padderRegion.prefWidthProperty().bind(difficulty.widthProperty());
    settings.setLeft(padderRegion);
    

    这里左侧间隔的宽度绑定到右侧HBox的宽度,因此居中的Node(播放按钮)将在屏幕上居中。

    另一种可能性是在HBox 中使用各种填充器:

    HBox bottomPanel = new HBox();
    bottomPanel.setAlignment(Pos.CENTER);
    
    Region padderRegion1 = new Region();
    Region padderRegion2 = new Region();
    Region padderRegion3 = new Region();
    padderRegion1.prefWidthProperty().bind(diffLabel.widthProperty().add(diffBox.widthProperty()));
    
    bottomPanel.getChildren().addAll(padderRegion1, padderRegion2, playBtn, padderRegion3, diffLabel, diffBox);
    HBox.setHgrow(padderRegion2, Priority.ALWAYS);
    HBox.setHgrow(padderRegion3, Priority.ALWAYS);
    

    【讨论】:

    • 谢谢!这实际上非常有效。我尝试使用 AnchorPane 执行此操作,但没有成功。猜猜我需要使用一个区域。它也不会使底部比其他解决方案所做的顶部更宽。
    【解决方案2】:

    试试这个:

    public class Main extends Application {
    
        public static void main(String[] args) {
            Application.launch(args);
        }
    
        public void start(Stage stage) {
    
            BorderPane settings = new BorderPane();
            settings.setPadding(new Insets(10));
    
            Button playBtn = new Button("Play");
    
            Label zombieLabel = new Label("Zombie Dice");
            zombieLabel.setStyle("-fx-font-size: 60");
            zombieLabel.setAlignment(Pos.CENTER);
            HBox zomBox = new HBox();
            zomBox.getChildren().add(zombieLabel);
            zomBox.setAlignment(Pos.CENTER);
    
            Label indicateLabel = new Label("Indicate who is playing");
            indicateLabel.setStyle("-fx-font-size: 20");
            indicateLabel.setAlignment(Pos.CENTER);
            HBox indBox = new HBox();
            indBox.getChildren().add(indicateLabel);
            indBox.setAlignment(Pos.CENTER);
    
    
            ChoiceBox<String> diffBox = new ChoiceBox<>(FXCollections.observableArrayList(
                    "Easy", "Standard", "Hard"
            ));
            diffBox.getSelectionModel().select(1);
    
            GridPane gridPane = new GridPane();
            gridPane.setPadding(new Insets(5));
            gridPane.setHgap(5);
            gridPane.setVgap(5);
    
            ColumnConstraints col1 = new ColumnConstraints(150);
            ColumnConstraints col2 = new ColumnConstraints(150);
            ColumnConstraints col3 = new ColumnConstraints(150);
    
            gridPane.getColumnConstraints().addAll(col1, col2, col3);
            gridPane.setAlignment(Pos.CENTER);
    
            HBox hBox = new HBox(10);
            hBox.getChildren().addAll(playBtn);
            hBox.setAlignment(Pos.CENTER);
    
            gridPane.add(zomBox, 0, 0, 3, 1);
            gridPane.add(indBox, 0, 1, 3, 1);
            gridPane.add(hBox, 1, 2);
            gridPane.add(diffBox, 2, 2);
    
            Scene root = new Scene(gridPane);
            stage.setScene(root);
            stage.show();
        }
    }
    

    可能您必须使用填充和间隙。

    【讨论】:

    • 好吧,这行得通,但是我想知道是否有办法做到这一点,这样您就不会对列约束进行硬编码。另外我做了一些修改,发现如果我将中间的Column Constraint变小,它不会拉伸窗口并使底部比顶部更宽,但同时如果我把它做得太小,播放按钮不会显示“播放”,但改为省略号。有没有办法解决这个问题并让它尽可能小?
    猜你喜欢
    • 1970-01-01
    • 2015-06-15
    • 2014-10-21
    • 1970-01-01
    • 2012-11-22
    • 2017-12-17
    • 2017-08-27
    • 1970-01-01
    相关资源
    最近更新 更多