gpt4 book ai didi

java - 按钮不在应有的位置

转载 作者:行者123 更新时间:2023-12-01 11:02:56 26 4
gpt4 key购买 nike

我有这个代码:

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...

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

最佳答案

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

buttonPane.setAlignment(Pos.CENTER_RIGHT);

或者你可以告诉网格 Pane 不要让buttonPane随着增长

GridPane.setFillWidth(buttonPane, false);

关于java - 按钮不在应有的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33204319/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com