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

错误消息为“无法访问类型为TryDialogBox的封闭实例。必须使用类型为TryDialogBox的封闭实例(例如x.new A(),其中x是以下实例)来限定分配
 TryDialogBox)。”

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中删除​​“静态”
将“静态”添加到类MyDialog
将课程MyDialog移到课程TryDialogBox之外

10-08 17:34