매 초마다 전달되는 카운터가 작아 지지만 코드를 실행하면 때때로 TextField에 corret 값이 나타나고 다른 시간은 복제됩니다. NetBeans Console에는 이상한 오류가 있습니다.JavaFX 8 - 새 스레드가있는 카운터를 만드는 데 보조 효과가 있음
아무도 도와 줄 수 있습니까? 방금 코드에서 몇 가지 변경 사항을 만들었지 만 운이 없다. 나는 "Thread.sleep를"하지 않고 코드를 만들어 대신 내가 "PauseTransition"를 사용하지만 한 가지 문제가있다
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);
}
}
는, 카운터도 메시지 이후에 -1 중지 : 여기
코드입니다 "Terminou o 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);
}
}
처럼 Platform.runLater를 사용하여 메인 UI 스레드. 이것은 도움이 될 수 있습니다 : ['Platform.runLater'] (https://docs.oracle.com/javase/8/javafx/api/javafx/application/Platform.html#runLater-java.lang.Runnable-) 및 [' Task'] (https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html)를 참조하십시오. – fabian