如何使用 JavaFX 创建进度条?
javafxobject oriented programmingprogramming更新于 2026/1/7 12:28:17
进度条是事件(一系列步骤)进展的指示器。您可以通过实例化 javafx.scene.control.ProgressBar 类来创建进度条。
示例
以下示例演示了如何创建 ProgressBar。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ProgressBarExample extends Application {
public void start(Stage stage) {
//创建进度条
ProgressBar progress = new ProgressBar();
//设置进度条的值
progress.setProgress(0.5);
//创建进度指示器
ProgressIndicator indicator = new ProgressIndicator(0.6);
//设置进度条的大小
progress.setPrefSize(300, 30);
//创建一个 hbox 来容纳进度条和进度指示器
HBox hbox = new HBox(20);
hbox.setSpacing(5);
hbox.setPadding(new Insets(75, 150, 50, 60));
hbox.getChildren().addAll(progress, indicator);
//设置舞台
Group root = new Group(hbox);
Scene scene = new Scene(root, 595, 200, Color.BEIGE);
stage.setTitle("Progress Bar Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
输出


