1

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

错误显示“无法访问 TryDialogBox 类型的封闭实例。必须使用 TryDialogBox 类型的封闭实例限定分配(例如 xnew 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);
    }
}
4

1 回答 1

0

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

  • 从方法中删除“静态”createLoginDialog

  • 将“静态”添加到类MyDialog

  • 将课堂MyDialog移出课堂TryDialogBox

于 2013-10-16T09:22:15.193 回答