【问题标题】:GridPane doesn't align in centerGridPane 未居中对齐
【发布时间】:2019-01-08 10:42:46
【问题描述】:

我正在使用数字键盘制作登录屏幕,但我似乎无法将窗格中的按钮 GridPane 居中对齐。我做错了什么?

Main.java

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.input.KeyCombination;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args){
        launch(args);
    }

    public void start(Stage primaryStage) throws Exception{

        Rectangle2D bounds = Screen.getPrimary().getBounds();
        LoginScreen loginScreen = new LoginScreen(bounds.getWidth(), bounds.getHeight());

        Scene scene = new Scene(loginScreen.get());

        primaryStage.setScene(scene);
        primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
        primaryStage.show();
        primaryStage.setFullScreen(true);
    }
}

LoginScreen.java

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;

public class LoginScreen {
    private Pane root;
    private GridPane numberPad;

    public LoginScreen(double screenWidth, double screenHeight){
        root = new Pane();
        root.setPrefSize(screenWidth, screenHeight);
        root.setBackground(new Background(new BackgroundFill(Color.AQUA, CornerRadii.EMPTY, Insets.EMPTY)));

        numberPad = new GridPane();

        Button button01 = new Button("1");
        Button button02 = new Button("2");
        Button button03 = new Button("3");
        Button button04 = new Button("4");
        Button button05 = new Button("5");
        Button button06 = new Button("6");
        Button button07 = new Button("7");
        Button button08 = new Button("8");
        Button button09 = new Button("9");

        numberPad.setAlignment(Pos.CENTER);

        numberPad.add(button01, 0, 0);
        numberPad.add(button02, 1, 0);
        numberPad.add(button03, 2, 0);
        numberPad.add(button04, 0, 1);
        numberPad.add(button05, 1, 1);
        numberPad.add(button06, 2, 1);
        numberPad.add(button07, 0, 2);
        numberPad.add(button08, 1, 2);
        numberPad.add(button09, 2, 2);

        root.getChildren().addAll(numberPad);

    }

    public Pane get(){
        return root;
    }
}

GUI 代码很冗长,而且这个帖子编辑器不允许我按原样发布我的问题,所以我需要这些额外的行来让它接受我的问题。如果我认为我可以将我的代码减少到 numberPad.setAlignment(Pos.Center) 并且仍然清楚我是如何尝试将我的 GridPane 居中的,我肯定会这样做。我衷心感谢那些愿意借给我时间帮助我解决这个问题的人。

编辑01:

我的问题是 GridPane 本身被绘制在屏幕的左上角而不是屏幕的中心。

【问题讨论】:

    标签: java javafx alignment gridpane


    【解决方案1】:

    您需要实际设置父容器的对齐方式。但是,Pane 不是执行此操作的有效容器。

    如果您要改用VBox,您可以简单地设置它的对齐方式:

    VBox root = new VBox(10);
    root.setAlignment(Pos.CENTER);
    

    这将导致VBox 的所有子代被放置在中心。

    Pos 枚举还提供了其他定位方法,例如,包括TOP_CENTER、TOP_LEFT 和BOTTOM_RIGHT。

    【讨论】:

    • 我可能弄错了,但我相信 CollumnConstraints 用于对齐 GridPane 内的元素,而不是对齐 GridPane 本身。如果我使用这段代码,Hgap 和 Vgap 是可见的,但 GridPane 仍然绘制在屏幕的左上角。
    • 您需要为 GridPane 的父级设置对齐方式。例如,将 GridPane 放在 VBox 中并使用 pane.setAlignment(Pos.CENTER)
    • 我已经更新了我的答案以实际回答您的真实问题。 :)
    猜你喜欢
    • 2014-11-09
    • 2015-07-25
    • 2017-01-14
    • 2016-07-31
    • 2019-04-14
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多