【发布时间】:2018-05-22 00:07:22
【问题描述】:
我遇到了 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 人物的方式使用这种方式。
此时,打开应用程序后,在控制台花费的时间是正确的。但是,我希望在应用程序窗口中显示完全相同的时间,此时只显示背景,不显示其他内容。
【问题讨论】: