gpt4 book ai didi

java - 不断更新 JavaFX GUI

转载 作者:行者123 更新时间:2023-11-29 08:31:34 24 4
gpt4 key购买 nike

我目前正在做一个项目,我将时间转换为以 10 为基数的时间(基本上它最终会显示已经过去的一天的百分比。例如:中午 12:00 在以 10 为基数时将显示为 50.00时间)。目前,我知道我的算法是正确的,因为如果我将它打印到控制台,它会正确打印出来,但由于某种原因,我无法显示我的 GUI。如果我摆脱了我尝试不断更新 GUI 以显示正确数字的部分,GUI 显示正常,但没有数字。我的代码如下:

package ClockPackage;

import java.util.Calendar;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class ClockView extends Application {
Pane background;
static Text firstDigit;
static Text secondDigit;
static Text thirdDigit;
static Text fourthDigit;
Text middleDecimal;

/* Sets the first digit of the base 10 time to the passed through char. */
static void setFirstDigit(char x1) {
String digitString = "";
digitString += x1;
firstDigit.setText(digitString);
}

/* Sets the second digit of the base 10 time to the passed through char. */
static void setSecondDigit(char x2) {
String digitString = "";
digitString += x2;
secondDigit.setText(digitString);
}

/* Sets the third digit of the base 10 time to the passed through char. */
static void setThirdDigit(char y1) {
String digitString = "";
digitString += y1;
thirdDigit.setText(digitString);
}

/* Sets the fourth digit of the base 10 time to the passed through char. */
static void setFourthDigit(char y2) {
String digitString = "";
digitString += y2;
fourthDigit.setText(digitString);
}

/* Main Method that Launches the GUI */
public static void main(String[] args) {
Application.launch(args);

}

@Override
public void start(Stage primaryStage) throws Exception {
final double TEXTFIELD_LAYOUT_Y = 200;

//Background Pane
background = new Pane();

//First digit textField
firstDigit = new Text();
firstDigit.setLayoutX(17);
firstDigit.setLayoutY(TEXTFIELD_LAYOUT_Y);
firstDigit.setStyle("-fx-font-size: 96pt;");

//Second digit textField
secondDigit = new Text();
secondDigit.setLayoutX(117);
secondDigit.setLayoutY(TEXTFIELD_LAYOUT_Y);
secondDigit.setStyle("-fx-font-size: 96pt;");

//Middle decimal
middleDecimal = new Text(".");
middleDecimal.setLayoutX(219);
middleDecimal.setLayoutY(210);
middleDecimal.setStyle("-fx-font-size: 72pt;");

//Third digit textField
thirdDigit = new Text();
thirdDigit.setLayoutX(250);
thirdDigit.setLayoutY(TEXTFIELD_LAYOUT_Y);
thirdDigit.setStyle("-fx-font-size: 96pt;");

//Fourth digit textField
fourthDigit = new Text();
fourthDigit.setLayoutX(362);
fourthDigit.setLayoutY(TEXTFIELD_LAYOUT_Y);
fourthDigit.setStyle("-fx-font-size: 96pt;");

/* Adding the Nodes to the Pane */
background.getChildren().addAll(firstDigit, secondDigit, middleDecimal, thirdDigit, fourthDigit);

/* Setting the Scene */
Scene scene = new Scene(background, 470, 258);
primaryStage.setTitle("Base 10 Clock");
primaryStage.setScene(scene);
primaryStage.show();

/*
* Calculates the time in base 10 time and calls the 4 methods
* to set the GUI display.
*
* In a constant while loop in order to continuously update
* the GUI.
*/
Calendar now;
double currentTime;
String timeString;
long timestamp;
while(true) {
/* Sleep for 8.64 seconds since that is how long it is between
increments of 0.01 in base 10 time. */
Thread.sleep(8640);
now = Calendar.getInstance();
timestamp = now.get(Calendar.HOUR_OF_DAY)*60*60 + now.get(Calendar.MINUTE)*60 + now.get(Calendar.SECOND);
currentTime = timestamp/86400.0;
timeString = "" + currentTime;
setFirstDigit(timeString.charAt(2));
setSecondDigit(timeString.charAt(3));
setThirdDigit(timeString.charAt(4));
setFourthDigit(timeString.charAt(5));
}
}
}

有谁知道我将如何让 GUI 既显示又不断更新数字?我不知道如何让它同时做到这两点。我见过人们用按钮更新数据的地方,但我没见过显示不断自动更新的地方。

谢谢!

最佳答案

Replace:

    Calendar now;
double currentTime;
String timeString;
long timestamp;
while (true)
{
/* Sleep for 8.64 seconds since that is how long it is between
increments of 0.01 in base 10 time. */
Thread.sleep(8640);
now = Calendar.getInstance();
timestamp = now.get(Calendar.HOUR_OF_DAY) * 60 * 60 + now.get(Calendar.MINUTE) * 60 + now.get(Calendar.SECOND);
currentTime = timestamp / 86400.0;
timeString = "" + currentTime;
setFirstDigit(timeString.charAt(2));
setSecondDigit(timeString.charAt(3));
setThirdDigit(timeString.charAt(4));
setFourthDigit(timeString.charAt(5));
}

with:

        Timeline overEightSeconsWonder = new Timeline(new KeyFrame(Duration.seconds(8.64), (ActionEvent event) ->
{
Calendar now = Calendar.getInstance();
long timestamp = now.get(Calendar.HOUR_OF_DAY) * 60 * 60 + now.get(Calendar.MINUTE) * 60 + now.get(Calendar.SECOND);
double currentTime = timestamp / 86400.0;
String timeString = "" + currentTime;
setFirstDigit(timeString.charAt(2));
setSecondDigit(timeString.charAt(3));
setThirdDigit(timeString.charAt(4));
setFourthDigit(timeString.charAt(5));
}));
overEightSeconsWonder.setCycleCount(Timeline.INDEFINITE);
overEightSeconsWonder.play();

关于java - 不断更新 JavaFX GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47704888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com