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

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

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

问题

以下是翻译好的内容:

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

第一个控制器:

package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.stage.Stage;

public class Controller {
    @FXML Button btnNew;

    public void switchHandler(ActionEvent evt) {
        try {
            launch(Main.getStage(), "sample2.fxml", "");
            Controller2 cont = new Controller2();
            cont.text();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void launch(Stage primaryStage, String form, String title) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource(form));
        primaryStage.setTitle(title);
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show();
    }
}

第二个控制器:

package sample;

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

public class Controller2 {
    @FXML
    TextField txt;

    public void text() {
        txt.setText("Hello World"); // 此行出错
    }
}

两个表单如下:

第一个表单:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>


<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">
   <children>
      <Button fx:id="btnNew" onAction="#switchHandler" layoutX="213.0" layoutY="145.0" mnemonicParsing="false" text="Button" />
   </children>
</AnchorPane>

第二个表单:

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller2">
   <children>
      <TextField fx:id="txt" layoutX="151.0" layoutY="156.0" />
   </children>
</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:

package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.stage.Stage;

public class Controller {
    @FXML Button btnNew;

    public void switchHandler (ActionEvent evt) {
        try {
            launch(Main.getStage(), &quot;sample2.fxml&quot;, &quot;&quot;);
            Controller2 cont = new Controller2();
            cont.text();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void launch(Stage primaryStage, String form, String title) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource(form));
        primaryStage.setTitle(title);
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.show();
    }
}

And the second controller:

package sample;

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

public class Controller2 {
    @FXML
    TextField txt;

    public void text() {
        txt.setText(&quot;Hello World&quot;); //this line goes wrong
    }
}

The forms are as follows:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;?import javafx.scene.control.*?&gt;
&lt;?import java.lang.*?&gt;
&lt;?import javafx.scene.layout.*?&gt;
&lt;?import javafx.geometry.Insets?&gt;
&lt;?import javafx.scene.layout.GridPane?&gt;
&lt;?import javafx.scene.control.Button?&gt;
&lt;?import javafx.scene.control.Label?&gt;


&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;
   &lt;children&gt;
      &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;
   &lt;/children&gt;
&lt;/AnchorPane&gt;

And the second form:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;?import java.lang.*?&gt;
&lt;?import java.util.*?&gt;
&lt;?import javafx.scene.*?&gt;
&lt;?import javafx.scene.control.*?&gt;
&lt;?import javafx.scene.layout.*?&gt;


&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;
   &lt;children&gt;
      &lt;TextField fx:id=&quot;txt&quot; layoutX=&quot;151.0&quot; layoutY=&quot;156.0&quot; /&gt;
   &lt;/children&gt;
&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

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

您应该像这样进行操作:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample2.fxml"));
Parent root = fxmlLoader.load();
MyController controller = fxmlLoader.getController();

... //显示场景等。

controller.text();
英文:

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

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(&quot;/sample2.fxml&quot;));
Parent root = fxmlLoader.load();
MyController controller = fxmlLoader.getController();

... //show scene etc.

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:

确定