【发布时间】:2017-10-04 23:19:38
【问题描述】:
所以我正在为我在学校的项目制作纸牌游戏。它使用图形用户界面向用户显示卡片。
我在 intellij 中使用 Java FXML
我只是想让一切正常工作,然后再让它看起来很漂亮,但卡片是附有图像的按钮。
我查找了如何将图像添加到按钮上,而 JavaFX 的主要指南对我不起作用。我也尝试过搜索并找到了一个 - Loading image resource - 但我完全按照说明进行操作,但仍然出现错误:
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)
... 48 more
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Image.java:1128)
at javafx.scene.image.Image.<init>(Image.java:706)
at sample.Controller.cardClickHandler(Controller.java:26)
... 58 more
这是我的 IDE 与目录的图片:http://imgur.com/a/B9U64
我想要的是,当点击按钮时,图像会变成梅花王牌(我会做到,当点击交易按钮时,它们都会变成随机的,但这是在我完成这项工作之后发生的)
这里是atm的代码:
public void cardClickHandler(ActionEvent actionEvent)
{
Button button = (Button)actionEvent.getSource();
Integer row = GridPane.getRowIndex(button);
Integer column = GridPane.getColumnIndex(button);
if (row==1)
{
System.out.println("Opponent's Hand, Card "+Integer.toString(column+1));
}
if (row==3)
{
System.out.println("Your Hand, Card "+Integer.toString(column+1));
}
Image image = new Image(getClass().getResourceAsStream("/resources/images/Ace-of-Clubs.png"));
ImageView imageView = new ImageView(image);
imageView.setFitHeight(80);
imageView.setFitWidth(50);
button.setGraphic(imageView);
}
图片的网址必须是什么?还是我设置错了?
任何帮助将不胜感激:)
谢谢
【问题讨论】:
标签: java image path resources fxml