我遇到了javaFX的问题。 我想每隔1000毫秒在App Window中显示时间。
public class Main extends Application {
StackPane root = new StackPane();
Time time;
Text t1;
@Override
public void start(Stage primaryStage) throws Exception{
root.setStyle("-fx-background-color: #00FF00");
primaryStage.setTitle("My App");
primaryStage.setScene(new Scene(root, 1000, 800));
primaryStage.show();
checkTime();
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
Platform.exit();
System.exit(0);
}
});
}
public static void main(String[] args) {
launch(args);
}
public void checkTime(){
time = new Time();
time.start();
}
public void displayTime(int hour, int minute, int second){
t1= new Text(200, 50, hour + ":" + minute + ":" + second);
root.getChildren().add(t1);
}
}
那是第二堂课:
package sample;
import java.util.Calendar;
public class Time extends Thread {
Thread t;
public int hour;
public int minute;
public int second;
Calendar calendar;
Main main = new Main();
Time() {
}
public void run() {
for (; ; ) {
try {
getTime();
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
public void start() {
t = new Thread(this);
t.start();
}
public void getTime() {
calendar = Calendar.getInstance();
hour = calendar.get(Calendar.HOUR);
minute = calendar.get(Calendar.MINUTE);
second = calendar.get(Calendar.SECOND);
System.out.println(hour + ":" + minute + ":" + second);
main.displayTime(hour, minute, second);
}
}
我希望它以类似于数字时钟的方式工作。在项目的下一个阶段,我将希望以类似于其他2D图形的方式使用这种方式。
此时,在打开应用程序后,在控制台中花费的时间是正确的。但是,我想在应用程序窗口中显示完全相同的时间,此时只显示背景而没有其他内容。
答案 0 :(得分:1)
Thread
扩展,因为JavaFX具有可以充当计时器的Timeline
之类的东西。如果您认为有必要(由于您未指定的其他原因),那么Time
类完全没问题。Thread
延伸并创建另一个Thread
绝对是不必要的。当Time
类扩展Thread
时,它本身已经是Thread
。Main
中创建Time
的新实例。 Main
应该创建Time
并将其自身传递给Time
,否则Time
将不会保留原始Main
对象的引用。Text
周期都会创建一个新的Time
对象,因此即使没有例外,这些Text
对象也会相互重叠。如果您出于其他特殊原因想要保留Time
课程,那么这通常是您应该做的事情:
public class Main extends Application {
StackPane root = new StackPane();
Time time;
Text t1 = new Text(); // Instantiates only once
@Override
public void start(Stage primaryStage) throws Exception{
root.setStyle("-fx-background-color: #00FF00");
primaryStage.setTitle("My App");
primaryStage.setScene(new Scene(root, 1000, 800));
primaryStage.show();
root.getChildren().add(t1);
checkTime();
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
Platform.exit();
System.exit(0);
}
});
}
public static void main(String[] args) {
launch(args);
}
public void checkTime(){
time = new Time(this); // Pass ownself into Time
time.start();
}
public void updateTime(int hour, int minute, int second){
Platform.runLater(() -> t1.setText(200, 50, hour + ":" + minute + ":" + second));
}
}
public class Time extends Thread {
//Thread t; // Not needed
public int hour;
public int minute;
public int second;
Calendar calendar;
Main main; // Don't instantiate
// Pass in the main object
Time(Main main) {
this.main = main;
}
public void run() {
for (; ; ) {
try {
getTime();
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
// This is not needed, Time class has its own start() method, which will call its run() method.
//public void start() {
// t = new Thread(this);
// t.start();
//}
public void getTime() {
calendar = Calendar.getInstance();
hour = calendar.get(Calendar.HOUR);
minute = calendar.get(Calendar.MINUTE);
second = calendar.get(Calendar.SECOND);
System.out.println(hour + ":" + minute + ":" + second);
main.updateTime(hour, minute, second); // Update main
}
}