【问题标题】:GridPane stay centered while window is resized调整窗口大小时,GridPane 保持居中
【发布时间】:2017-04-04 19:40:32
【问题描述】:

目前我的网格窗格在调整窗口大小时保持在 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);

【问题讨论】:

  • 你必须添加一些代码,这样我们才能看到你在做什么以及你是怎么做的。
  • @SedrickJefferson 我已经添加了代码
  • 如果你想要一个具体的答案,你应该为视图添加完整的代码。
  • @SedrickJefferson here 是完整的视图,仍然无法做到

标签: javafx gridpane


【解决方案1】:

这里的关键是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);
    }

}

【讨论】:

  • 我的 StackPane 位于选项卡中,因此在调整窗口大小时网格仍然不集中。有什么办法吗? @SedrickJefferson
  • 请看this截图,StackPane是绿色填充的。
  • 看起来你需要 vgow = Always on stackpane;
  • 我忘了说 TabPane 在 VBox 中,所以它仍然不起作用。感谢您的帮助,很抱歉被弱化了
  • 为什么在VBox中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 2015-02-23
  • 2012-03-23
  • 2019-10-05
  • 2011-09-10
  • 2012-05-20
相关资源
最近更新 更多