【发布时间】:2020-03-19 23:16:03
【问题描述】:
我正在尝试将图像设置为按钮,但无法获得有效结果。 我的按钮位于网格窗格中。
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);
}
【问题讨论】:
-
@Slaw 我重写了
-
默认情况下
Button会在图形中添加一些插图。即使你让它工作,按钮和行+列也会增长......此外,我建议重用Image数据。您可以在任意数量的ImageViews 中显示相同的Image数据。我还建议将图像数据存储为资源。顺便说一句:File对象不区分现有文件和不存在文件。你确定文件路径正确吗? -
@fabian 路径正确,当我使用 gridpane 方法添加时,我得到带有图像的按钮,但有时按钮被删除并且我得到空单元格
-
@fabian 我正在尝试编写扫雷器,此方法应将图像标志添加到按钮
标签: java button javafx interface gridpane