上週我發佈了一個關於切換舞臺/場景的問題,有人建議我閱讀MVC框架。我已經完成了這個工作,並開始轉換我的應用程序,並且現在當我嘗試更改場景時出現錯誤。線程中的異常JavaFX應用程序線程無法投射
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: com.WesternAdvance.Lasercontroller.LaserControllerUI cannot be cast to com.WesternAdvance.Lasercontroller.LoginPageController
at com.WesternAdvance.Lasercontroller.LoginManager.showMainViewAdmin(LoginManager.java:60)
它指的是該生產線是
loader.<LoginPageController>getController();
下面是我在做什麼...
我有一個登錄頁面loginPage.java
創建這樣的情景:
public class LoginPage extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) throws IOException {
Scene scene = new Scene(new StackPane());
LoginManager loginManager = new LoginManager(scene);
loginManager.showLoginScreen();
stage.setScene(scene);
stage.show();
stage.setTitle("Laser Controller");
stage.setOnCloseRequest(e -> System.exit(1));
stage.setX(0);
stage.setY(0);
stage.sizeToScene();
}
}
然後我有一個LoginPageController.java
檢查密碼輸入和比較這跟這樣的哈希:
public void initManager(final LoginManager loginManager) {
login.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
String passwordhashadmin = "$2a$08$WV7EDELlZbLHqrcW5qpuSuc6Bk8PkpvttIqeBi1n0EwP.yLt.tg2K";
String passwordhashuser = "$2a$08$2eS3Mukla5T.rCC747/hj.FGrP2GHSWCgloP4ZBVuTROYIQWwo6lm";
if (BCrypt.checkpw(userId.getText() + passwordfield.getText(), passwordhashuser))
{
loginManager.showMainViewUser();//Login as User
}
else if (BCrypt.checkpw(userId.getText() + passwordfield.getText(), passwordhashadmin))
{
loginManager.showMainViewAdmin();
}
else{
errorMessage.setText("Login Incorrect!");
System.out.print("false");
}
}
}
然後根據其在LoginManager.java
用戶登錄應該打開正確的場景是這樣的...
void showMainViewUser() {
try {
FXMLLoader loader = new FXMLLoader(
getClass().getResource("LaserControllerUserUi.fxml")
);
scene.setRoot((Parent) loader.load());
LoginPageController controller =
loader.<LoginPageController>getController();
controller.initManager(this);
scene.getStylesheets().add(LaserControllerUI.class.getResource("LaserControllerUI.css").toExternalForm());
} catch (IOException ex) {
Logger.getLogger(LoginManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
void showMainViewAdmin() {
try {
FXMLLoader loader = new FXMLLoader(
getClass().getResource("LaserControllerAdminUi.fxml")
);
scene.setRoot((Parent) loader.load());
LoginPageController controller =
loader.<LoginPageController>getController();
controller.initManager(this);
scene.getStylesheets().add(LaserControllerUI.class.getResource("LaserControllerUI.css").toExternalForm());
} catch (IOException ex) {
Logger.getLogger(LoginManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
爲什麼我會得到我是錯誤?在我去之前,我正在踢自己不備份。但所有的部分學習經驗。