我对JavaFX相当陌生,我正在构建一个GUI游戏。我以前用Java编写过游戏的逻辑,现在我正试图将其与SceneBuilder集成。这种怀疑可能是非常基本和明显的,但我似乎不明白我错在哪里。
我必须在SceneBuilder画布上创建一个计时器标签,从java代码中提取时间并在GUI上更新。这个计时器不是一个普通的计时器,这就是为什么我不能在我的控制器中使用时钟。我必须使用从Java代码中获得的值。这是:
FXML控制器文件:
public class SampleController implements Initializable {
public SampleController getSampleController(){
return this;
}
@FXML private Label displayTime;
@FXML
public void updatePage(String data){
displayTime.setText(data);
}
@FXML
private void handleStartGameA1(ActionEvent event) throws InterruptedException{
System.out.println("Game has been started!");
World w = new World();
w.runGame();
}
FXML文件:
<Label id="displayTime" fx:id="displayTime" layoutX="381.0" layoutY="125.0" prefHeight="23.0" prefWidth="301.0">
<font>
<Font size="18.0" />
</font>
</Label>
Java文件:
public class World {
public static Clock WorldClock; // Clock class only to get the custom time
public World(){
WorldClock = new Clock();
public void runGame() throws InterruptedException{
for (int minute=0; minute<=WorldProperty.MaxMinutes/10; minute++){
// irrelevant code
FXMLLoader fxmlLoader = new FXMLLoader();
SampleController sc = new SampleController();
sc.getSampleController();
sc.updatePage(WorldClock.getTime());
}
}
}
运行时错误:
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:8413)
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:381)
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$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
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:498)
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:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 45 more
Caused by: java.lang.NullPointerException
at javafxapplication1.SampleController.updatePage(SampleController.java:41)
at World.World.runGame(World.java:102)
at javafxapplication1.SampleController.handleStartGameA1(SampleController.java:52)
... 55 more
出了什么问题?通过创建SampleController构造函数,我觉得自己很不传统,但我从在线资源中了解到它是有效的。另外,在updatePage(String data)
函数中,如果我不更新标签,只需打印一条语句系统。出来println(“时间为:”数据)
,它工作得非常好。这让我相信,由于某些原因,标签没有得到正确更新。我能做些什么来解决这个问题?
您不加载FXML,因此不会创建UI组件(包括标签),也不会将控制器连接到FXML加载程序。
你需要像
FXMLLoader fxmlLoader = new FXMLLoader();
SampleController sc = new SampleController();
fxmlLoader.setController(sc);
fxmlLoader.setLocation(getClass().getResource("path/to/file.fxml"));
Parent root = fxmlLoader.load();
// display root somewhere...
我正在IntelliJ中打开新的JavaFX项目,我安装了JDK的所有必要文件,但是在fxml文件中有多个错误“意外令牌”。打开新的javafx项目和fxml文件后立即显示此错误(这意味着我没有从基本fxml标记中添加或删除任何内容)。我能做什么呢?
我一直在做一个桌面应用程序,在那里我需要使用JavaFX。我在使用Scene Builder构建的eclipse上的JavaFX项目中创建了各种FXML文件。除了这个讨厌的文件(fxmlimglist.FXML)外,我加载任何FXML文件都没有问题。 其主要思想是,当按下一个按钮时,会出现一个新窗口。下面是该按钮的事件处理程序的代码: package explorer中的项目如下所示: https
我正在创建一个javafx GUI应用程序,我的项目是一个maven配置的项目。我希望能够在我的控制器中引用我的fxml文件,如下所示: 其中,我的main.fxml文件位于src/main/resources文件夹中,而我的控制器位于src/main/java文件夹中。我该怎么做呢?我的src/main/resources文件夹在构建路径中,并且我能够从src/main/java文件夹中的类调用
由于以前的Eclipse用户切换到IntelliJ,我希望在开发时能够从IDE内部访问项目外部的文件夹。 在Eclipse中,我使用了创建链接资源的特性。 我想知道IntelliJ中是否有任何功能可以帮助我以类似的方式访问外部文件和文件夹(例如,如果我想编辑位于项目外部的应用程序服务器的配置)。
因此,在我尝试编写JavaFX教程和FXML示例时。但是,每当我在.fxml文件中向GridPane中添加一些内容时,程序就会崩溃。它打开一个普通的GridPane,如果没有其他东西放在其中。 } 究竟是什么原因导致它坠毁?