如何在 JavaFX 中获得狭窄的进度条?

sze*_*jic 6 css java javafx javafx-2

正如标题所说,我需要制作一个细长的进度条。我用过这个:

progressBar.setMaxHeight(0.1);
progressBar.setPrefHeight(0.1);
Run Code Online (Sandbox Code Playgroud)

但这不起作用。有没有人有想法?

Nic*_*ppe 6

你必须弄乱样式才能让它更小。我真的建议看一看caspian.cssJavafx 中包含的 - 这是默认的样式表。当尝试覆盖默认皮肤的外观和感觉时,它有很大帮助。这是我放在一起的一个例子,展示了它是如何完成的:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class ProgressBarTest extends Application {

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

    @Override
    public void start(final Stage stage) throws Exception {
        //All the controls are added here
        VBox box = new VBox();
        box.getStylesheets().add("test.css");
        ProgressBar pb = new ProgressBar(50);

        box.getChildren().add(pb);

        //Setting up your scene
        Scene scene = new Scene(box);
        stage.setScene(scene);

        stage.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我加载的 test.css:

.progress-bar .bar {-fx-padding:1px; -fx-background-insets:0;}
Run Code Online (Sandbox Code Playgroud)

这是测试应用程序的输出:

小进步

  • 我将这个问题的答案添加到 [StackOverflow JavaFX Progress Bar Styling Community Wiki Answer](http://stackoverflow.com/questions/19417246/how-can-i-style-the-progressbar-component-in-javafx) . (4认同)