如何根据登录用户的类别显示不同的屏幕?

时间:2017-06-13 21:19:53

标签: java authentication javafx

我正在创建一个 JavaFX 应用程序。在登录屏幕之后,如果他也是管理员,我希望用户看到稍微不同的布局。

如何根据此更改MainWindow之后LoginWindow的视图?

(我试图尽可能地减轻代码)

以下是负责本地管理用户的LoginController.java类(以及其他内容)的代码:

public class LoginController {
    // All @FXMLs needed ...
    @FXML
    private Button loginButton;

    public void initialize() {
        loginButton.setOnAction(event -> {
            if (authorize()) {
                MainLoader mainLoader = new MainLoader();
                mainLoader.loadMain();
                // Close login screen
            }
        });

    private boolean authorize() {
        // returns true if the user is authorized
    }

    private boolean isAdmin() {
        // returns true if the user is also admin
    }
}

如您所见,LoginController来电MainLoader.java

public class MainLoader {
    public void loadMain() {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("../../view/main_screen.fxml"));
            Stage stage = new Stage();
            stage.setScene(new Scene(root));
            stage.show();
        } catch (IOException ex) {
            // Manage exception
        }
    }
}

以下是MainController.java类的代码,负责正确设置应用程序的主窗口:

public class MainController {
    // All @FXMLs needed ...
    @FXML
    private Tab adminTab;

    public void initialize() {
        // Setting all controls of previous widgets...

        // IF IS ADMIN THEN:
            adminTab.setDisable(false);
    }
}

0 个答案:

没有答案
相关问题