我正在尝试将图像设置为按钮,但无法获得工作结果。 我的按钮位于网格 Pane 中。
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);
}
按下按钮时调用此方法。在此之前,我使用了另一种方法,其中使用了标签,并且此方法有效。
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("")
。
关于JavaFX 按钮设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59018697/