gpt4 book ai didi

java - 将 2 个变量从一个 Controller 传递到另一个 Controller

转载 作者:行者123 更新时间:2023-12-01 10:21:36 24 4
gpt4 key购买 nike

我目前有 3 个 java 类(Main、FileSelector 和 FileDisplayer)以及 2 个 FXML 文件(FileSelector.fxml 和 FileDisplayer.fxml)

我的目标是最初显示一个带有 3 个按钮的窗口,其中 2 个用于用户选择文件,第三个用于提交文件。然后,我希望关闭该窗口,并打开另一个窗口,其中有 2 个标签,这些标签将这 2 个文件的路径名显示为字符串。

Main.java:

public class Main extends Application {

@Override
public void start(Stage stage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/FileSelector.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Select Files");
stage.setScene(scene);
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

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

文件选择器.java:

public class FileSelector {

@FXML
private Button file1;

@FXML
private Button file2;

@FXML
private Button go;

@FXML
private Label file1Label;

@FXML
private Label file2Label;

@FXML
public void selectFile1(ActionEvent event) {
final FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select file1");
File file = fileChooser.showOpenDialog(null);
file1Label.setText(file.toString());
}

@FXML
public void selectFile2(ActionEvent event) {
final FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select file2");
File file = fileChooser.showOpenDialog(null);
file2Label.setText(file.toString());
}

@FXML
public void goButton(ActionEvent event) {
if (!(file1Label.getText().equals("") || file2Label.getText().equals(""))) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/FileDisplayer.fxml"));

Parent root = (Parent) loader.load();
FileDisplayer fileDisplayer = loader.getController();
fileDisplayer.setFile1(file1Label.getText());
fileDisplayer.setFile2(file2Label.getText());
Scene scene = new Scene(root);

Stage stage = new Stage();

stage.setScene(scene);

stage.show();

((Node)(event.getSource())).getScene().getWindow().hide();

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

FileDisplayer.java:

public class FileDisplayer {

@FXML
private Label file1Label;

@FXML
private Label file2Label;

public void setFile1(String file1Path) {
this.file1Label.setText(file1Path);
}

public void setFile2(String file2Path) {
this.file2Label.setText(file2Path);
}

}

文件选择器.fxml:

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

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

<AnchorPane prefHeight="150.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FileSelector">
<children>
<Button fx:id="file1" layoutX="25.0" layoutY="20.0" minWidth="130.0" mnemonicParsing="false" onAction="#selectFile1" prefWidth="130.0" text="Select File 1" />
<Button fx:id="file2" layoutX="25.0" layoutY="66.0" minWidth="130.0" mnemonicParsing="false" onAction="#selectFile2" prefWidth="130.0" text="Select File 2" />
<Button fx:id="go" layoutX="230.0" layoutY="109.0" mnemonicParsing="false" onAction="#goButton" prefWidth="40.0" text="Go" />
<Label fx:id="file1Label" layoutX="174.0" layoutY="20.0" prefHeight="25.0" prefWidth="310.0" />
<Label fx:id="file2Label" layoutX="174.0" layoutY="66.0" prefHeight="25.0" prefWidth="310.0" />
</children>
</AnchorPane>

文件显示器.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="300.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="file1Label" layoutX="68.0" layoutY="56.0" prefHeight="56.0" prefWidth="264.0" textAlignment="CENTER" />
<Label fx:id="file2Label" layoutX="68.0" layoutY="150.0" prefHeight="56.0" prefWidth="264.0" textAlignment="CENTER" />
</children>
</AnchorPane>

我遵循了其他问题中的许多不同示例,但我不断收到 NullPointerException,除非我删除对 setfile_ 方法的调用,这只会给我一个空白的第二个窗口。

编辑

这是完整的堆栈跟踪:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException
at application.FileSelector.goButton(FileSelector.java:58)
... 58 more

最佳答案

您没有在 FileDisplayer.fxml 的根节点上设置 fx:controller。这意味着 loader.getController (在 FileSelector#goButton 中)可能返回 null。

关于java - 将 2 个变量从一个 Controller 传递到另一个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35562547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com