在JavaFX中创建自定义ProgressIndicator可以通过以下步骤实现:
以下是一个示例的自定义进度指示器类的代码:
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
public class CustomProgressIndicator extends Region {
private Circle progressCircle;
public CustomProgressIndicator() {
progressCircle = new Circle();
progressCircle.setStroke(Color.BLUE);
progressCircle.setFill(Color.TRANSPARENT);
progressCircle.setStrokeWidth(5);
getChildren().add(progressCircle);
}
@Override
protected void layoutChildren() {
super.layoutChildren();
double centerX = getWidth() / 2;
double centerY = getHeight() / 2;
double radius = Math.min(getWidth(), getHeight()) / 2 - progressCircle.getStrokeWidth() / 2;
progressCircle.setCenterX(centerX);
progressCircle.setCenterY(centerY);
progressCircle.setRadius(radius);
}
public void setProgress(double progress) {
progressCircle.setLength(progress * 360);
}
public double getProgress() {
return progressCircle.getLength() / 360;
}
public void setColor(Color color) {
progressCircle.setStroke(color);
}
public void setSize(double size) {
progressCircle.setRadius(size / 2 - progressCircle.getStrokeWidth() / 2);
}
public void setAnimation(boolean animate) {
if (animate) {
progressCircle.getStyleClass().add("animated");
} else {
progressCircle.getStyleClass().remove("animated");
}
}
public void setOtherProperty(Object value) {
// 设置其他属性
}
}
在主程序中使用自定义进度指示器:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
CustomProgressIndicator progressIndicator = new CustomProgressIndicator();
progressIndicator.setProgress(0.5);
progressIndicator.setColor(Color.RED);
progressIndicator.setSize(100);
progressIndicator.setAnimation(true);
StackPane stackPane = (StackPane) root;
stackPane.getChildren().add(progressIndicator);
}
public static void main(String[] args) {
launch(args);
}
}
这样就可以在JavaFX中创建一个自定义的进度指示器了。你可以根据需要调整进度指示器的样式、颜色、大小、动画效果等属性。
领取专属 10元无门槛券
手把手带您无忧上云