JavaFX按钮集图像

JavaFX按钮集图像,java,button,javafx,interface,gridpane,Java,Button,Javafx,Interface,Gridpane,我试图设置一个按钮的图像,但我不能得到一个工作结果。 我的按钮位于gridpane中 Image mine = new Image(new File("..some path").toURI().toString()); ImageView im = new ImageView(mine); im.setFitHeight(btn.getHeight()); im.setFitWidth(btn.getWidth()); btn.setGraphic(im); 我该怎么做 我试图添加到面板,

我试图设置一个按钮的图像,但我不能得到一个工作结果。 我的按钮位于gridpane中

Image mine = new Image(new File("..some path").toURI().toString());
ImageView im = new ImageView(mine);

im.setFitHeight(btn.getHeight());
im.setFitWidth(btn.getWidth());
btn.setGraphic(im);
我该怎么做

我试图添加到面板,但有时按钮被删除,我并没有得到带有图像的按钮

private static void setFlag(Button btn,int x,int y,GridPane gridPane){
        Image mine = new Image(new File("some path..").toURI().toString());
        ImageView im = new ImageView(mine);

        im.setFitHeight(btn.getHeight());
        im.setFitWidth(btn.getWidth());
        btn.setGraphic(im);

        gridPane.add(btn, y, x);
    }
按下按钮时调用此方法。在此之前,我使用了另一种方法,在这里我使用了label,这个方法很有效

private static void setBombCell(Button btn, int x,int y,GridPane gridPane){
        Image mine = new Image(new File("..").toURI().toString());
        ImageView im = new ImageView(mine);

        Label label = new Label();


        label.setMaxWidth(Double.MAX_VALUE);
        label.setMaxHeight(Double.MAX_VALUE);
        GridPane.setHgrow(label, Priority.ALWAYS);
        GridPane.setVgrow(label, Priority.ALWAYS);

        im.setFitHeight(btn.getHeight());
        im.setFitWidth(btn.getWidth());
        label.setGraphic(im);
        gridPane.add(label, y, x);
    }

我解决了这个问题。为此,我使用了方法
button.setStyle(“”)

我解决了这个问题。我使用了方法
button.setStyle(“”

@Slaw默认情况下我重写了
按钮
为图形添加了一些插图。即使你能做到这一点,按钮,因此行+列将增长。。。此外,我建议重用
图像
数据。您可以在任意数量的
ImageView
s中显示相同的
Image
数据。我还建议将图像数据存储为资源。顺便说一句:
文件
对象不区分现有文件和不存在的文件。你确定文件路径是正确的吗?@fabian path是正确的,当我使用gridpane方法add时,我用图像获取按钮,但有时按钮被取下,我就空了cells@fabian我正在尝试编写“扫雷舰”,此方法应在按钮上添加图像标志。请。@Slaw我已在默认情况下重写
按钮
在图形中添加一些插图。即使你能做到这一点,按钮,因此行+列将增长。。。此外,我建议重用
图像
数据。您可以在任意数量的
ImageView
s中显示相同的
Image
数据。我还建议将图像数据存储为资源。顺便说一句:
文件
对象不区分现有文件和不存在的文件。你确定文件路径是正确的吗?@fabian path是正确的,当我使用gridpane方法add时,我得到带有图像的按钮,但有时按钮被删除,我得到的是空的cells@fabian我正在尝试写扫雷舰,这个方法应该添加图像标志到按钮,请。。