我有以下代码:

public static final String DEFAULT_TITLE = "Login";

public static final String USERNAME_LABEL_DEFAULT_TEXT = "Username: ";
public static final String PASSWORD_LABEL_TEXT = "Password: ";

public static final String DEFAULT_LOGIN_TEXT = "Login";
public static final String CANCEL_TEXT = "Cancel";

public static final boolean DEFAULT_HAS_CANCEL = true;
public static final boolean DEFAULT_CAN_CLOSE = true;

private static final Insets MAIN_PANE_PADDING = new Insets(10);
private static final int MAIN_PANE_GAP = 10;
private static final int BUTTON_PREF_WIDTH = 60;
private static final int BUTTON_PANE_SPACING = 10;

private static final boolean IS_RESIZABLE = false;

private static final ColumnConstraints COL_1_CONSTRAINS = new ColumnConstraints(
        70);
private static final ColumnConstraints COL_2_CONSTRAINS = new ColumnConstraints(
        200);

private static String username = null;
private static char[] password = null;

public static UserInfo showLoginPane() {
    return showLoginPane(DEFAULT_TITLE);
}

public static UserInfo showLoginPane(String title) {
    return showLoginPane(title, USERNAME_LABEL_DEFAULT_TEXT);
}

public static UserInfo showLoginPane(String title,
        String usernameLabelText) {
    return showLoginPane(title, usernameLabelText, DEFAULT_LOGIN_TEXT);
}

public static UserInfo showLoginPane(String title,
        String usernameLabelText, String loginText) {
    return showLoginPane(title, usernameLabelText, loginText, DEFAULT_HAS_CANCEL, DEFAULT_CAN_CLOSE);
}

public static UserInfo showLoginPane(String title,
        String usernameLabelText, String loginText, boolean hasCancel,
        boolean canClose) {

    Stage stage = new Stage();

    GridPane mainPane = new GridPane();
    mainPane.setPadding(MAIN_PANE_PADDING);
    mainPane.setHgap(MAIN_PANE_GAP);
    mainPane.setVgap(MAIN_PANE_GAP);
    mainPane.getColumnConstraints().addAll(COL_1_CONSTRAINS,
            COL_2_CONSTRAINS);

    Label userLabel = new Label(usernameLabelText);
    GridPane.setHalignment(userLabel, HPos.RIGHT);

    TextField usernameField = new TextField();
    GridPane.setHalignment(usernameField, HPos.LEFT);

    mainPane.addRow(0, userLabel, usernameField);

    Label passwordLabel = new Label(PASSWORD_LABEL_TEXT);
    GridPane.setHalignment(passwordLabel, HPos.RIGHT);

    TextField passwordField = new TextField();
    GridPane.setHalignment(passwordField, HPos.LEFT);

    mainPane.addRow(1, passwordLabel, passwordField);

    HBox buttonPane = new HBox(BUTTON_PANE_SPACING);

    Button login = new Button(loginText);
    login.setPrefWidth(BUTTON_PREF_WIDTH);
    buttonPane.getChildren().add(login);
    login.setOnAction(e -> {
        username = usernameField.getText();
        password = passwordField.getText().toCharArray();
    });

    if (hasCancel) {
        Button cancel = new Button(CANCEL_TEXT);
        cancel.setPrefWidth(BUTTON_PREF_WIDTH);
        buttonPane.getChildren().add(cancel);
        cancel.setOnAction(e -> stage.close());
    }

    mainPane.add(buttonPane, 1, 2);
    GridPane.setHalignment(buttonPane, HPos.RIGHT);

    Scene scene = new Scene(mainPane);

    stage.setTitle(title);
    stage.setScene(scene);
    stage.setResizable(IS_RESIZABLE);
    stage.setOnCloseRequest(e -> {
        if (canClose) {
            stage.close();
        } else {
            e.consume();
        }
    });

    stage.showAndWait();

    return new UserInfo(username, password);
}


但是当我呼叫showLoginPane()时,我得到:

java - 按钮不在应有的位置-LMLPHP

这些按钮应该在右侧,相反,我在中央附近找到它,就像它在其单元格的左侧一样。为什么会这样呢?我对JavaFX相当陌生,但是有一些Java经验。

编辑:

看来buttonPane占据了整个空间。如果我添加:

buttonPane.setStyle("-fx-border-color:blue;");


然后:

java - 按钮不在应有的位置-LMLPHP

现在的问题是:如何解决这个问题?

最佳答案

您可以告诉buttonPane将其内容向右对齐:

buttonPane.setAlignment(Pos.CENTER_RIGHT);


或者您可以告诉网格窗格不要让buttonPane

GridPane.setFillWidth(buttonPane, false);

08-04 19:10