簡體   English   中英

Javafx css按鈕圖形調整大小

[英]Javafx css button graphic resize

我想改變按鈕內圖標的大小。 圖像大小為512 x 512,我想調整大小為16x16。 那么,使用javaFX CSS實現這一目標的最佳方法是什么。

以下是我到目前為止所做的事情:

#btnCancel.button {
    -fx-graphic: url("/img/close.png") ;
    -fx-graphic-size:16px 16px ; ( I think this property not exist in javafx css)

}

但這不適合我。

我不知道任何可以直接用於設置-fx-graphic大小的css屬性 ,但是你可以使用-fx-background-image屬性獲得類似的結果。 這將允許您使用-fx-background-size設置寬度和高度值。

您可以從此鏈接訪問BackgroundSize的javadoc

.logButton {
    /*
    -fx-graphic: url(images/log2.png);
    -fx-graphic-size:16px 16px;
    */
    -fx-background-image: url(images/log2.png);
    -fx-background-size: 64px 64px;
    -fx-background-repeat: no-repeat;
    -fx-background-position: center;
}

看到實際行動

使用-fx-graphic

帶-fx-graphic的按鈕

使用-fx-background-image-fx-background-size

按鈕使用-fx-background-size調整大小

我知道這個問題是關於css的,但對於那些希望用java代碼執行此操作的其他程序員並找到這個線程,這是我用java代碼做的解決方案。 (而不是問一個新問題)

Platform.runLater(() -> {
    InputStream input = getClass().getResourceAsStream("/resources/images/close-512x512.png");
    //set the size for image
    Image image = new Image(input, 16, 16, true, true);
    ImageView imageView = new ImageView(image);            
    myButton.setGraphic(imageView);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM