gpt4 book ai didi

java - ProgressIndicator(节点)作为鼠标光标?

转载 作者:行者123 更新时间:2023-11-30 08:10:26 24 4
gpt4 key购买 nike

是否可以将节点用作鼠标光标?我在 ProgressIndicator 中思考。例如确定性的,让用户知道当前任务完成了多少百分比。

最佳答案

可能最可靠的方法是将光标设置为 Cursor.NONE,并使用带有进度指示器的标签作为其图形,跟踪鼠标坐标。

我尝试使用更新后的 ImageCursor,但什么也没有出现:我猜测图像计算速度不够快。

这是第一种技术的 SSCCE:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.ImageCursor;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class ProgressIndicatorAsCursor extends Application {

@Override
public void start(Stage primaryStage) {

Button button = new Button("Start");

Service<Void> service = new Service<Void>() {
@Override
public Task<Void> createTask() {
return new Task<Void>() {
@Override
public Void call() throws Exception {
for (int i = 1 ; i <= 1000; i++) {
Thread.sleep(10);
updateProgress(i, 1000);
}
return null ;
}
};
}
};

button.disableProperty().bind(service.runningProperty());
button.setOnAction(e -> service.restart());

ProgressIndicator pi = new ProgressIndicator();
pi.progressProperty().bind(service.progressProperty());

Pane pane = new Pane();

// fill pane with rectangle as task progresses:
Rectangle rectangle = new Rectangle();
rectangle.setFill(Color.CORNFLOWERBLUE);
rectangle.setX(0);
rectangle.widthProperty().bind(pane.widthProperty());

rectangle.heightProperty().bind(pane.heightProperty().multiply(service.progressProperty()));
rectangle.yProperty().bind(pane.heightProperty().subtract(rectangle.heightProperty()));

pane.getChildren().add(rectangle);

Label label = new Label();
label.graphicProperty().bind(
Bindings.when(service.runningProperty())
.then(pi)
.otherwise((ProgressIndicator)null));

pane.setOnMouseEntered(e ->
pane.getChildren().add(label));
pane.setOnMouseExited(e ->
pane.getChildren().remove(label));
pane.setOnMouseMoved(e -> label.relocate(e.getX(), e.getY()));

pane.cursorProperty().bind(
Bindings.when(service.runningProperty())
.then(Cursor.NONE)
.otherwise(Cursor.DEFAULT));

BorderPane.setAlignment(button, Pos.CENTER);
BorderPane.setMargin(button, new Insets(10));
BorderPane root = new BorderPane(pane, new Rectangle(0,0,0,20), null, button, null);

primaryStage.setScene(new Scene(root, 400, 600));
primaryStage.show();

}

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

关于java - ProgressIndicator(节点)作为鼠标光标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31707935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com