JavaFx 不会改变场景

标签 java user-interface javafx

package Data_Project;

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

import java.io.IOException;

public class Main extends Application {
    public Stage window;
    @Override
    public void start(Stage primaryStage) throws Exception{
        window = primaryStage;
        //Scene1
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        Parent root1 = FXMLLoader.load(getClass().getResource("Admin.fxml"));
        Controller a1 = new Controller();
        a1.getSubmitButton().setOnAction(e -> {
            window.setScene(new Scene(root1,500,500));
        });
        window.setTitle("Log in");
        window.setScene(new Scene(root,500,500));
        window.show();
    }


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

    }
}

嘿,我在这段代码中遇到错误,我不知道出了什么问题。

Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157) at com.sun.javafx.application.LauncherImpl$$Lambda$1/989110044.run(Unknown Source) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.NullPointerException at Data_Project.Main.start(Main.java:20)

XML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Data_Project.Controller">
   <children>

          <Label text="Username" GridPane.columnIndex="1" GridPane.rowIndex="1" />
          <TextField fx:id="userField" GridPane.columnIndex="2" GridPane.rowIndex="1" />
          <Label text="Password" GridPane.columnIndex="1" GridPane.rowIndex="3" />
          <PasswordField fx:id="passField" GridPane.columnIndex="2" GridPane.columnSpan="1" GridPane.rowIndex="3" />
          <Button fx:id="submitButton" onAction="#submitForm" text="Log in" GridPane.columnIndex="2" GridPane.columnSpan="2" GridPane.rowIndex="4" />
          <Label fx:id="errorLabel" textFill="#c30808" GridPane.columnIndex="2" GridPane.columnSpan="2" GridPane.rowIndex="6" />
   </children>

</GridPane>






<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Data_Project.Admin">
    <children>

        <Label text="Username" GridPane.columnIndex="1" GridPane.rowIndex="1" />
        <TextField fx:id="userField" GridPane.columnIndex="2" GridPane.rowIndex="1" />
        <Label text="Password" GridPane.columnIndex="1" GridPane.rowIndex="3" />
        <PasswordField fx:id="passField" GridPane.columnIndex="2" GridPane.columnSpan="1" GridPane.rowIndex="3" />
        <Button fx:id="submitButton" text="Log in" GridPane.columnIndex="2" GridPane.columnSpan="2" GridPane.rowIndex="4" />
        <Label fx:id="errorLabel" textFill="#c30808" GridPane.columnIndex="2" GridPane.columnSpan="2" GridPane.rowIndex="6" />
    </children>

</GridPane>

XML 都是不同的文件,但此处显示为 1 个文件,它们具有相同的属性,而且都是因为它是用于测试场景切换器,但由于某种原因我无法让它工作!

Controller 类:

package Data_Project;


import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.*;


public class Controller{
    Scene window;
    @FXML Button submitButton;
    @FXML TextField userField;
    @FXML PasswordField passField;
    @FXML Label errorLabel;

    public void submitForm(ActionEvent actionEvent) {
       authorizedUser user = new authorizedUser();
        if(!user.checkCredentials(userField.getText(), passField.getText()))
        {
            errorLabel.setText("Invalid credentials");
        }
    }


    public Button getSubmitButton() {
        return submitButton;
    }
}

管理员:

package Data_Project;


import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.*;


public class Admin{
    Scene window;
    @FXML Button submitButton;
    @FXML TextField userField;
    @FXML PasswordField passField;
    @FXML Label errorLabel;

    public Button getSubmitButton() {
        return submitButton;
    }
}

最佳答案

FXMLLoader 解析 FXML 文件,创建 Controller 类的实例(如果指定了),并将任何带有 @FXML 注释的字段注入(inject)到该 Controller 实例中。

如果您创建自己的 Controller 实例,就像您所做的那样

Controller a1 = new Controller();

那么 FXMLLoader 对该实例一无所知(怎么会呢?),因此 @FXML 带注释的字段未初始化。因此当你打电话时

a1.getSubmitButton()

它返回null,因此您会得到一个NullPointerException

要解决此问题,请替换

Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

Controller a1 = new Controller();

FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();
Controller a1 = loader.getController();

这为您提供了对 FXMLLoader 创建的 Controller 实例的引用,因此它的 @FXML 注释字段已正确初始化。

关于JavaFx 不会改变场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28931816/

相关文章:

java - 如何在以编程方式选择项目时触发 ComboBox EventHandler?

java - XPATH 查询不返回结果

java - 如何重用 javadoc 来实现类似的功能

java - 如何从正则表达式中提取值并打印它们并对这些值进行计算

python - 如何仅使用kivy进行绑定(bind)?

iphone - UIDEVICE方向

java - 尝试取消选择 TableView 中的选定行时出现 IndexOutOfBoundsException

java - 使用 TextView.setText 显示数据时出错 : Resources$NotFoundException: String resource ID #0x0

actionscript-3 - 以 OOP 正确的方式访问父类

java - 写一个不扩展Application的javafx程序