面临使用JavaFX、FXMLLoader、fxml设置登录视图的问题

面临使用JavaFX、FXMLLoader、fxml设置登录视图的问题,javafx,fxml,Javafx,Fxml,因此,当我执行代码时,不会出现任何错误。 我的fxml中也有安装控制器,我在下面发布的代码是控制器和主类的代码 package realEstateApplication; import javafx.application.Application; //import javafx.fxml.FXMLLoader; //import javafx.scene.Parent; //import javafx.scene.Scene; import javafx.stage.Stage; impo

因此,当我执行代码时,不会出现任何错误。 我的fxml中也有安装控制器,我在下面发布的代码是控制器和主类的代码

package realEstateApplication;

import javafx.application.Application;
//import javafx.fxml.FXMLLoader;
//import javafx.scene.Parent;
//import javafx.scene.Scene;
import javafx.stage.Stage;
import realEstateApplication.controllers.loginViewController;

public class Main extends Application {

public Main() {
    System.out.println("Main invoked ... \n");
}

@Override
public void start(Stage primaryStage) throws Exception{
   // Parent loginRoot = FXMLLoader.load(getClass().getResource("views/loginView.fxml"));
    loginViewController loginMVC =  new loginViewController();

   // Parent login = FXMLLoader.load(getClass().getResource("views/loginView.fxml"));

   /* Parent admin = FXMLLoader.load(getClass().getResource("views/adminView.fxml"));
    Parent registerAdmin = FXMLLoader.load(getClass().getResource("views/registerAdminView.fxml"));
    Parent registerCustomer = FXMLLoader.load(getClass().getResource("views/registerCustomerView.fxml"));
    Parent registerCompany = FXMLLoader.load(getClass().getResource("views/registerCompanyView.fxml"));
    Parent registerFlat    = FXMLLoader.load(getClass().getResource("views/registerFlatView.fxml"));*/

   // Stage firstStage = new Stage();
   // firstStage.setTitle("Login");
   // firstStage.setScene(new Scene(loginRoot, 600, 400));
    //firstStage.show();

    /*Stage secondaryStage = new Stage();
    secondaryStage.setTitle("Administrator");
    secondaryStage.setScene(new Scene(admin, 600, 399));
    secondaryStage.show();

    Stage ternaryStage = new Stage();
    ternaryStage.setTitle("For new admins");
    ternaryStage.setScene(new Scene(registerAdmin, 600, 400));
    ternaryStage.show();

    Stage forthStage = new Stage();
    forthStage.setTitle("New customers register here");
    forthStage.setScene(new Scene(registerCustomer, 600, 400));
    forthStage.show();

    Stage pentaStage = new Stage();
    pentaStage.setTitle("Register new company here");
    pentaStage.setScene(new Scene(registerCompany, 600, 400));
    pentaStage.show();

    Stage hexaStage = new Stage();
    hexaStage.setTitle("Create flats here");
    hexaStage.setScene(new Scene(registerFlat, 600, 400));
    hexaStage.show();*/

}


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

以下是我的控制器代码:

package realEstateApplication.controllers;

import javafx.fxml.FXML;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;


/**
 * Created by PriteshJ on 4/16/16.
 */
public class loginViewController extends Application {
    @FXML private TextField username_tField;
    @FXML private PasswordField password_tField;
    @FXML private Button login_Button;
    @FXML private Button signUp_Button;


    public loginViewController() {
        System.out.println("Login Controller constructor invoked ...\n");
    }

    @Override
    public void start(Stage loginStage) throws Exception {
        Parent loginRoot = FXMLLoader.load(getClass().getResource("views/loginView.fxml"));
        if(loginRoot == null)
            System.out.println("something weird happened .... ");
        loginStage.setTitle("Login");
        loginStage.setScene(new Scene(loginRoot, 600, 400));
        loginStage.initModality(Modality.WINDOW_MODAL);
        loginStage.show();
    }

}
(所以我尝试传递对父变量的引用,该引用指向我的fxml文件,该文件在start方法的作用域内,因为我的主类扩展了应用程序(javafx),这给了我NullPointerException。 试图使其静止,但也不起作用。 然后我尝试了我现在发布的方法,它没有错误,但都不起作用。)

我试图在JavaFX中从MVC的角度进行思考。所以我得到了一个loginViewController,我依靠它完全拥有login.fxml并对其执行任何操作。我得到了一个主类,JavaFX应用程序在其中启动,但它只能从我的viewController的一个对象构造我的视图。我看了很多例子,但到目前为止,我还没有找到一个例子来展示如何做我想做的事情


期待您的建议。

我建议您查看一下。我在控制器类中没有调用initialize的可能重复项。这就是问题所在。我设法解决了这个问题。非常感谢您的帮助:)我建议您查看一下。我在控制器类中没有调用initialize的可能重复项。这就是问题所在。我设法解决了这个问题。非常感谢您的帮助:)