JavaFX简单更新标签(线程)
我试图向一些初学者演示如何将JavaFX应用程序上的标签设置为自动更新。基本上,他们希望标签上的值每分钟左右减少一次,而不需要任何用户交互 Java不是我的强项,通过查看前面的一些问题,我发现我需要处理线程和Runnable() 我已经把下面的代码放在一起,但我只是想知道是否有更好的方法来实现这一点,或者有更简单的方法来用更简单的代码演示相同的结果JavaFX简单更新标签(线程),java,multithreading,javafx,javafx-2,Java,Multithreading,Javafx,Javafx 2,我试图向一些初学者演示如何将JavaFX应用程序上的标签设置为自动更新。基本上,他们希望标签上的值每分钟左右减少一次,而不需要任何用户交互 Java不是我的强项,通过查看前面的一些问题,我发现我需要处理线程和Runnable() 我已经把下面的代码放在一起,但我只是想知道是否有更好的方法来实现这一点,或者有更简单的方法来用更简单的代码演示相同的结果 public class MainTimer2 extends Application { private int count = 100;
public class MainTimer2 extends Application {
private int count = 100;
private Label response = new Label(Integer.toString(count));
public static void main(String[] args) {
launch(args);
}
//Update function
private void decrementCount() {
count--;
response.setText(Integer.toString(count));
}
@Override
public void start(Stage myStage) {
myStage.setTitle("Update Demo");
//Vertical and horizontal gaps set to 10px
FlowPane rootNode = new FlowPane(10, 10);
rootNode.setAlignment(Pos.CENTER);
Scene myScene = new Scene(rootNode, 200, 100);
myStage.setScene(myScene);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Runnable updater = new Runnable() {
@Override
public void run() {
decrementCount();
}
};
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println("Timer error");
}
// UI update is run on the Application thread
Platform.runLater(updater);
}
}
});
// don't let thread prevent JVM shutdown
thread.setDaemon(true);
thread.start();
rootNode.getChildren().addAll(response);
myStage.show();
}
}
使用以下命令进行倒计时:
或者,您可以使用:
有很多方法可以做到这一点。主要方法是使用
时间线
和任务
。您还可以通过绑定来更新标签,而不是显式调用setText()
。总之,我认为您的方法已经很简单,适合初学者使用——使用其他方法需要您和您的“观众”更加熟悉JavaFX。但是,请注意,除了控件来自JavaFX之外,您的方法并没有使用太多JavaFXAPI。我尝试遵循的规则的可能重复之处在于,当您要更新GUI元素时,请使用动画类中的某些内容。当不使用线程/计时器时。实际上,任何扩展动画
的操作都将类似,只有时间线
内置了自动循环功能。
import javafx.animation.PauseTransition;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class MainTimer2 extends Application {
private int count = 100;
private Label response = new Label(Integer.toString(count));
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage myStage) {
myStage.setTitle("Update Demo");
//Vertical and horizontal gaps set to 10px
FlowPane rootNode = new FlowPane(10, 10);
rootNode.setAlignment(Pos.CENTER);
Scene myScene = new Scene(rootNode, 200, 100);
myStage.setScene(myScene);
rootNode.getChildren().addAll(response);
myStage.show();
update();
}
private void update() {
PauseTransition pause = new PauseTransition(Duration.seconds(1));
pause.setOnFinished(event ->{
decrementCount();
pause.play();
});
pause.play();
}
//Update function
private void decrementCount() {
count = (count > 0) ? count -1 : 100;
response.setText(Integer.toString(count));
}
}
private void update() {
KeyFrame keyFrame = new KeyFrame(
Duration.seconds(1),
event -> {
decrementCount();
}
);
Timeline timeline = new Timeline();
timeline.setCycleCount(Animation.INDEFINITE);
//if you want to limit the number of cycles use
//timeline.setCycleCount(100);
timeline.getKeyFrames().add(keyFrame);
timeline.play();
}