我需要在按下特定按钮时从MainController类中为ZoneController类中的标签设置文本。我总共有7个按钮。我在执行此任务时遇到麻烦。
Main Controller.java:
package application.controller;
import application.Main;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
public class MainController implements EventHandler<ActionEvent> {
@FXML Button zoneG, zoneR, zoneTY, zoneTR, zoneB, zoneD, zoneX;
public void handleG(ActionEvent event) {
ZoneController zone = new ZoneController();
zone.setZoneName("Test");
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/view/Zone.fxml"));
Main.stage.setScene(new Scene(root, 800, 800));
Main.stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public void handleR(ActionEvent event) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/view/Zone.fxml"));
Main.stage.setScene(new Scene(root, 800, 800));
Main.stage.show();
} catch(Exception e) {
e.printStackTrace();
} }
public void handleTY(ActionEvent event) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/view/Zone.fxml"));
Main.stage.setScene(new Scene(root, 800, 800));
Main.stage.show();
} catch(Exception e) {
e.printStackTrace();
} }
public void handleTR(ActionEvent event) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/view/Zone.fxml"));
Main.stage.setScene(new Scene(root, 800, 800));
Main.stage.show();
} catch(Exception e) {
e.printStackTrace();
} }
public void handleB(ActionEvent event) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/view/Zone.fxml"));
Main.stage.setScene(new Scene(root, 800, 800));
Main.stage.show();
} catch(Exception e) {
e.printStackTrace();
} }
public void handleD(ActionEvent event) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/view/Zone.fxml"));
Main.stage.setScene(new Scene(root, 800, 800));
Main.stage.show();
} catch(Exception e) {
e.printStackTrace();
} }
public void handleX(ActionEvent event) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/view/Zone.fxml"));
Main.stage.setScene(new Scene(root, 800, 800));
Main.stage.show();
} catch(Exception e) {
e.printStackTrace();
} }
@Override
public void handle(ActionEvent event) {
// TODO Auto-generated method stub
}
}
ZoneController.java:
package application.controller;
import java.net.URL;
import java.util.ResourceBundle;
import application.Main;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
public class ZoneController implements Initializable,
EventHandler<ActionEvent>{
@FXML Button homeButton, addButton, relocateButton;
@FXML TextField addDinoName, addDinoType, relocateDinoName,
relocateDinoZone, numDino, levelThreat;
@FXML RadioButton yesCarnivore, noCarnivore;
@FXML Label zoneName;
public void setZoneName(String name) {
zoneName.setText(name);
}
@Override
public void handle(ActionEvent event) {
try {
Parent root =
FXMLLoader.load(getClass().getResource("/application/view/Main.fxml"));
Main.stage.setScene(new Scene(root, 800, 800));
Main.stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
}
我尝试使用所需的正确字符串在MainController
内制作一个占位符标签,然后从ZoneController
进行调用以尝试从占位符标签获取文本。例如:
MainController {
@FXML Label placeholder;
public void handleG(ActionEvent event {
placeholder.setText("G-Zone");
}
然后进入:
ZoneController {
MainController user = new MainController();
zoneName.setText(user.placeholder.getText());
}
但是这样做会给我这个错误:
javafx.fxml.LoadException:
/C:/Users/admin/eclipse-workspace/gys415-lab6/bin/application/view/Zone.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.controller.MainController.handleB(MainController.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
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:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
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$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at
application.controller.ZoneController.initialize(ZoneController.java:47)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 66 more
感谢任何帮助或指导。