JavaFX如何"裁剪"图形到按钮

Vee*_*nIn 0 java javafx button

基本上,我想要的是创建Button一个裁剪图形.在意义上裁剪,Image在后面,并且Button是一个洞,显示出来Graphic.截至目前,它看起来像尝试失败

但是我希望图形适合按钮,即使它更大,也只是被切断了.我目前的代码是沿着的

Image image = new Image(Main.class.getResource("/texture.png").toExternalForm());
yesButton.setGraphic(new ImageView(image));
Run Code Online (Sandbox Code Playgroud)

dzi*_*ysk 5

您可以使用javafx css设置背景:

Button button = new Button("Button");
button.setStyle("-fx-background-image: url('/texture.png')");
Run Code Online (Sandbox Code Playgroud)

或者以编程方式使用Background by setBackground:

Image image = new Image(Main.class.getResource("/texture.png").toExternalForm());
BackgroundImage backgroundImage = new BackgroundImage(image, BackgroundRepeat.NO_REPEAT,
        BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.AUTO);
Background background = new Background(backgroundImage);

Button button = new Button("Button");
button.setBackground(background);
Run Code Online (Sandbox Code Playgroud)