JAVAFX 2.0如何将滑块中的滑块图标更改为图像? [英] JAVAFX 2.0 How can i change the slider icon in a slider to an image?
问题描述
我想将图标更改为我的图像,我查看了CSS参考指南,但我似乎无法找到任何相关内容。它甚至可能吗?无论是使用CSS还是声明性地使用主JavaFX脚本都无关紧要。
I want to change the icon to my image, I've looked through the CSS reference guide but I can't seem to find anything relevant. Is it even possible? Doesn't matter if it is using CSS or declaratively from main JavaFX script.
推荐答案
查看示例代码和图像如何在此 AudioPlayer 。
Take a look at the sample code and images of how a custom slider is rendered in this AudioPlayer.
JFXtras图书馆也有很多指标你只想要反馈而不是交互式控件。
Also the JFXtras library has numerous gauges if you just want feedback rather than an interactive control.
下面是一些使用由invariant的答案指出的选择器的示例css。请注意,我需要在图像尺寸的一半处添加-fx-padding规范,以便显示整个图像。
Here is some sample css using the selector pointed out by invariant's answer. Note that I needed to add an -fx-padding specification at half the images dimensions in order for the whole image to display.
/** slider.css
place in same directory as SliderCss.java
ensure build system copies the css file to the build output path */
.slider .thumb {
-fx-background-image :url("http://icons.iconarchive.com/icons/double-j-design/diagram-free/128/piggy-bank-icon.png");
-fx-padding: 64;
}
/* Icon license: creative commons with attribution: http://www.doublejdesign.co.uk/products-page/icons/diagram */
示例应用程序:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class SliderCss extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) throws Exception {
VBox layout = new VBox();
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10px;");
layout.getChildren().setAll(new Slider());
layout.getStylesheets().add(getClass().getResource("slider.css").toExternalForm());
stage.setScene(new Scene(layout));
stage.show();
}
}
示例程序输出:
这篇关于JAVAFX 2.0如何将滑块中的滑块图标更改为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!