【发布时间】:2018-05-07 22:10:53
【问题描述】:
我想创建一个在每一秒过去后递减的计数器,但是当我运行代码时,有时会在 TextField 中显示正确的值,有时会重复它,或者 NetBeans 控制台会出现一些奇怪的错误。
谁能帮我解决这个问题? 我只是对代码进行了几处更改,但没有成功。
代码如下:
package javafx_contador;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.control.TextField;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.geometry.Pos;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaFX_Contador extends Application
{
TextField tf;
Thread1 contador;
@Override
public void start(Stage stage)
{
tf = new TextField();
tf.setEditable(false);
tf.setMaxWidth(100);
tf.setAlignment(Pos.CENTER);
tf.setLayoutX(160);
tf.setLayoutY(140);
tf.setFont(Font.font("Cambria", FontWeight.BOLD, 20));
Pane pane = new Pane();
pane.setStyle("-fx-background: green;");
pane.getChildren().add(tf);
Scene scene = new Scene(pane, 400, 300);
stage.setResizable(false);
stage.setTitle("JavaFX (Contador)");
stage.setScene(scene);
stage.show();
pane.requestFocus();
stage.setOnCloseRequest((WindowEvent we) ->
{
System.out.println("A Aplicação vai encerrar.");
Platform.exit();
System.exit(0);
});
contador = new Thread1();
contador.start();
}
public class Thread1 extends Thread
{
int tempo = 60;
@Override
public void run()
{
tf.setText(Integer.toString(tempo));
while (tempo > 0)
{
try
{
Thread1.sleep(1000);
}
catch (InterruptedException ex)
{
Logger.getLogger(JavaFX_Contador.class.getName()).log(Level.SEVERE, null, ex);
}
tempo--;
tf.setText(Integer.toString(tempo));
}
System.out.println("Terminou o tempo!");
}
}
public static void main(String[] args)
{
launch(args);
}
}
我在没有“Thread.Sleep”的情况下编写了代码,而是使用了“PauseTransition”,但有一个问题,即使在消息“Terminou o tempo!”之后,计数器也会停止在 -1。出现在控制台中。 我有“while (tempo > 0)”所以我不明白为什么它在达到值 0 时不会停止。
package javafx_contador;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.control.TextField;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.geometry.Pos;
import javafx.animation.PauseTransition;
import javafx.util.Duration;
public class JavaFX_Contador extends Application
{
int tempo = 60;
TextField tf;
Thread1 contador;
@Override
public void start(Stage stage)
{
tf = new TextField(Integer.toString(tempo));
tf.setEditable(false);
tf.setMaxWidth(100);
tf.setAlignment(Pos.CENTER);
tf.setLayoutX(160);
tf.setLayoutY(140);
tf.setFont(Font.font("Cambria", FontWeight.BOLD, 20));
Pane pane = new Pane();
pane.setStyle("-fx-background: green;");
pane.getChildren().add(tf);
Scene scene = new Scene(pane, 400, 300);
stage.setResizable(false);
stage.setTitle("JavaFX (Contador)");
stage.setScene(scene);
stage.show();
pane.requestFocus();
stage.setOnCloseRequest((WindowEvent we) ->
{
System.out.println("A Aplicação vai encerrar.");
Platform.exit();
System.exit(0);
});
contador = new Thread1();
contador.start();
}
public class Thread1 extends Thread
{
@Override
public void run()
{
PauseTransition pausa = new PauseTransition(Duration.millis(1000));
pausa.setOnFinished(e ->
{
tempo--;
tf.setText(Integer.toString(tempo));
});
while (tempo > 0)
pausa.play();
System.out.println("Terminou o tempo!");
}
}
public static void main(String[] args)
{
launch(args);
}
}
【问题讨论】:
-
仅从 javafx 应用程序线程更新 GUI。这可能会有所帮助:
Platform.runLater和Task。
标签: java multithreading javafx javafx-8