【问题标题】:JavaFX how to "crop" graphics to buttonsJavaFX如何将图形“裁剪”到按钮
【发布时间】:2018-09-03 07:39:02
【问题描述】:

基本上,我想要的是创建一个带有裁剪图形的Button。从某种意义上说,Image 在后面,Button 是一个孔,显示Graphic。截至目前,它看起来像

但是,我希望图形适合按钮,即使它更大,也可以被切断。我当前的代码类似于

Image image = new Image(Main.class.getResource("/texture.png").toExternalForm());
yesButton.setGraphic(new ImageView(image));

【问题讨论】:

    标签: java button javafx


    【解决方案1】:

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

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

    或通过setBackground 以编程方式使用Background

    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);
    

    【讨论】:

    猜你喜欢
    • 2021-10-08
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多