在JavaFX中如何在窗口之间传递对象
我是java新手,需要帮助理解对象。我试图用javaFx制作一个应用程序,但是在窗口之间传递对象时遇到了麻烦。目前,有一个登录窗口,如果通过检查数据库,登录详细信息正确,仪表板窗口将打开。但是,我现在需要在JavaFX中如何在窗口之间传递对象,java,javafx,fxml,Java,Javafx,Fxml,我是java新手,需要帮助理解对象。我试图用javaFx制作一个应用程序,但是在窗口之间传递对象时遇到了麻烦。目前,有一个登录窗口,如果通过检查数据库,登录详细信息正确,仪表板窗口将打开。但是,我现在需要BackendInterface对象,我刚刚在仪表板窗口中按下login按钮时创建了该对象,以从BackendInterface类调用其方法。我现在给出了一个空指针异常 将构造函数添加到LoginController类中,该类传递this.backendInterface将产生fxml加载异常
BackendInterface
对象,我刚刚在仪表板窗口中按下login按钮时创建了该对象,以从BackendInterface
类调用其方法。我现在给出了一个空指针异常
将构造函数添加到LoginController
类中,该类传递this.backendInterface
将产生fxml加载异常
所以我的问题是,如何将实例化对象传递给另一个窗口以使用它的方法
Main
public class MainGui extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
primaryStage.setTitle("Login");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
登录按钮的登录控制器
public class LoginController {
BackendInterface backendInterface;
@FXML
TextField username;
@FXML
PasswordField password;
@FXML
Button loginButton;
@FXML
Label loginLabel;
@FXML
public void loginButtonPress(){
if (username.getText().isEmpty() == true || password.getText().isEmpty() == true ) {
loginLabel.setText("Please enter data in the fields below");
} else {
//initialises backend interface with username and password
backendInterface = new BackendInterface(username.getText(), password.getText().toCharArray());
//opens a connection to the database
backendInterface.openConnection();
if (backendInterface.getConnectionResponse() == "success"){
//return and print response
System.out.println(backendInterface.getConnectionResponse());
//directs the user to the dashboard after successful login
try{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("dashboard.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Welcome " + username.getText());
stage.setScene(new Scene(root1));
stage.show();
//Closes the login screen window
Stage stage2 = (Stage) loginButton.getScene().getWindow();
stage2.hide();
} catch (Exception e){
e.printStackTrace();
}
} else {
//warns user of invalid login
loginLabel.setText(backendInterface.getConnectionResponse());
}
}
}
}
仪表板上按钮的仪表板控制器
public class DashboardController {
@FXML
public void doStuff(){
LoginController loginController = new LoginController();
loginController.backendInterface.printSomething();
}
}
基于Clayns响应的更新解决方案:
第一个控制器加载新页面并传递对象
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("dashboard.fxml"));
loader.load();
Parent p = loader.getRoot();
Stage stage = new Stage();
stage.setScene(new Scene(p));
DashboardController dashboardController = loader.getController();
dashboardController.setBackendInterface(backendInterface);
第二个控制器检索对象方法
public void setBackendInterface(BackendInterface backendInterface) {
this.backendInterface = backendInterface;
}
您正在
仪表板控制器
中创建一个新的登录控制器
,该控制器与用于登录的控制器无关
您应该为DashboardController提供设置后端接口的方法,然后在LoginController中使用fxmlLoader.getController()
获取DashboardController并将后端接口传递给它
编辑:
fabian的回答与您在
仪表板控制器
中创建的新登录控制器
的工作方式相同,该控制器与用于登录的控制器无关
您应该为DashboardController提供设置后端接口的方法,然后在LoginController中使用fxmlLoader.getController()
获取DashboardController并将后端接口传递给它
编辑:
费边的回答也是这样