簡體   English   中英

JavaFX Scene Builder訪問不同類中的方法

[英]JavaFX Scene Builder Accessing a Method in a different Class

我一直在尋找解決此問題的方法已有一段時間,但一直找不到。

我基本上是JavaFX,Scene Builder,FXML,CSS的新手。 在此之前,我有3個月的Java經驗,僅此而已。

我創建了一個項目; JavaFX。 我在該項目中創建了兩個包; 控制器和視圖。 在控制器程序包中有兩個文件。 Login.java和AdministratorHomescreen.java。 在視圖包中有四個文件。 QuizApp.fxml,QuizApp.css,Login.fxml和AdministratorHomescreen.fxml。

我一直在谷歌搜索並遵循不同的教程來學習如何做各種事情,到目前為止,我一直都很好。 但是現在我已經站起來了。

當我的應用程序啟動並且用戶按下“登錄”按鈕時,我要做的就是將它們轉發到管理員主屏幕上。

這是Login.java中的代碼:

public class Login extends Application {

private BorderPane quizApp;
private Stage windowLogin;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage windowLogin) {
    this.windowLogin = windowLogin;
    this.windowLogin.setTitle("QuizApp - Please Login or Register");

    windowLogin.resizableProperty().setValue(Boolean.FALSE);

    applyQuizAppLayout();
    applyLoginLayout();
}

public void applyQuizAppLayout() {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(Login.class.getClassLoader().getResource("view/QuizApp.fxml"));
        quizApp = (BorderPane) fxmlLoader.load();

        Scene scene = new Scene(quizApp);
        windowLogin.setScene(scene);
        windowLogin.show();
    } catch (IOException exception) {
        exception.printStackTrace();
    }
}

public void applyLoginLayout() {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(Login.class.getClassLoader().getResource("view/Login.fxml"));
        AnchorPane windowLogin = (AnchorPane) fxmlLoader.load();
        quizApp.setCenter(windowLogin);
    } catch (IOException exception) {
        exception.printStackTrace();
    }
}

public Stage getWindowLogin() {
    return windowLogin;
}

Button loginButton = new Button();

@FXML
private void handleLoginButtonAction(ActionEvent event) {
        System.out.println("Login details correct.");
        // windowLogin.setScene(AdministratorHomescreen.start(windowAdministratorHomescreen));
    }
}
}

我注釋掉的最后一行代碼是我得到錯誤的地方。 該錯誤顯示為“無法解析為變量”。 該按鈕的作用程度與之不同,因此單擊該按鈕時,會顯示一條消息到控制台。

這是AdministratorHomepage.java中的代碼:

public class AdministratorHomescreen {

private BorderPane quizApp;
private Stage windowAdministratorHomescreen;

public void start(Stage windowAdministratorHomescreen) {
    this.windowAdministratorHomescreen = windowAdministratorHomescreen;
    this.windowAdministratorHomescreen.setTitle("QuizApp - Welcome Administrator");

    windowAdministratorHomescreen.resizableProperty().setValue(Boolean.FALSE);

    applyQuizAppLayout();
    applyWindowAdministratorHomescreenLayout();
}

public void applyQuizAppLayout() {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(Login.class.getClassLoader().getResource("view/QuizApp.fxml"));
        quizApp = (BorderPane) fxmlLoader.load();

        Scene scene = new Scene(quizApp);
        windowAdministratorHomescreen.setScene(scene);
        windowAdministratorHomescreen.show();
    } catch (IOException exception) {
        exception.printStackTrace();
    }
}

public void applyWindowAdministratorHomescreenLayout() {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(
                Login.class.getClassLoader().getResource("view/WindowAdministratorHomescreen.fxml"));
        AnchorPane windowAdministratorHomescreen = (AnchorPane) fxmlLoader.load();
        quizApp.setCenter(windowAdministratorHomescreen);
    } catch (IOException exception) {
        exception.printStackTrace();
    }
}

public Stage getWindowAdministratorHomescreen() {
    return windowAdministratorHomescreen;
}
}

因此,我知道編譯器無法找到我的變量,但是任何時候我在AdministratorHomescreen.java中更改Method時,都會引發大量新錯誤,因此我現在基本上只是在盤旋而已。

是否有任何解決方法? 我本來可以解決所有這些錯誤,但這最終還是一個學習曲線。

任何幫助深表感謝。

PS:當用戶單擊該按鈕時,我不僅要打開任何隨機場景,還想打開一個已經應用QuizApp.fxml,QuizApp.css和AdministratorLogin.fxml的場景,因此,代碼的布局也是如此。

在用於登錄按鈕的偵聽器中,您需要將相關信息傳遞到下一階段。

首先在登錄類中為您的登錄按鈕設置一個偵聽器

loginbutton.setOnAction(new EventHandler<ActionEvent>() {
   @Override
   public void handle(ActionEvent t) {
     try{
       if("the login is successful"){
         String temp = "Your relevant user information, like username or however you keep track of your data";
         windowLogin.hide();
         //You'll then make a new stage to pass the info to the administrator page
         AdministratorHomeScreen nextStage = new AdministratorHomeScreen(windowLogin, temp);
        }
        else{
              System.out.println("incorrect login);
            }
        }
catch(Exception e){
e.printStackTrace();
}
}
});

然后在您的AdministratorHomeScreen類中,您將

public AdministratorHomeScreen(Stage stage, String takenOver){
windowLogIn = stage;
temp = takenOver;
start(new Stage());
}

我很確定這幾乎涵蓋了它。

編輯:格式化代碼的最高位,使其更易於閱讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM