如何在使用JavaFX进行多窗体操作时调用另一个控制器中的方法?

huangapple go评论108阅读模式
英文:

How to call a method in another controller when multiforming with JavaFX?

问题

以下是翻译好的内容:

我想创建一个包含两个表单的项目,但是当我试图从第一个控制器调用第二个控制器中的一个方法时,出现了问题。为了简单起见,我已经创建了一个最小可重现示例。两个控制器分别如下:

第一个控制器:

  1. package sample;
  2. import javafx.fxml.FXML;
  3. import javafx.fxml.FXMLLoader;
  4. import javafx.scene.Parent;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Button;
  7. import javafx.event.ActionEvent;
  8. import javafx.stage.Stage;
  9. public class Controller {
  10. @FXML Button btnNew;
  11. public void switchHandler(ActionEvent evt) {
  12. try {
  13. launch(Main.getStage(), "sample2.fxml", "");
  14. Controller2 cont = new Controller2();
  15. cont.text();
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. private void launch(Stage primaryStage, String form, String title) throws Exception {
  21. Parent root = FXMLLoader.load(getClass().getResource(form));
  22. primaryStage.setTitle(title);
  23. primaryStage.setScene(new Scene(root, 600, 400));
  24. primaryStage.show();
  25. }
  26. }

第二个控制器:

  1. package sample;
  2. import javafx.fxml.FXML;
  3. import javafx.scene.control.TextField;
  4. public class Controller2 {
  5. @FXML
  6. TextField txt;
  7. public void text() {
  8. txt.setText("Hello World"); // 此行出错
  9. }
  10. }

两个表单如下:

第一个表单:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import javafx.scene.control.*?>
  3. <?import java.lang.*?>
  4. <?import javafx.scene.layout.*?>
  5. <?import javafx.geometry.Insets?>
  6. <?import javafx.scene.layout.GridPane?>
  7. <?import javafx.scene.control.Button?>
  8. <?import javafx.scene.control.Label?>
  9. <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
  10. <children>
  11. <Button fx:id="btnNew" onAction="#switchHandler" layoutX="213.0" layoutY="145.0" mnemonicParsing="false" text="Button" />
  12. </children>
  13. </AnchorPane>

第二个表单:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <?import java.lang.*?>
  3. <?import java.util.*?>
  4. <?import javafx.scene.*?>
  5. <?import javafx.scene.control.*?>
  6. <?import javafx.scene.layout.*?>
  7. <AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller2">
  8. <children>
  9. <TextField fx:id="txt" layoutX="151.0" layoutY="156.0" />
  10. </children>
  11. </AnchorPane>

txt.setText("Hello World"); 这行代码会产生一个 NullPointerException。有人能告诉我为什么以及如何解决这个问题吗?

英文:

I want to create a project with two forms, but now there's a problem when I try to call a method in the second controller from the first controller. I've created a minimal reproducible example for simplicity. The two controllers are as follows:

  1. package sample;
  2. import javafx.fxml.FXML;
  3. import javafx.fxml.FXMLLoader;
  4. import javafx.scene.Parent;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Button;
  7. import javafx.event.ActionEvent;
  8. import javafx.stage.Stage;
  9. public class Controller {
  10. @FXML Button btnNew;
  11. public void switchHandler (ActionEvent evt) {
  12. try {
  13. launch(Main.getStage(), &quot;sample2.fxml&quot;, &quot;&quot;);
  14. Controller2 cont = new Controller2();
  15. cont.text();
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. private void launch(Stage primaryStage, String form, String title) throws Exception {
  21. Parent root = FXMLLoader.load(getClass().getResource(form));
  22. primaryStage.setTitle(title);
  23. primaryStage.setScene(new Scene(root, 600, 400));
  24. primaryStage.show();
  25. }
  26. }

And the second controller:

  1. package sample;
  2. import javafx.fxml.FXML;
  3. import javafx.scene.control.TextField;
  4. public class Controller2 {
  5. @FXML
  6. TextField txt;
  7. public void text() {
  8. txt.setText(&quot;Hello World&quot;); //this line goes wrong
  9. }
  10. }

The forms are as follows:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;?import javafx.scene.control.*?&gt;
  3. &lt;?import java.lang.*?&gt;
  4. &lt;?import javafx.scene.layout.*?&gt;
  5. &lt;?import javafx.geometry.Insets?&gt;
  6. &lt;?import javafx.scene.layout.GridPane?&gt;
  7. &lt;?import javafx.scene.control.Button?&gt;
  8. &lt;?import javafx.scene.control.Label?&gt;
  9. &lt;AnchorPane maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns=&quot;http://javafx.com/javafx/8&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;sample.Controller&quot;&gt;
  10. &lt;children&gt;
  11. &lt;Button fx:id=&quot;btnNew&quot; onAction=&quot;#switchHandler&quot; layoutX=&quot;213.0&quot; layoutY=&quot;145.0&quot; mnemonicParsing=&quot;false&quot; text=&quot;Button&quot; /&gt;
  12. &lt;/children&gt;
  13. &lt;/AnchorPane&gt;

And the second form:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;?import java.lang.*?&gt;
  3. &lt;?import java.util.*?&gt;
  4. &lt;?import javafx.scene.*?&gt;
  5. &lt;?import javafx.scene.control.*?&gt;
  6. &lt;?import javafx.scene.layout.*?&gt;
  7. &lt;AnchorPane prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns=&quot;http://javafx.com/javafx/8&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;sample.Controller2&quot;&gt;
  8. &lt;children&gt;
  9. &lt;TextField fx:id=&quot;txt&quot; layoutX=&quot;151.0&quot; layoutY=&quot;156.0&quot; /&gt;
  10. &lt;/children&gt;
  11. &lt;/AnchorPane&gt;

The line txt.setText(&quot;Hello World&quot;); produces a NullPointerException. Can someone tell me why and how to fix this problem?

答案1

得分: 1

一个小样例供您参考。只需将其替换为您的类名。

您应该像这样进行操作:

  1. FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample2.fxml"));
  2. Parent root = fxmlLoader.load();
  3. MyController controller = fxmlLoader.getController();
  4. ... //显示场景等。
  5. controller.text();
英文:

A small sample for you. Just replace with your classes.
You should do something like that:

  1. FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(&quot;/sample2.fxml&quot;));
  2. Parent root = fxmlLoader.load();
  3. MyController controller = fxmlLoader.getController();
  4. ... //show scene etc.
  5. controller.text();

huangapple
  • 本文由 发表于 2020年10月20日 12:29:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64438499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定