JAVAFX申请结束

时间:2018-05-23 07:33:53

标签: java javafx fxml

package login;

public class loginAppController implements Initializable {

    loginModel loginModel = new loginModel();

    @FXML
    private JFXButton loginButton;
    @FXML
    private Label dbStatus;
    @FXML
    private JFXTextField email;
    @FXML
    private JFXPasswordField password;
    @FXML
    private JFXComboBox<option> combobox;
    @FXML
    public Label loginStatus;


    @FXML
    public void initialize(URL location, ResourceBundle resources) {
        if(this.loginModel.isDatabaseConnected()) {
            this.dbStatus.setText("Connected To Database");
        } else {
            this.dbStatus.setText("Not Connected To Database");
        }

        this.combobox.setItems(FXCollections.observableArrayList(option.values()));
    }

    @FXML
    public void Login(ActionEvent event) throws Exception {
        try {
            if (this.loginModel.isLogin(this.email.getText(), this.password.getText(), ((option) this.combobox.getValue()).toString())) {
                Stage stage = (Stage) this.loginButton.getScene().getWindow();
                stage.close();
                switch (((option) this.combobox.getValue()).toString()) {
                    case "Admin":
                        adminLogin();
                        break;
                    case "Student":
                        studentLogin();
                        break;
                }
            } else {
                this.loginStatus.setText("Wrong Data");
            }
        } catch (Exception localException) {
        }
    }



    public void studentLogin() {
        try {
            Stage userStage = new Stage();
            FXMLLoader loader = new FXMLLoader();
            Pane root = (Pane) loader.load(getClass().getResource("/Users/Alar/Desktop/SMS/src/main/resources/students/studentFXML.fxml").openStream());
            StudentsController studentsController = (StudentsController) loader.getController();

            Scene scene = new Scene(root);
            userStage.setScene(scene);
            userStage.setTitle("Student Dashboard");
            userStage.setResizable(false);
            userStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void adminLogin() {
        try {
            Stage adminStage = new Stage();
            FXMLLoader adminLoader = new FXMLLoader();
            Pane adminroot = (Pane) adminLoader.load(getClass().getResource("/Users/Alar/Desktop/SMS/src/main/resources/Admin/Admin.fxml").openStream());
            AdminController adminController = (AdminController) adminLoader.getController();

            Scene adminscene = new Scene(adminroot);

            adminStage.setScene(adminscene);
            adminStage.setTitle("Admin Dashboard");
            adminStage.setResizable(true);
            adminStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

当我输入登录数据时,我调试了它,应用程序从

跳转
Pane adminroot = (Pane) adminLoader.load(getClass().getResource("/Users/Alar/Desktop/SMS/src/main/resources/Admin/Admin.fxml").openStream());

catch (Exception localException) { }

它不会打开新窗口。 studentLogin()也是如此。

非常感谢任何帮助

谢谢

2 个答案:

答案 0 :(得分:1)

getResource&#39; s参数是类路径中的路径。如果您正在使用jar,它将匹配jar文件中资源数据条目的名称。您正在使用文件路径。

此外,最好使用url作为加载fxml的位置,因为这样可以使用相对于文档位置的资源(attribute values with prefix @)。

FXMLLoader adminLoader = new FXMLLoader(getClass().getResource("/Admin/Admin.fxml"));
Pane adminroot = (Pane) adminLoader.load();

如果上述代码段中的路径不起作用,请根据资源相对于类路径根的位置调整路径。

答案 1 :(得分:0)

我猜你的路径错了,尝试将你的行改为:

public void adminLogin(){
        try {
            Stage adminStage = new Stage();
            FXMLLoader adminLoader = new FXMLLoader();
            Pane adminroot = (Pane)adminLoader.load(getClass().getResource("/Admin/Admin.fxml").openStream());
            AdminController adminController = (AdminController)adminLoader.getController();

            Scene adminscene = new Scene(adminroot);

            adminStage.setScene(adminscene);
            adminStage.setTitle("Admin Dashboard");
            adminStage.setResizable(true);
            adminStage.show();



        }catch (IOException e){
            e.printStackTrace();


        }

}