gpt4 book ai didi

javafx对话框错误

转载 作者:行者123 更新时间:2023-12-01 04:16:15 25 4
gpt4 key购买 nike

我正在学习如何在javafx中创建一个对话框,我编写了一个给我带来问题的代码。错误出在 createLoginDialog 方法中。

错误显示“无法访问 TryDialogBox 类型的封闭实例。必须使用 TryDialogBox 类型的封闭实例来限定分配(例如 x.new A(),其中 x 是 尝试对话框)。”

public class TryDialogBox extends Application {
static Stage LOGIN_DIALOG;
static int dx = 1;
static int dy = 1;

private static Stage createLoginDialog(Stage parent, boolean modal) {
if (LOGIN_DIALOG != null) {
LOGIN_DIALOG.close();
}
return new MyDialog(parent, modal, "welcom");
}

public void start(final Stage stage) {
stage.setTitle("developing dialog");
Group root = new Group();
Scene scene = new Scene(root, 433, 312, Color.WHEAT);

MenuBar menuBar = new MenuBar();
menuBar.prefWidthProperty().bind(stage.widthProperty());

Menu menu = new Menu("home");

// add
MenuItem newItem = new MenuItem("change password", null);
newItem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
if (LOGIN_DIALOG == null) {
LOGIN_DIALOG = createLoginDialog(stage, true);
}
LOGIN_DIALOG.sizeToScene();
LOGIN_DIALOG.show();
}
});

menu.getItems().add(newItem);

// add separator
menu.getItems().add(new SeparatorMenuItem());

// add non mdal menu item
ToggleGroup modalGroup = new ToggleGroup();
RadioMenuItem nonModalItem = RadioMenuItemBuilder.create()
.toggleGroup(modalGroup).text("non modal").selected(true)
.build();
nonModalItem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
LOGIN_DIALOG = createLoginDialog(stage, false);
}
});
menu.getItems().add(nonModalItem);
// add modal
RadioMenuItem modalItem = RadioMenuItemBuilder.create()
.toggleGroup(modalGroup).text("modal").selected(true).build();
modalItem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
LOGIN_DIALOG = createLoginDialog(stage, true);
}
});
menu.getItems().add(modalItem);

// add sep
menu.getItems().add(new SeparatorMenuItem());

// add exit
MenuItem exitItem = new MenuItem("Exit", null);
exitItem.setMnemonicParsing(true);
exitItem.setAccelerator(new KeyCodeCombination(KeyCode.X,
KeyCombination.CONTROL_DOWN));
exitItem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Platform.exit();
}
});
menu.getItems().add(exitItem);

// add menu
menuBar.getMenus().add(menu);

root.getChildren().add(menuBar);
stage.setScene(scene);
stage.show();

}

class MyDialog extends Stage {
public MyDialog(Stage owner, boolean modality, String title) {
super();
initOwner(owner);
Modality m = modality ? Modality.APPLICATION_MODAL : Modality.NONE;
initModality(m);
setOpacity(.90);
setTitle(title);
Group root = new Group();
Scene scene = new Scene(root, 250, 150, Color.WHITE);
setScene(scene);

GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(5));
gridpane.setHgap(5);
gridpane.setVgap(5);

Label mainLabel = new Label("enter username & password");
gridpane.add(mainLabel, 1, 0, 2, 1);

Label userNamelbl = new Label("username");
gridpane.add(userNamelbl, 0, 1);

Label passwordlbl = new Label("password");
gridpane.add(passwordlbl, 0, 2);

// username textfield
final TextField userNameFld = new TextField("Admin");
gridpane.add(userNameFld, 1, 1);

// password
final PasswordField passwordFld = new PasswordField();
passwordFld.setText("dwossap");
gridpane.add(passwordFld, 1, 2);

Button login = new Button("change");
login.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
close();
}
});

gridpane.add(login, 1, 3);
GridPane.setHalignment(login, HPos.RIGHT);
root.getChildren().add(gridpane);

}
}

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

最佳答案

三种可能的解决方案(选择一个):

  • 从方法createLoginDialog中删除“static”

  • 将“static”添加到类MyDialog

  • 将类 MyDialog 移到类 TryDialogBox 之外

关于javafx对话框错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19396823/

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