按钮不应该在哪里

时间:2015-10-18 23:16:37

标签: java javafx javafx-8

我有这段代码:

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()时,我得到了:

not working

按钮应该在右边,相反,我发现它靠近中心,好像它在它的单元格的左侧。为什么会这样?我是JavaFX的新手,但在Java方面有一些经验。

修改

似乎buttonPane占据了整个空间。如果我添加:

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

然后:

Hmm...

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

1 个答案:

答案 0 :(得分:1)

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

buttonPane.setAlignment(Pos.CENTER_RIGHT);

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

一起成长
GridPane.setFillWidth(buttonPane, false);