英文:
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(), "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();
}
}
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("Hello World"); //this line goes wrong
}
}
The forms are as follows:
<?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>
And the second form:
<?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>
The line txt.setText("Hello World");
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("/sample2.fxml"));
Parent root = fxmlLoader.load();
MyController controller = fxmlLoader.getController();
... //show scene etc.
controller.text();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论