实例化时Javafx控制器无效

发布于 2025-02-13 13:59:01 字数 2796 浏览 0 评论 0原文

我已经陷入了几个小时。我不知道为什么我的Javafx的控制器指向NULL。我在场景构建器中制作了Javafx GUI,并使用其适当的骨架代码将控制器类添加到我的项目中。但是,当我在start方法中实例化控制器时,它指向NULL。

app.java

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

public class App extends Application{
    public static void main(String[] args) {
        launch(args);
    }

    public static SampleController controller;

    @Override
    public void start(Stage stage) throws Exception {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("testProjectFx.fxml"));
            Parent root = loader.load();

            controller = (SampleController) loader.getController();
            System.out.println("controller is : " + controller);

            Scene scene = new Scene(root, 300, 300);

            stage.setTitle("FXML Welcome");
            stage.setScene(scene);
            stage.show();

        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

samplecontroller.java

import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class SampleController {

    @FXML
    private Label firstLabel;

}

testprojectfx.fxml

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label fx:id="firstLabel" layoutX="314.0" layoutY="135.0" text="Label" />
   </children>
</AnchorPane>

错误stacktrace

cd /home/hexaquarks/vscode/testProject ; /usr/bin/env /usr/lib/jvm/java-11-openjdk-amd64/bin/java --module-path /home/hexaquarks/Downloads/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml,javafx.swing -Dfile.encoding=UTF-8 @/tmp/cp_s5yrrw0jqsav930qkowo7m19.argfile App 
Aug. 11, 2021 1:30:27 P.M. javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 16 by JavaFX runtime of version 11.0.2
controller is : null

带有文件夹树:

Testproject
├── .vscode
├── bin
├── lib
└── src
    ├── App.java
    ├── SampleController.java
    └── testProjectFx.fxml

我遵循 tery 和其他一些stackoverflow页面。我正与提到的完全重现指令,但我仍然将控制器视为null。

我没有想法,有人知道我在做什么错吗?

I have been stuck on this for hours. I don't know why my controller from JavaFX is pointing to null. I made a JavaFX GUI in scene builder, added the controller class to my project with its appropriate skeleton code. But when I instantiate the controller in my start method it is pointing to null.

App.java

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

public class App extends Application{
    public static void main(String[] args) {
        launch(args);
    }

    public static SampleController controller;

    @Override
    public void start(Stage stage) throws Exception {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("testProjectFx.fxml"));
            Parent root = loader.load();

            controller = (SampleController) loader.getController();
            System.out.println("controller is : " + controller);

            Scene scene = new Scene(root, 300, 300);

            stage.setTitle("FXML Welcome");
            stage.setScene(scene);
            stage.show();

        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

SampleController.java

import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class SampleController {

    @FXML
    private Label firstLabel;

}

testProjectFx.fxml

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label fx:id="firstLabel" layoutX="314.0" layoutY="135.0" text="Label" />
   </children>
</AnchorPane>

Error stacktrace

cd /home/hexaquarks/vscode/testProject ; /usr/bin/env /usr/lib/jvm/java-11-openjdk-amd64/bin/java --module-path /home/hexaquarks/Downloads/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml,javafx.swing -Dfile.encoding=UTF-8 @/tmp/cp_s5yrrw0jqsav930qkowo7m19.argfile App 
Aug. 11, 2021 1:30:27 P.M. javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 16 by JavaFX runtime of version 11.0.2
controller is : null

with folder tree:

Testproject
├── .vscode
├── bin
├── lib
└── src
    ├── App.java
    ├── SampleController.java
    └── testProjectFx.fxml

I followed the instructions mentionned in here and here, and some other StackOverflow pages. I am reproducing the instructions exactly as they are mentioned but I still get my controller as null.

I am short of ideas, does anyone know what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女净妖师 2025-02-20 13:59:01

我需要在.fxml文档中手动添加fx:controler =“ samplecontroller”,在我的情况下是&lt; anchorpane&gt; 代码>标签,现在起作用。

<AnchorPane (...) fx:controller="SampleController">
   <children>
      (...)
   </children>
</AnchorPane>

如果一个人使用的是“场景构建器”,则将其输入左下的“控制器”选项卡,然后在文本字段“控制器类”中输入“ start()方法中给出的所选实例名称”。

I needed to manually add fx:controller="SampleController" in the root tag of the .fxml document which in my case was the <AnchorPane> tag, now it works.

<AnchorPane (...) fx:controller="SampleController">
   <children>
      (...)
   </children>
</AnchorPane>

If one is using "Scene Builder" then go in the bottom left "controller" tab and input in the text field "controller class" the chosen instance name that is given in the start() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文