【问题标题】:Javafx button with circular background image [duplicate]带有圆形背景图像的Javafx按钮[重复]
【发布时间】:2018-04-16 09:09:32
【问题描述】:

我必须从某个 url 创建一个带有背景图像的 javafx 按钮。图像显示完美。但问题是图像不圆。图像显示为其原始形状。但我需要将按钮创建为带有图像的圆形视图。我怎样才能做到这一点?

这是我尝试过的代码

Image cameraIcon = new Image("//url");//image will be from url
ImageView cameraIconView = new ImageView(cameraIcon);
cameraIconView.setFitHeight(120);
cameraIconView.setFitWidth(120);
cameraIconView.setStyle(""
            + "-fx-border-color: black;"
            + "-fx-border-style: solid;"
            + "-fx-border-width: 10;"
            + "-fx-border-radius: 10 10 10 10;"
    );      

Button button;
button.setGraphic(cameraIconView);
button.setStyle("-fx-base: coral;");
button.setStyle(
            "-fx-background-radius: 50em; "
            + "-fx-min-width: 60px; "
            + "-fx-min-height: 60px; "
            + "-fx-max-width: 130px; "
            + "-fx-max-height: 60px;"
    );

【问题讨论】:

  • 我看过你的链接,但是有关于如何设置背景的。我的问题不在于设置背景图像。我已经设置了背景图像,但图像没有像按钮一样四舍五入。 @Elarbi Mohamed Aymen
  • 你是不是这个jewelsea的answer
  • @Pagbo .....非常感谢

标签: java javafx imageview


【解决方案1】:

用 css 样式试试

.btnHome {
    -fx-background-image: url("/images/btnHomeHover.png");
    -fx-background-size: 50px, 50px;
    -fx-background-repeat: no-repeat;
    -fx-background-position: center;
    -fx-border-radius: 30;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2013-10-07
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多