在调整窗口大小时,GridPane保持居中

时间:2017-04-04 19:40:32

标签: javafx gridpane

目前我的gridpane在调整窗口大小的同时保持在X轴的中心位置,我怎样才能使它保持在Y轴的中心位置?

我在TabPane的标签中有这个:

    GridPane grid = new GridPane();
    grid.setPadding(new Insets(20, 0, 0, 0));
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(20);
    grid.setVgap(20);

    this.setContent(grid);

1 个答案:

答案 0 :(得分:0)

此处的关键是VBox.setVgrow(tabPane, Priority.ALWAYS);

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TabPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication65 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        VBox root = new VBox();
        MenuBar mb = new MenuBar();

        TabPane tabPane = new TabPane();
        root.getChildren().addAll(mb, tabPane);


        //StackPane stackPane = new StackPane();
        CreateProfilePane cp = new CreateProfilePane();
        tabPane.getTabs().add(cp);
        VBox.setVgrow(tabPane, Priority.ALWAYS);



        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

enter image description here

enter image description here