简体   繁体   English

如何根据 slider 上拇指的当前 position 的 slider 的颜色更改 JFXSlider 拇指的颜色?

[英]How can I change the color of a JFXSlider's thumb according to the color of the slider at the current position of the thumb on the slider?

I'm using a JFXSlider in JavaFX and I've used a linear gradient for the color of the JFXSlider 's track (with CSS).我在 JavaFX 中使用了JFXSlider ,并且我对JFXSlider的轨道颜色使用了线性渐变(使用 CSS)。 However, I'd also like to change the color of the thumb to that of the slider for that position.但是,我还想将拇指的颜色更改为 position 的 slider 的颜色。 I've used the following CSS for the slider's linear gradient and for getting rid of the default green color of the JFXSlider :我已使用以下 CSS 来实现滑块的线性渐变并摆脱JFXSlider的默认绿色:

.jfx-slider .track {
    -fx-pref-height: 10;
    -fx-background-color: linear-gradient(to right,red,orange);
}
.jfx-slider .colored-track {
    -fx-background-color: transparent;
}

I tried the following CSS code to get the thumb color to be the same as that of the slider at the current position, but it didn't work.我尝试了以下 CSS 代码以使拇指颜色与当前 position 的 slider 的颜色相同,但它不起作用。

.jfx-slider .thumb {
    -fx-background-color: linear-gradient(to right,red,orange);
}

I guess it's probably that the code I tried only provides an internal linear gradient for the thumb's background color.我想这可能是我尝试的代码只为拇指的背景颜色提供了一个内部线性渐变。 Does anyone know how to solve this problem?有谁知道如何解决这个问题? PS I'm using JFoenix 9.0.10, JavaFX 15, and JDK 15. PS 我正在使用 JFoenix 9.0.10、JavaFX 15 和 JDK 15。

One possible solution would be to add a global CSS variable and change it depending on the JFXSlider current value.一种可能的解决方案是添加一个全局 CSS 变量并根据 JFXSlider 当前值更改它。 For example:例如:

.root {
    -fx-custom-color : red;
}

And then use this variable on your jfx-slider css rules like:然后在您的 jfx-slider css 规则上使用此变量,例如:

/* Styling the slider thumb */
.jfx-slider>.thumb {
    -fx-background-color: -fx-custom-color;
}

/* Styling the animated thumb */
.jfx-slider>.animated-thumb {
    -fx-background-color: -fx-custom-color;
}

After that, you need to figure out how to update the "-fx-custom-color" variable and how to determine which color you need to set for the specific value of the Slider (or rather location).之后,您需要弄清楚如何更新“-fx-custom-color”变量以及如何确定需要为 Slider 的特定值(或者更确切地说是位置)设置哪种颜色。

First, you should add a listener to the value property to listen for value changes.首先,您应该向 value 属性添加一个侦听器以侦听值的变化。 Second, use the interpolate method of the Color class to determine the color, and finally, update the new value for the -fx-custom-color using inline CSS style to the JFXSlider.其次,使用颜色 class 的插值方法来确定颜色,最后使用内联 CSS 样式将-fx-custom-color的新值更新到 JFXSlider。

Here is a complete example:这是一个完整的例子:

import com.jfoenix.controls.JFXSlider;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class SliderTesting extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        JFXSlider slider = new JFXSlider(0, 100, 0);

        slider.valueProperty().addListener(e -> {
            Color imageColor = Color.RED.interpolate(Color.ORANGE,
                    slider.getValue() / 100);
            slider.setStyle("-fx-custom-color : " + colorToHex(imageColor) + ";");
        });

        VBox box = new VBox(slider);
        box.setPadding(new Insets(20));
        box.setPrefSize(400, 400);
        box.setAlignment(Pos.CENTER);

        Scene scene = new Scene(box);
        scene.getStylesheets()
                .add(this.getClass().getResource("custom-jfoenix.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static String colorToHex(Color color) {
        return String.format("#%02X%02X%02X", (int) (color.getRed() * 255),
                (int) (color.getGreen() * 255), (int) (color.getBlue() * 255));
    }
}

And the "custom-jfoenix.css" file还有“custom-jfoenix.css”文件

.root {
    -fx-custom-color : red;
}

/* Styling the slider track */
.jfx-slider>.track {
    -fx-pref-height: 10;
}

/* Styling the slider thumb */
.jfx-slider>.thumb {
    -fx-background-color: -fx-custom-color;
}

/* Styling the filled track */
.jfx-slider>.colored-track {
    -fx-background-color: linear-gradient(to right, red, orange);
}

/* Styling the animated thumb */
.jfx-slider>.animated-thumb {
    -fx-background-color: -fx-custom-color;
}

And the result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM