滑块与进度条JavaFX的混合

滑块与进度条JavaFX的混合

本文介绍了滑块与进度条JavaFX的混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在滑块上点击ProgressBar。并学习一定比例的赛道。
我会使用滑块而不是进度条,但是在拇指之前它没有突出显示的轨道。

I want to handle click on ProgressBar like on slider. and learn a percent of track.I would use slider instead progressbar but it doesn't have a highlighted track until thumb.

我需要在播放歌曲的音乐播放器中创建类似进度的东西,并且可以通过点击进度来寻找。

I need create something like a progress in a music player of playing song, and possibility to seek with a click on progress.

有人有提示我该怎么办?

Do anybody have a tips how can i do it?

推荐答案

这是另一种方法。滑块和进度条真正的混合:)。遇见SlidoProgressBar!

Here is another approach. Real hybrid of slider and progress bar :). Meet SlidoProgressBar!

public class SlidoProgressBarDemo extends Application {

    @Override
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root);
        scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm());
        stage.setScene(scene);
        stage.setTitle("Progress Controls");

        double sliderWidth = 200;

        final Slider slider = new Slider();
        slider.setMin(0);
        slider.setMax(50);
        slider.setMinWidth(sliderWidth);
        slider.setMaxWidth(sliderWidth);

        final ProgressBar pb = new ProgressBar(0);
        pb.setMinWidth(sliderWidth);
        pb.setMaxWidth(sliderWidth);

        final ProgressIndicator pi = new ProgressIndicator(0);

        slider.valueProperty().addListener(new ChangeListener<Number>() {
            public void changed(ObservableValue<? extends Number> ov,
                    Number old_val, Number new_val) {
                pb.setProgress(new_val.doubleValue() / 50);
                pi.setProgress(new_val.doubleValue() / 50);
            }
        });

        StackPane pane = new StackPane();

        pane.getChildren().addAll(pb, slider);

        final HBox hb = new HBox();
        hb.setSpacing(5);
        hb.setAlignment(Pos.CENTER);
        hb.getChildren().addAll(pane, pi);

        scene.setRoot(hb);
        stage.show();
    }

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

with style.css:

with style.css:

.slider .track {
    -fx-background-color:null;  /* Hide the track */
    -fx-background-insets: 1 0 -1 0, 0, 1;
    -fx-background-radius: 2.5, 2.5, 1.5;
    -fx-padding: 0.208333em; /* 2.5 */
}

基本逻辑是将滑块和进度放入堆栈窗格。给它们相同的宽度。绑定它们的进度值。隐藏滑块的轨道。

输出:

The basic logic is to put slider and progress into stackpane. Give them the same width. Bind the progress values of them. Hide the track of the slider.
Output:

这篇关于滑块与进度条JavaFX的混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:47