加载不同的场景,如果某些按钮被选中。

huangapple go评论77阅读模式
英文:

Loading a different Scene if some buttons are checked

问题

以下是您提供的内容的翻译:

// 当“Enter”按钮被点击时触发的方法
@FXML
private void onActionBtnEnter(ActionEvent event) {
    try {
        if (txtUser.getText() == null || txtUser.getText().isEmpty()) {
            new Message().showModal(Alert.AlertType.ERROR, "用户验证",
            (Stage) btnEnter.getScene().getWindow(), "需要用户名");
        } else if (txtPass.getText() == null || txtPass.getText().isEmpty()) {
            new Message().showModal(Alert.AlertType.ERROR, "密码验证",
            (Stage) btnEnter.getScene().getWindow(), "需要密码");
        } else {
            FlowController.getInstance().goMain(); // 加载一个名为goMain()的FXML视图
            ((Stage) btnEnter.getScene().getWindow()).close(); // 关闭登录窗口并加载新场景
        }
    } catch (Exception ex) {
        Logger.getLogger(LandingViewController.class.getName()).log(Level.SEVERE, "登录错误。", ex);
    }
}

// 当"学生"按钮被点击时触发的方法
@FXML
private void onActionBtnStd(ActionEvent event) {
    btnStd.setDisable(true);
    txtPass.setDisable(false);
    txtUser.setDisable(false);
}

// 当"教授"按钮被点击时触发的方法
@FXML
private void onActionBtnProf(ActionEvent event) {
    btnProf.setDisable(true);
    txtPass.setDisable(false);
    txtUser.setDisable(false);
}

// 当"Enter"按钮被点击时触发的方法
@FXML
private void onActionBtnEnter(ActionEvent event) {
    if (btnStd.isDisabled()) { // 如果"学生"按钮被禁用
        FlowController.getInstance().goMain(); // 加载一个名为goMain()的FXML视图
        ((Stage) btnEnter.getScene().getWindow()).close(); // 关闭登录窗口并加载新场景
    } else if (btnProf.isDisabled()) { // 如果"教授"按钮被禁用
        FlowController.getInstance().goMain2(); // 加载一个名为goMain2()的FXML视图
        ((Stage) btnEnter.getScene().getWindow()).close(); // 关闭登录窗口并加载新场景
    } else if (btnProf.isDisabled() && btnStd.isDisabled()) { // 如果"教授"和"学生"按钮都被禁用
        FlowController.getInstance().goMain3(); // 加载一个名为goMain3()的FXML视图
        ((Stage) btnEnter.getScene().getWindow()).close(); // 关闭登录窗口并加载新场景
    }
}

请注意,这里提供的是您所需的代码翻译部分,用于加载不同的FXML视图和执行相关操作。如果您有任何其他问题或需要进一步帮助,请随时提问。

英文:

So I'm trying to load three specific scenes if some buttons are clicked. It is a simple loading screen that calls three other scenes which has 2 textfields(txtUser,txtPass) and 2 buttons (btnCancel, btnEnter) and 2 selecting buttons (btnStd, btnProf) which loads a fxml separately - a third fxml if both are clicked disabled

The tricky part is in this Enter button which is called after enabled or disabling two other buttons, for students and teachers. This also loads a third if both buttons are pressed: the administrator

@FXML
private void onActionBtnEnter(ActionEvent event) {
try {
if (txtUser.getText() == null || txtUser.getText().isEmpty()) {
new Message().showModal(Alert.AlertType.ERROR, "User validation",
(Stage) btnEntrar.getScene().getWindow(),"Needs username");}
else if (txtPass.getText() == null || txtPass.getText().isEmpty()) {
new Message().showModal(Alert.AlertType.ERROR, "Password validation",
(Stage) btnEntrar.getScene().getWindow(), "Needs password");}
else {
FlowController.getInstance().goMain(); //this is one of the fxlm views to load
((Stage) btnEnter.getScene().getWindow()).close(); //closes the login and loads the scene
}
}       
catch (Exception ex) {
Logger.getLogger(LandingViewController.class.getName()).log(Level.SEVERE, "Login error.", ex);
}
}

This works alright but instead of choosing specific fxml to load it only loads goMain() which contains administratorview.fxml. I also did a goMain2() and goMain3().

For students, the studentsview.fxml should load. For profesors the profesorview.fxml. Following code for buttons methods and actions

@FXML
private void onActionBtnStd(ActionEvent event) {
btnStd.setDisable(true);
txtPass.setDisable(false);
txtUser.setDisable(false);
}
@FXML
private void onActionBtnProf(ActionEvent event) {
btnProf.setDisable(true);
txtPass.setDisable(false);
txtUser.setDisable(false);
}

So far good, but loads only a single fxml because they aren't called yet. So I guess in the btnEnter action there should be some code which I tried as in (same event as above):

@FXML
private void onActionBtnEntrar(ActionEvent event) {
else if (btnStd.getValue().isDisabled()){ //added else if but I don't know how to call a boolean property in a void
FlowController.getInstance().goMain(); 
((Stage) btnEnter.getScene().getWindow()).close();}
else if (btnProf.getValue().isDisabled()){ 
FlowController.getInstance().goMain2(); 
((Stage) btnEnter.getScene().getWindow()).close();}
else if (btnProf.getValue().isDisabled() && btnStd.getValue().isDisabled()){ 
FlowController.getInstance().goMain3(); 
((Stage) btnEnter.getScene().getWindow()).close();}

答案1

得分: 0

傻我了,在if语句中我添加了.getValue() - 所需的只是放置component.isDisabled()或.isEnabled()(如果它保存了布尔值)。然而,如果两者都被禁用,它不会加载adminview,而是会加载studentview。进一步调查...

编辑:好的,最后一个“else if”必须比另外两个先被调用。这样做运行得很好。

英文:

Silly me, I added .getValue() in the if statement - all it was needed is to put the component.isDisabled() or .isEnabled() if it holds a boolean. However it doesn't load the adminview if both are checked disabled - it load the studentview. Investigating further...

Edit: Ok the last "else if" must be called first than the other two. It works like a charm.

huangapple
  • 本文由 发表于 2020年5月5日 06:33:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61602739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定