gpt4 book ai didi

JavaFX:逐渐更改绑定(bind)到后台线程中的 DoubleProperty

转载 作者:行者123 更新时间:2023-12-03 17:14:33 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





JavaFX UI Frozen when performing Task in new Thread

(1 个回答)


5年前关闭。




我正在尝试以编程方式为 JavaFX 中一组矩形的大小调整设置动画,但我遇到的问题很可能是由于线程。

程序在遇到 widthOfRectangles.set(... 时崩溃即使它是后台线程中的一个循环并且它应该停止。我假设设置 DoubleProperty 我所有的矩形宽度都绑定(bind)到 Platform.runLater不会真的工作。

所以我又试了一次,没有Platform.runLater (不是下面显示的代码),但是这一次,它滞后于矩形应该扩展/最小化的持续时间,然后突然跳到结束状态 - 至少它没有崩溃,但它不是平滑的我一直在寻找的动画。

是否可以通过 PropertyBindings 平滑地插入节点的属性,如果是这样,我将如何在不卡住 JavaFX 主线程的情况下进行呢?

expandProgressGraphic = new Thread(new Task<Void>() {
@Override
public Void call() throws Exception {
while (widthOfRectangles.get() < 130) {
Platform.runLater(() ->
widthOfRectangles.set(widthOfRectangles.get() + (130 - widthOfRectangles.get()) / 3 + 2));

try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
widthOfRectangles.set(130);
return null;
}
});
expandProgressGraphic.setDaemon(true);

minimizeProgressGraphic = new Thread(new Task<Void>() {
@Override
public Void call() throws Exception {
while (widthOfRectangles.get() > 40) {
Platform.runLater(() ->
widthOfRectangles.set(widthOfRectangles.get() - (widthOfRectangles.get() - 40) / 3 - 2));
try {
Thread.sleep(30);
} catch (InterruptedException e) {e.printStackTrace();}
}
widthOfRectangles.set(40);
return null;
}
});
expandProgressGraphic.setDaemon(true);


pane.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(widthOfRectangles.get() != 130 && event.getX() < widthOfRectangles.get() && !expandProgressGraphic.isAlive()) {
expandProgressGraphic.run();
} else if(widthOfRectangles.get() != 40 && event.getX() > widthOfRectangles.get() && !minimizeProgressGraphic.isAlive()){
minimizeProgressGraphic.run();
}
}
});

最佳答案

再次感谢@sillyfly。

我应该使用 Thread.start()而不是 Thread.run()

关于JavaFX:逐渐更改绑定(bind)到后台线程中的 DoubleProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38523132/

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