JavaFX(无法调用“javafx.stage.Stage.setScene(javafx.scene.Scene)”,因为“this.stage”为空)解决方案?

问题描述 投票:0回答:1

我正在尝试用按钮切换舞台上的场景。起始场景是登录屏幕,从那里转换到仪表板场景没有任何问题。问题是当我调用 clickAccount 方法时。

我检查了 fxid 是否正确,并且如果我将它们初始化为第一个场景,所有 fxml 文件都将正确加载。

package ui;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import logic.UserDriver;

import java.io.IOException;


public class BudgetApplication extends Application {
    public UserDriver userDriver;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        // Initialize UserDriver
        userDriver = new UserDriver();

        // Load FXML and get controller
        FXMLLoader loader = new FXMLLoader(getClass().getResource("login.fxml"));
        Parent root = loader.load();

        // Load dashboard FXML
        FXMLLoader dashboardLoader = new FXMLLoader(getClass().getResource("dashboard.fxml"));
        Parent dashboardRoot = dashboardLoader.load();

        // Initialize dashboardScene
        Scene dashboardScene = new Scene(dashboardRoot, 1280, 720);

        //Load account FXML
        FXMLLoader accountLoader = new FXMLLoader(getClass().getResource("accounts.fxml"));
        Parent accountRoot = accountLoader.load();

        //Initialize accountScene
        Scene accountScene = new Scene(accountRoot, 1280, 720);

        // Get the controller instance and set up dependencies
        BudgetController controller = loader.getController();
        controller.setUserDriver(userDriver);  // Pass userDriver to the controller
        controller.setStage(primaryStage);     // Pass the stage to the controller if needed
        controller.setDashboardScene(dashboardScene); // Pass dashboardScene to controller
        controller.setAccountScene(accountScene); //Pass accountScene to controller

        // Set the primary scene
        Scene loginScene = new Scene(root, 500, 300);
        primaryStage.setScene(loginScene);
        primaryStage.setTitle("JavaBucks");
        primaryStage.setOnCloseRequest(e -> controller.windowClose("UserInfo.ser"));
        primaryStage.show();
    }


}

package ui;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import logic.UserDriver;
import java.io.IOException;

public class BudgetController {

    @FXML public TextField usernameTF;
    @FXML public TextField passwordTF;
    @FXML public Button loginButton;
    @FXML public Button accountButton;

    private UserDriver userDriver;
    private Stage stage;
    private Scene dashboardScene;
    private Scene accountScene;
    private Parent root;
    // Setters to pass data from BudgetApplication
    public void setUserDriver(UserDriver userDriver) {
        this.userDriver = userDriver;
    }

    public void setStage(Stage stage) {
        this.stage = stage;
        System.out.println("Stage set in controller: " + (stage != null));
    }

    public void setDashboardScene(Scene dashboardScene){
        this.dashboardScene = dashboardScene;
    }
    public void setAccountScene(Scene accountScene){
        this.accountScene = accountScene;
    }

    @FXML
    public void clickLogin(ActionEvent event) throws IOException {
        String username = usernameTF.getText();
        String password = passwordTF.getText();

        // Debug output
        System.out.println("Username: " + username);
        System.out.println("Password: " + password);

        // Validate login
        if (userDriver != null && userDriver.validateLogin(username, password)) {
            System.out.println("Login successful!");
            usernameTF.clear();
            passwordTF.clear();
            stage.setScene(dashboardScene);

        } else {
            System.out.println("Login failed.");
            usernameTF.clear();
            passwordTF.clear();
        }
    }
    @FXML
    public void clickAccount() {
        System.out.println("Button binded");
        stage.setScene(accountScene);
    }

    public void windowClose(String filename) {
        userDriver.writeToFile(filename);
        System.out.println("Data saved to " + filename);
    }
}


java user-interface javafx
1个回答
0
投票

像在任何控制器中一样获取舞台,并且不使用设置器:

Stage Stage = (Stage) anyNodeInYourController.getScene().getWindow();
© www.soinside.com 2019 - 2024. All rights reserved.