Javafx将Image对象设置为Button的背景

时间:2018-06-13 15:40:54

标签: javafx

我有一个Image对象,并希望将其设置为 Javafx Button的背景

Image image_here = //Image from API call
Button btn = new Button();
btn.setShape(new Circle(25));
btn.setMinSize(50, 50);
btn.setMaxSize(50, 50);

我试过了

btn.setGraphic(image_here); //this causes image to go out of button boundary. Not Ideal for my usecase.

及以下方法

BackgroundImage icon_background = new BackgroundImage(image_here, 
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, 
BackgroundPosition.CENTER, BackgroundSize.DEFAULT);
Background background = new Background(icon_background);
btn.setBackground(background);

可以使用

设置背景
setStyle  btn.setStyle("-fx-background-image: url('" + image_here+ "');");

但在尝试通过fx-background-image添加图片之前,我不确定是否需要将图片转换为其他形式

0 个答案:

没有答案
相关问题