gpt4 book ai didi

java - 使用maven将照片添加到javafx中的按钮时出现问题

转载 作者:行者123 更新时间:2023-12-02 09:47:32 26 4
gpt4 key购买 nike

我正在做一些 Java FX 项目。不知何故,我无法将 icon.png 添加到按钮。

我尝试将图标放入资源文件夹中,甚至放入 Main.java 文件夹中这就是问题所在: icons.put("0", new Image(getClass().getResourceAsStream("icons/zero.png")));

private Map<String, Image> icons = new HashMap<>();

private void newGame(GridPane grid, Game game) {
grid.getChildren().clear();

bombAmount.setText(Integer.toString(game.getBombs()));

markedAmount.setText(Integer.toString(gra.getCounter()));
buttonBoard = new Button[game.getRow()][game.getCol()];
for (int r = 0; r < game.getRow(); r++) {
for (int c = 0; c < game.getCol(); c++) {
Button button = new Button();
button.setGraphic(new ImageView(icons.get("button")));
button.setStyle("-fx-background-color: transparent;");
final int rr = r;
final int cc = c;

button.setOnMouseClicked(event -> {
MouseButton b = event.getButton();

if (b == MouseButton.PRIMARY) {
if(gra.returnValue(rr, cc) == 9){
button.setGraphic(new ImageView(icons.get("bomb")));
}
else{
button.setGraphic(new ImageView(icons.get(Integer.toString(gra.returnValue(rr, cc)))));
}
button.setDisable(true);
show_nummber(rr, cc);
} else if (b == MouseButton.SECONDARY) {
System.out.println("secondary");
if (gra.getMarked(rr, cc) == 0) {
gra.addCounter();
} else if (gra.getMarked(rr, cc) == 1) {
gra.subCounter();
}
gra.addMarked(rr, cc);

if(gra.getMarked(rr, cc) == 0){
button.setGraphic(new ImageView(icons.get("button")));
}
else if(gra.getMarked(rr, cc) == 1){
button.setGraphic(new ImageView(icons.get("d")));
}
else if(gra.getMarked(rr, cc) == 2){
button.setGraphic(new ImageView(icons.get("q")));
}

markedAmount.setText(Integer.toString(gra.getCounter()));
System.out.println(gra.getCounter());
}
});

buttonBoard[r][c] = button;
grid.add(button, c, r);
}
}
}

private void show_nummber(int r, int c) { // funkcja do wyświetlania sąsiednich pól po otwarciu pola pustego
if (!this.gra.isEnd()) {
ArrayList<Point> retval = new ArrayList<>();
if (this.gra.check_neighbours(r, c, retval)) {
for (Point p : retval) {
this.buttonBoard[p.x][p.y].setGraphic(new ImageView(icons.get(Integer.toString(gra.returnValue(p.x, p.y)))));
this.buttonBoard[p.x][p.y].setDisable(true);
}
} else {
this.buttonBoard[r][c].setGraphic(new ImageView(icons.get("bomb")));
this.buttonBoard[r][c].setDisable(true);
show();

}
}
}

private void show() { // funkcja do wyświetlenia wszystkich bomb po wciśnięciu bomby
ArrayList<Point> temp = this.gra.allBombs();
for (Point p : temp) {
this.buttonBoard[p.x][p.y].setGraphic(new ImageView(icons.get("bomb")));
}

}


@Override
public void start(Stage primaryStage) throws Exception {

icons.put("0", new Image(getClass().getResourceAsStream("icons/zero.png")));
icons.put("1", new Image(getClass().getResourceAsStream("icons/one.png")));
icons.put("2", new Image(getClass().getResourceAsStream("icons/two.png")));
icons.put("3", new Image(getClass().getResourceAsStream("icons/three.png")));
icons.put("4", new Image(getClass().getResourceAsStream("icons/four.png")));
icons.put("5", new Image(getClass().getResourceAsStream("icons/five.png")));
icons.put("6", new Image(getClass().getResourceAsStream("icons/six.png")));
icons.put("7", new Image(getClass().getResourceAsStream("icons/seven.png")));
icons.put("8", new Image(getClass().getResourceAsStream("icons/eight.png")));
icons.put("bomb", new Image(getClass().getResourceAsStream("icons/bomb.png")));
icons.put("q", new Image(getClass().getResourceAsStream("icons/question.png")));
icons.put("d", new Image(getClass().getResourceAsStream("icons/triangle.png")));
icons.put("button", new Image(getClass().getResourceAsStream("icons/button.png")));
icons.put("buttonx", new Image(getClass().getResourceAsStream("icons/buttonx.png")));
icons.put("qx", new Image(getClass().getResourceAsStream("icons/questionx.png")));
icons.put("dx", new Image(getClass().getResourceAsStream("icons/trianglex.png")));

public static void main(String[] args) {

launch(args);
}
Exception in Application start method
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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
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.Main.start(Main.java:163)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
... 1 more
Exception running application sample.Main

enter image description here

如果你想知道的话,这就是我的 pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.saper</groupId>
<artifactId>Saper</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>

<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>packagee.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>

</dependencies>


</project>

最佳答案

getClass().getResourceAsStream("/icons/zero.png") 以起始斜杠表示绝对路径,而不是相对于类包的路径 (/sample)

关于java - 使用maven将照片添加到javafx中的按钮时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56539442/

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