如何在 JavaFX 中创建 ProgressIndicator?
进度指示器是一种圆形 UI 组件,用于指示某些操作的进度。可以通过实例化javafx.scene.control.ProgressIndicator 类来创建进度指示器。
示例
以下示例演示了如何创建ProgressIndicator。
import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.ProgressIndicator; import javafx.scene.control.Slider; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; public class ProgressIndicatorExample extends Application { public void start(Stage stage) { //Creating a progress indicator ProgressIndicator indicator = new ProgressIndicator(0.6); //Setting the size of the progress bar indicator.setPrefSize(300, 120); //Creating a slider Slider slider= new Slider(0, 1, 0.2); slider.setShowTickLabels(true); slider.setShowTickMarks(true); slider.setMajorTickUnit(0.25); slider.setBlockIncrement(0.1); slider.valueProperty().addListener(new ChangeListener<Number>() { public void changed(ObservableValue <?extends Number>observable, Number oldValue, Number newValue){ //Setting the angle for the rotation indicator.setProgress((double) newValue); //Setting value to the indicator indicator.setProgress((double) newValue); } }); //Creating a vbox to hold the progress bar and progress indicator VBox vbox = new VBox(20); vbox.setSpacing(5); vbox.setPadding(new Insets(15, 150, 10, 60)); vbox.getChildren().addAll(slider, indicator); vbox.setStyle("-fx-background-color: BEIGE"); //Setting the stage Scene scene = new Scene(vbox, 595, 200, Color.BEIGE); stage.setTitle("Progress Indicator"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
输出
广告