如何在JavaFX中创建一个线程?我环顾四周,没有答案是明确/我想要的,这与Java的Thread Runnable基本相同,它与JavaFx不相容,除非它是用于后台任务。
我的基本开始应用程序类:
public class Main extends Application {
private Stage stage;
private AnchorPane rootLayout;
@Override
public void start(Stage stage) {
this.stage = stage;
this.stage.setTitle("Main");
setLayout();
}
private void setLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/View.fxml"));
rootLayout = (AnchorPane) loader.load();
Scene scene = new Scene(rootLayout);
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
我需要一个JavaFX线程,它会不断更改按钮的可见性,而不会在应用程序运行时冻结/暂停,我该怎么做呢?
编辑:我需要这个,但对于JavaFX应用程序:
public void run(){
while (true){
if (button.isVisible)
button.setVisibility(false);
else
button.setVisibility(true);
Thread.sleep(1000);
}
}