6

我想像在滑块上一样处理对 ProgressBar 的单击。并学习百分比的轨道。我会使用滑块而不是进度条,但在拇指之前它没有突出显示的轨道。

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

有没有人有提示我该怎么做?

4

3 回答 3

18

这是另一种方法。滑块和进度条的真正混合体:)。认识 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);
    }
}

使用 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 */
}

基本逻辑是将滑块和进度放入堆栈窗格。给它们相同的宽度。绑定它们的进度值。隐藏滑块的轨迹。
输出:
在此处输入图像描述

于 2013-01-04T14:44:11.477 回答
1

我用代码解决了这个问题:

progress.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            if (event.getButton() == MouseButton.PRIMARY){
                Bounds b1 = progress.getLayoutBounds();
                double mouseX = event.getSceneX();
                double percent = (((b1.getMinX() + mouseX ) * 100) / b1.getMaxX());
                //correcting a percent, i don't know when it need
                percent -= 2;
                progress.setProgress((percent) / 100);
                //do something with progress in percent
            }
        }
    });
于 2013-01-04T14:12:23.050 回答
0

只是一个 tornadoFX 示例:

package bj

import javafx.application.Application
import javafx.geometry.Insets
import javafx.scene.control.ProgressBar
import javafx.scene.control.Slider
import javafx.scene.paint.Color
import tornadofx.*

/**
* Created by BaiJiFeiLong@gmail.com at 18-12-13 下午9:28
*/

class MainView : View() {

    private lateinit var progressBar: ProgressBar
    private lateinit var slider: Slider

    override val root = vbox {
        stackpane {
            padding = Insets(100.0)
            progressbar(initialValue = .0) {
                progressBar = this
                maxWidth = Double.MAX_VALUE
            }
            slider(max = 1, value = .0) {
                slider = this
            }
        }
    }

    init {
        progressBar.progressProperty().bind(slider.valueProperty())
        progressBar.paddingLeftProperty.bind(progressBar.heightProperty().divide(2))
        progressBar.paddingRightProperty.bind(progressBar.heightProperty().divide(2))
    }
}

class MainStylesheet : Stylesheet() {
    init {
        slider {
            track {
                backgroundColor = MultiValue(arrayOf(Color.TRANSPARENT))
            }
        }
    }

}

class App : tornadofx.App(MainView::class, MainStylesheet::class)

fun main(args: Array<String>) {
    Application.launch(App::class.java, *args)
}
于 2018-12-13T14:56:06.237 回答