为什么按钮上的 javafx 图像视图未按预期设置

Why javafx Image View on Button is not setting as expected

我使用按钮在 javafx 中制作随机图片库。我将 imageView 设置为按钮,因为我需要点击图像。问题是 imageView 没有按预期设置在按钮上。按钮的首选项高度和宽度为 110。

下面是我用来为按钮设置 imageView 的代码。

for(int i=0; i<35; i++){
    Button button = list.get(i);
    ImageView imageview =new ImageView(imageList.get(i));
    imageview.setPreserveRatio(true);
    imageview.setFitHeight(110);
    imageview.setFitWidth(110);
    button.setGraphic(imageview);
}

您需要将 Buttonpadding 更改为空:

Button button = new Button(null, imageView);

// quadratic grey background
button.setBackground(new Background(new BackgroundFill(Color.GREY, CornerRadii.EMPTY, Insets.EMPTY)));

// no padding
button.setPadding(Insets.EMPTY);