更新时间:2022-01-29 23:00:44
您仍然可以使用独立类。
You can still use your standalone class.
规则是必须在FX应用程序线程上更改UI。您可以通过包装来自其他线程的调用来实现这一点,这些线程会更改您传递给 Platform.runLater(...)的
。 Runnable
中的UI
The rule is that changes to the UI must happen on the FX Application Thread. You can cause that to happen by wrapping calls from other threads that change the UI in Runnable
s that you pass to Platform.runLater(...)
.
在您的示例代码中, d.addElement(i)
更改了UI ,所以你会这样做:
In your example code, d.addElement(i)
changes the UI, so you would do:
class Thread2 implements Runnable {
private DifferentThreadJavaFXMinimal d;
Thread2(DifferentThreadJavaFXMinimal dt){
d=dt;
}
@Override
public void run() {
for (int i=0;i<4;i++) {
final int value = i ;
Platform.runLater(() -> d.addElement(value));
// presumably in real life some kind of blocking code here....
}
System.out.println("Hahó");
}
}