gpt4 book ai didi

JavaFX - 手动将图像添加到 GridPane

转载 作者:行者123 更新时间:2023-11-30 02:03:01 28 4
gpt4 key购买 nike

我有以下设置

enter image description here

这三个井字游戏板是 JavaFx 中的网格 Pane 。

我正在尝试一种非常简单的方法,将 ImageView 添加到网格节点,以指示它是 x 还是 o。

我正在使用的代码是。

private void handleNewGameAction(ActionEvent event) throws IOException {

FXMLLoader fxmlloader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));

GridPane pane = (GridPane)fxmlloader.getNamespace().get("topGrid");

Image image = new Image("x.png");
pane.add(new ImageView(image), 0, 0);

}

我收到的错误是

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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
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.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
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.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(Thread.java:748)
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)
... 43 more
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1118)
at javafx.scene.image.Image.<init>(Image.java:620)
at pkg3dtictactoe.FXMLDocumentController.handleNewGameAction(FXMLDocumentController.java:40)
... 53 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1110)
... 55 more

我真的很茫然,我一直在查看堆栈溢出并看到与我写的完全一样的答案,但我仍然收到上面的错误。

最佳答案

首先,回答眼前的问题,错误的最终原因由堆栈跟踪给出:

Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1110)
... 55 more

这意味着您提供给 Image 构造函数的 URL ("x.png") 无效。要么您使用了错误的路径,要么资源不在您认为的位置。您在评论中提到您的图像文件与 .java 文件位于同一目录中。如果您使用 Maven 或 Gradle,资源的默认位置是 src/main/resources (对于 *.java 则为 src/main/java > 文件)。如果这是一个纯 NetBeans JavaFX 项目(可能使用 Ant?),我不知道资源应该去哪里。查看是否有指定资源的目录,如果有,则将所有资源文件移至该目录。

您的代码还有其他一些问题:

private void handleNewGameAction(ActionEvent event) throws IOException {

FXMLLoader fxmlloader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));

GridPane pane = (GridPane)fxmlloader.getNamespace().get("topGrid");

Image image = new Image("x.png");
pane.add(new ImageView(image), 0, 0);

}

第一期涉及fxmlloader.getNamespace().get("topGrid")。您正在新创建的 FXMLLoader 上调用此函数,但尚未调用 load()。此调用的结果将始终为 null,因为 FXMLLoader 尚未实际实例化任何内容。

第二个(在我看来更大)问题涉及每次调用此方法时都会创建一个新的 GridPane 的事实。这个新的 GridPane 永远不会添加到任何 Scene 中,因此 GUI 永远不会更新。需要明确的是,您在方法中检索的GridPane与已经显示的不同。当该方法退出时,GridPane 超出范围并被垃圾收集。

由于您正在创建游戏板,因此您应该只需要加载板一次(或者可能每个游戏一次)。之后,您可以使用加载的板添加图像。您应该使用与 FXML 文件链接的 Controller 类。

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;

public class Controller {

@FXML
private GridPane topGrid;

@FXML
private void handleAction(ActionEvent event) {
topGrid.add(new ImageView(/*image*/), 0, 0);
event.consume();
}

}

要将 Controller 链接到 FXML 文件,您可以使用文件中的 fx:controller 属性或者您可以使用 FXMLLoader.setController(Object) 在调用实例 load() 方法之前。要检索 Controller ,您可以在调用 load 后使用 FXMLLoader.getController()

要了解有关 JavaFX 中的 FXML 的更多信息,请参阅 Introduction to FXML .

关于JavaFX - 手动将图像添加到 GridPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52193490/

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