gpt4 book ai didi

java - 通过另一个文件中的 Controller 实例引用在 MainController 中设置文本字段

转载 作者:行者123 更新时间:2023-12-02 08:57:08 27 4
gpt4 key购买 nike

我正在尝试从另一个文件设置主 Controller 中文本字段的文本。我读到this :

FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
MainController mc = (MainController) loader.getController();

是您应该如何获取对 Javafx 中 Controller 实例的引用。我尝试实现它,但在尝试设置 TextField 时它给了我一个 NullPointerException 。这是我的代码:

Main.java:

package application;

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


public class Main extends Application {

static MainController mc;

@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
Scene scene = new Scene(root,348,212);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Flextime calculator");
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("assets/icon.png")));
primaryStage.setResizable(false);

FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
mc = (MainController) loader.getController();

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

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

public static MainController getMc() {
return mc;
}
}

MainController.java:

public class MainController {

@FXML Label fxFlextime;
@FXML Label fxTotalFlextime;
@FXML TextField fxStarttime;
@FXML TextField fxEndtime;
@FXML TextField fxDailyWorktime;
@FXML TextField fxBreaktime;
@FXML TextField fxAddBreaktime;
@FXML TextField fxBreakGap;
@FXML CheckBox fxAddBreak;

//Create Model, Userconfig
Model model = new Model();
UserConfig config = new UserConfig();

public void initialize() {
config.initialize();
}
}

用户配置.java:

public class UserConfig {

public void initialize() {
insertDefault();
}

void insertDefault() {
List<String> prefValues = getPreference();
//FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
//MainController mc = (MainController) loader.getController();
// ^Tried also putting it here to see wether im referencing the variable wrong or something,
// so maybe im getting the Controller wrong? With mc.fxStarttime.setText("") it also didnt work here
Main.mc.fxStarttime.setText("");
Main.mc.fxEndtime.setText(prefValues.get(2));
Main.mc.fxDailyWorktime.setText(prefValues.get(3));
Main.mc.fxBreaktime.setText(prefValues.get(4));
Main.mc.fxAddBreaktime.setText(prefValues.get(5));
Main.mc.fxBreakGap.setText(prefValues.get(6));

}

static List<String> getPreference() {
// This will define a node in which the preferences can be stored
Preferences userPrefs = Preferences.userNodeForPackage(Main.class);

try {
String[] keys = userPrefs.keys();

if (keys == null) {
userPrefs.put("DEFStarttime" , "1");
userPrefs.put("DEFEndtime" , "");
userPrefs.put("DEFDailyWorktime", "");
userPrefs.put("DEFBreaktime" , "");
userPrefs.put("DEFAddBreaktime" , "");
userPrefs.put("DEFBreakGap" , "");
}
} catch (BackingStoreException ex) {
System.err.println(ex);
}

List<String> prefValues = new ArrayList<String>();
prefValues.add(userPrefs.get("DEFStarttime" , ""));
prefValues.add(userPrefs.get("DEFEndtime" , ""));
prefValues.add(userPrefs.get("DEFDailyWorktime" , ""));
prefValues.add(userPrefs.get("DEFBreaktime" , ""));
prefValues.add(userPrefs.get("DEFAddBreaktime" , ""));
prefValues.add(userPrefs.get("DEFBreakGap" , ""));
/*
for(String string : prefValues) {
System.out.println(string);
}
*/
return prefValues;
}
}

错误:

javafx.fxml.LoadException: 
/C:/eclipse-workspace/project/bin/application/main.fxml

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at application.Main.start(Main.java:18)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2591)
... 17 more
Caused by: java.lang.NullPointerException
at application.UserConfig.insertDefault(UserConfig.java:25)
at application.UserConfig.initialize(UserConfig.java:18)
at application.MainController.initialize(MainController.java:39)
... 28 more

我试图将其减少到最少的代码量。

编辑:这里是 Main.fxml:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.VBox?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="212.0" prefWidth="348.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
<children>
<MenuBar fx:id="fxMenuBar">
<menus>
<Menu fx:id="fxFileMenu" mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="fxExitItem" mnemonicParsing="false" onAction="#exitProgram" text="Exit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Settings">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<FlowPane alignment="CENTER_LEFT" prefHeight="25.0" prefWidth="403.0">
<children>
<Label alignment="CENTER" prefHeight="15.0" prefWidth="65.0" text="Starttime:" underline="true">
<FlowPane.margin>
<Insets left="10.0" top="10.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxStarttime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00:00">
<FlowPane.margin>
<Insets top="10.0" />
</FlowPane.margin></TextField>
<Label alignment="CENTER" prefHeight="17.0" prefWidth="112.0" text="Required daily hours:" underline="true">
<FlowPane.margin>
<Insets left="10.0" right="5.0" top="10.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxDailyWorktime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00.00">
<FlowPane.margin>
<Insets top="10.0" />
</FlowPane.margin></TextField>
</children>
</FlowPane>
<FlowPane alignment="CENTER_LEFT" prefHeight="36.0" prefWidth="300.0">
<children>
<Label alignment="CENTER" prefHeight="15.0" prefWidth="65.0" text="Endtime:" underline="true">
<FlowPane.margin>
<Insets left="10.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxEndtime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00:00" />
<Label alignment="CENTER" prefHeight="15.0" prefWidth="65.0" text="Breaktime:" underline="true">
<FlowPane.margin>
<Insets left="57.0" right="5.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxBreaktime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00" />
</children>
</FlowPane>
<FlowPane prefHeight="38.0" prefWidth="300.0">
<children>
<CheckBox fx:id="fxAddBreak" mnemonicParsing="false" text="Add:">
<FlowPane.margin>
<Insets left="20.0" top="5.0" />
</FlowPane.margin>
</CheckBox>
<TextField fx:id="fxAddBreaktime" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00">
<FlowPane.margin>
<Insets left="10.0" right="5.0" top="5.0" />
</FlowPane.margin>
</TextField>
<Label text="breaktime from ">
<FlowPane.margin>
<Insets top="5.0" />
</FlowPane.margin>
</Label>
<TextField fx:id="fxBreakGap" alignment="CENTER" prefHeight="25.0" prefWidth="45.0" promptText="00.00">
<FlowPane.margin>
<Insets top="5.0" />
</FlowPane.margin>
</TextField>
<Label text="working hours">
<FlowPane.margin>
<Insets left="5.0" top="5.0" />
</FlowPane.margin>
</Label>
</children>
</FlowPane>
<FlowPane alignment="CENTER_RIGHT" prefHeight="32.0" prefWidth="300.0">
<children>
<Label text="Flextime:">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Label>
<Label fx:id="fxFlextime" alignment="CENTER" prefHeight="10.0" prefWidth="35.0" text="0.00" textAlignment="CENTER">
<FlowPane.margin>
<Insets />
</FlowPane.margin>
</Label>
<Label text="h">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Label>
</children>
</FlowPane>
<FlowPane alignment="CENTER_RIGHT" prefHeight="32.0" prefWidth="300.0">
<children>
<Label text="Total flextime:" underline="true">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Label>
<Label fx:id="fxTotalFlextime" alignment="CENTER" prefHeight="10.0" prefWidth="35.0" text="0.00" textAlignment="CENTER">
<FlowPane.margin>
<Insets />
</FlowPane.margin>
</Label>
<Label text="h">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Label>
</children>
</FlowPane>
<FlowPane alignment="CENTER_RIGHT" prefHeight="38.0" prefWidth="300.0">
<children>
<Button mnemonicParsing="false" onAction="#calculate" text="Calculate" underline="true">
<FlowPane.margin>
<Insets right="10.0" />
</FlowPane.margin>
</Button>
<Button mnemonicParsing="false" onAction="#addToTotal" text="Add to total" />
<Button mnemonicParsing="false" onAction="#clear" text="Clear">
<FlowPane.margin>
<Insets left="10.0" right="10.0" />
</FlowPane.margin>
</Button>
</children>
</FlowPane>
</children>
</VBox>

最佳答案

您获得 NullPointerException 的原因是因为当您尝试访问 UserConfig 中的 Main.mc 字段时,Main.mc 为 null。

在加载 main.fxml 的行 Parent root = FXMLLoader.load(getClass().getResource("main.fxml")); initialize() Controller 中的方法MainController被调用,此时您正在调用 userConfig.initialize()这也向后引用 Main.mc此时是 null

您可以在 MainController 类中公开配置,并在设置 mc 后初始化用户配置。主类中的字段。或者您可以将 MainController 作为参数注入(inject)到 UserConfig 初始化方法中。

Main.java

FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
Parent root = (Parent) loader.load();
Scene scene = new Scene(root,348,212);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Flextime calculator");
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("assets/icon.png")));
primaryStage.setResizable(false);

mc = (MainController) loader.getController();
mc.config.initialize();

MainController.java

public UserConfig config = new UserConfig();

@FXML
public void initialize() {
//remove the config.initialize(); from here
}

关于java - 通过另一个文件中的 Controller 实例引用在 MainController 中设置文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60428893/

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