使用Maven将照片添加到Javafx中的按钮的问题

时间:2019-06-11 08:25:29

标签: java maven intellij-idea javafx

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

我尝试将图标放在资源文件夹中,甚至使用Main.java放在文件夹中 多数民众赞成在问题所在:<?php // do something parsed by php ?> do something without php <?php // do something parsed by php ?> do something without php

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);
}

enter image description here

如果您想知道的话,那就是我的pom

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

2 个答案:

答案 0 :(得分:1)

getClass().getResourceAsStream("/icons/zero.png")带有一个绝对路径的起始斜线,而不是相对于类包(/sample)的相对路径

答案 1 :(得分:0)

您的pom.xml文件是什么?

此外,如果您的pom.xml文件中没有标签资源,请尝试添加

<resources>
   <resource>
      <directory>src/main/resources</directory>
   </resource>
</resources>

这对我有用