2016-03-08 345 views
3

我想更改按钮内图标的大小。 图像的大小为512 x 512,我想调整为16x16。 那么,使用javaFX CSS实现这一点的最佳方式是什么?Javafx css按钮图形调整大小

这里是我做直到如今

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

} 

但这不是我的作品。

+0

此[问题](http://stackoverflow.com/questions/30098116/javafx-css-button-with-image-how-to-define-the-size图像)没有被接受的答案,但它可能对你有帮助。 – Peter

+0

处理此问题的最佳方法是提供16x16图标而不是512x512图标。 – jewelsea

回答

2

我不知道,你可以直接使用设置-fx图形大小CSS属性的,但你可以使用-fx背景图像特性实现类似。您可以使用-fx-background-size来设置宽度和高度。

你可以从这个link

.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; 
 
}

访问的javadoc见行动

使用-fx图形

Button with -fx-graphic

使用-fx背景图像-fx背景大小

Button resized with -fx-background-size

0

我知道这个问题是关于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); 
}); 
+0

@kleopatra这个代码可以被用作答案是多种问题。如果有人想用java代码做这件事,这是一个可能的解决方案。 – Darrelk

+0

是的,但不是这里的要求:_什么是实现这个使用javaFX的最佳方式CSS_ – kleopatra

+0

我明白你的观点并非常感谢你帮助我。我一直在寻找一种解决方案,就像我在提出这个问题时所提供的解决方案一样,所以我教了一些其他人可能最终会出现在这里,他们希望显示所有可能的解决方案并选择适合他们的解决方案。而不是问一个新的问题。 – Darrelk