如何用图像替换按钮的默认外观?[使用场景生成器的JavaFx]

如何用图像替换按钮的默认外观?[使用场景生成器的JavaFx],javafx,scenebuilder,Javafx,Scenebuilder,我希望我的按钮看起来像一个图像,你可以点击它。但是当我尝试在上面放一个图像(有透明背景)时,按钮的外观仍然存在 以下是我迄今为止所尝试的: <fx:define> <Image fx:id="btnImage" url="images/image.png" /> </fx:define> 及 在.fxml文件中 还可以使用场景生成器的ImageView 但正如我前面所说的,按钮是这样的: 您可以看到按钮的灰色背景仍然存在,我希望它看起来像这

我希望我的按钮看起来像一个图像,你可以点击它。但是当我尝试在上面放一个图像(有透明背景)时,按钮的外观仍然存在

以下是我迄今为止所尝试的:

<fx:define>
   <Image fx:id="btnImage" url="images/image.png" />
</fx:define>


在.fxml文件中

还可以使用场景生成器的ImageView

但正如我前面所说的,按钮是这样的:

您可以看到按钮的灰色背景仍然存在,我希望它看起来像这样:


基本上是一个你可以点击的图像。如何使其工作?

为什么不使用代码而不是使用场景生成器轻松完成此操作

package testing;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class Controller implements Initializable {

    @FXML public Button myButton;

    @Override
    public void initialize(URL location, ResourceBundle resources)  {

        File imageFile = new File("images/image.png");
        Image image = new Image(imageFile.toURI().toString());
        ImageView imageView = new ImageView(image);

        myButton.setText("");
        myButton.setStyle("-fx-background-color: transparent;");
        myButton.setGraphic(imageView);


        // Add an event click to the button
        myButton.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                System.out.println("You clicked: " + ((Button)event.getSource()).getId());
            }                
        });

    }
}
封装测试;
导入java.io.File;
导入java.net.URL;
导入java.util.ResourceBundle;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.Button;
导入javafx.scene.image.image;
导入javafx.scene.image.ImageView;
公共类控制器实现可初始化{
@FXML公共按钮myButton;
@凌驾
公共void初始化(URL位置、ResourceBundle资源){
File imageFile=新文件(“images/image.png”);
Image Image=新图像(imageFile.toURI().toString());
ImageView ImageView=新的ImageView(图像);
myButton.setText(“”);
myButton.setStyle(“-fx背景色:透明;”);
myButton.setGraphic(图像视图);
//单击按钮添加事件
myButton.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
System.out.println(“您单击:”+((按钮)event.getSource()).getId());
}                
});
}
}

您只需将按钮的背景色设置为透明即可。您可以这样做:
button.setStyle(“-fx背景色:透明”)

我完全忘记了设置背景色时“透明”是有效的。为了正确回答我的问题,可以在Properties>Style(JavaFXCSS部分)下找到。我想我太依赖场景生成器了。@Itsknelton我过去也是。但是现在,我使用了比场景生成器更多的代码,因为它更灵活。
package testing;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class Controller implements Initializable {

    @FXML public Button myButton;

    @Override
    public void initialize(URL location, ResourceBundle resources)  {

        File imageFile = new File("images/image.png");
        Image image = new Image(imageFile.toURI().toString());
        ImageView imageView = new ImageView(image);

        myButton.setText("");
        myButton.setStyle("-fx-background-color: transparent;");
        myButton.setGraphic(imageView);


        // Add an event click to the button
        myButton.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                System.out.println("You clicked: " + ((Button)event.getSource()).getId());
            }                
        });

    }
}