javafx对话框错误

时间:2013-10-16 06:44:21

标签: java javafx

我正在学习如何在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);
    }
}

1 个答案:

答案 0 :(得分:0)

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

  • 从方法createLoginDialog

  • 中删除“静态”
  • 将“静态”添加到班级MyDialog

  • 将课程MyDialog移到课程TryDialogBox

  • 之外