JavaFX CSS按钮与图像 - 如何定义图像的大小? [英] JavaFX CSS Button with Image - How to define the size of the image?

查看:174
本文介绍了JavaFX CSS按钮与图像 - 如何定义图像的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用JavaFX CSS在按钮中插入图像。虽然,我可以很容易地使用 - fx-graphic标签,我找不到一个方法来调整图像大小我想要的任何大小。


我可以做到通过以下FXML代码,其中我给30我的首选宽度的图像,但我想用纯CSS做到这一点。有什么办法吗?

I am trying to insert an image in a button using JavaFX CSS. Although, I can do it easily using the "-fx-graphic" tag, I cannot find a way to resize the image in whatever size I want.

I can do this through the following FXML code, where I give 30 to my preferred width of image, but I would like to do this with pure CSS. Is there any way to do that?

FXML

<Button text="Press Me">
   <graphic>
      <ImageView fitWidth="30">
        <image>
          <Image url="myImage.png"/>
        </image>
      </ImageView>
   </graphic>
</Button>

CSS

#buttonWithImage {          
    -fx-graphic: url("myImage.png");
}


推荐答案

发现一个解决方法:而不是使用-fx-image,我使用-fx-background-image。

I had the same problem and found a workaround: instead of using -fx-image, I use -fx-background-image.

注意,我使用额外的lib使用svg文件示例。

Note that i use an additional lib to use svg file in the following example.

CSS

#buttonWithImage { 
    -fx-background-image: url('myimage.svg');
    -fx-background-size: 30px;
    -fx-background-repeat: no-repeat;
    -fx-background-position: 90%;
}

这篇关于JavaFX CSS按钮与图像 - 如何定义图像的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆