不断更新JavaFXGUI
我目前正在做一个项目,我将时间转换为10个基准时间(基本上它将显示一天中已过去的百分比。例如:中午12:00将显示为10个基准时间中的50.00)。目前,我知道我的算法是正确的,因为如果我将它打印到控制台,它会正确输出,但由于某些原因,我无法显示我的GUI。如果我去掉了试图不断更新GUI以显示正确数字的部分,GUI显示良好,但没有数字。我的代码如下:不断更新JavaFXGUI,java,user-interface,javafx,Java,User Interface,Javafx,我目前正在做一个项目,我将时间转换为10个基准时间(基本上它将显示一天中已过去的百分比。例如:中午12:00将显示为10个基准时间中的50.00)。目前,我知道我的算法是正确的,因为如果我将它打印到控制台,它会正确输出,但由于某些原因,我无法显示我的GUI。如果我去掉了试图不断更新GUI以显示正确数字的部分,GUI显示良好,但没有数字。我的代码如下: package ClockPackage; import java.util.Calendar; import javafx.applicati
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显示并不断更新数字吗?我想不出怎样才能做到两者兼得。我见过人们使用按钮更新数据的地方,但我没有见过显示器不断自动更新的地方
谢谢
替换:
与:
替换:
与:
您应该用新线程替换
while(true)
循环,或者用另一个线程替换服务调用。主线程已经负责更新UI,因此当您遇到某种阻塞(IO、耗时的循环等)时,它将阻止UI线程更新
创建服务:
public static class TimeService extends Service<String> {
protected Task createTask() {
return new Task<String>() {
protected String call() throws Exception {
// TODO: Do work - return the date/time as a String
// If you need a Thread.sleep, use a spawn a new thread
}
};
}
}
您还可以执行各种操作,例如将UI元素绑定到TimeService中的属性(这样,UI将在服务运行时“仅更新”)您应该使用新线程替换
while(true)
循环,或者使用另一个线程替换服务调用。主线程已经负责更新UI,因此当您遇到某种阻塞(IO、耗时的循环等)时,它将阻止UI线程更新
创建服务:
public static class TimeService extends Service<String> {
protected Task createTask() {
return new Task<String>() {
protected String call() throws Exception {
// TODO: Do work - return the date/time as a String
// If you need a Thread.sleep, use a spawn a new thread
}
};
}
}
您还可以执行各种操作,例如将UI元素绑定到TimeService中的属性(这样,UI将在服务运行时“仅更新”)这是一个工作版本
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Application;
import javafx.scene.Group;
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;
public final Timer clockTimer = new Timer();
/* 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) {
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("0");
firstDigit.setLayoutX(17);
firstDigit.setLayoutY(TEXTFIELD_LAYOUT_Y);
firstDigit.setStyle("-fx-font-size: 96pt;");
//Second digit textField
secondDigit = new Text("0");
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("0");
thirdDigit.setLayoutX(250);
thirdDigit.setLayoutY(TEXTFIELD_LAYOUT_Y);
thirdDigit.setStyle("-fx-font-size: 96pt;");
//Fourth digit textField
fourthDigit = new Text("0");
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(new Group(), 470, 258);
Group root = (Group)scene.getRoot();
root.getChildren().add(background);
primaryStage.setTitle("Base 10 Clock");
primaryStage.setScene(scene);
primaryStage.show();
clockTimer.scheduleAtFixedRate(new TimerTask() {
Calendar now;
double currentTime;
String timeString;
long timestamp;
@Override
public void run() {
/*
* 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.
*/
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;
Platform.runLater(new Runnable() {
@Override public void run() {
setFirstDigit(timeString.charAt(2));
setSecondDigit(timeString.charAt(3));
setThirdDigit(timeString.charAt(4));
setFourthDigit(timeString.charAt(5));
}
});
}
}, 0, 8640 // Sleep for 8.64 seconds since that is how long it is between
); // increments of 0.01 in base 10 time.
}
}
这是一个工作版本
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Application;
import javafx.scene.Group;
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;
public final Timer clockTimer = new Timer();
/* 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) {
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("0");
firstDigit.setLayoutX(17);
firstDigit.setLayoutY(TEXTFIELD_LAYOUT_Y);
firstDigit.setStyle("-fx-font-size: 96pt;");
//Second digit textField
secondDigit = new Text("0");
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("0");
thirdDigit.setLayoutX(250);
thirdDigit.setLayoutY(TEXTFIELD_LAYOUT_Y);
thirdDigit.setStyle("-fx-font-size: 96pt;");
//Fourth digit textField
fourthDigit = new Text("0");
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(new Group(), 470, 258);
Group root = (Group)scene.getRoot();
root.getChildren().add(background);
primaryStage.setTitle("Base 10 Clock");
primaryStage.setScene(scene);
primaryStage.show();
clockTimer.scheduleAtFixedRate(new TimerTask() {
Calendar now;
double currentTime;
String timeString;
long timestamp;
@Override
public void run() {
/*
* 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.
*/
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;
Platform.runLater(new Runnable() {
@Override public void run() {
setFirstDigit(timeString.charAt(2));
setSecondDigit(timeString.charAt(3));
setThirdDigit(timeString.charAt(4));
setFourthDigit(timeString.charAt(5));
}
});
}
}, 0, 8640 // Sleep for 8.64 seconds since that is how long it is between
); // increments of 0.01 in base 10 time.
}
}
while(true)
-<切勿在GUI线程中的JavaFX
程序中执行此操作。使用类。可能更适合这种情况。while(true)
-<切勿在GUI线程中的JavaFX
程序中执行此操作。使用类。可能更适合这个。这不好。这将更改JavaFX应用程序线程的场景图,这可能会导致。适当使用可以解决此问题。@jewelsea根据建议更改gui更新以使用Platform.runLater。更好的MVC设计会有所帮助。谢谢你的建议。这不好。这将更改JavaFX应用程序线程的场景图,这可能会导致。适当使用可以解决此问题。@jewelsea根据建议更改gui更新以使用Platform.runLater。更好的MVC设计会有所帮助。谢谢你的建议。
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Application;
import javafx.scene.Group;
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;
public final Timer clockTimer = new Timer();
/* 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) {
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("0");
firstDigit.setLayoutX(17);
firstDigit.setLayoutY(TEXTFIELD_LAYOUT_Y);
firstDigit.setStyle("-fx-font-size: 96pt;");
//Second digit textField
secondDigit = new Text("0");
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("0");
thirdDigit.setLayoutX(250);
thirdDigit.setLayoutY(TEXTFIELD_LAYOUT_Y);
thirdDigit.setStyle("-fx-font-size: 96pt;");
//Fourth digit textField
fourthDigit = new Text("0");
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(new Group(), 470, 258);
Group root = (Group)scene.getRoot();
root.getChildren().add(background);
primaryStage.setTitle("Base 10 Clock");
primaryStage.setScene(scene);
primaryStage.show();
clockTimer.scheduleAtFixedRate(new TimerTask() {
Calendar now;
double currentTime;
String timeString;
long timestamp;
@Override
public void run() {
/*
* 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.
*/
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;
Platform.runLater(new Runnable() {
@Override public void run() {
setFirstDigit(timeString.charAt(2));
setSecondDigit(timeString.charAt(3));
setThirdDigit(timeString.charAt(4));
setFourthDigit(timeString.charAt(5));
}
});
}
}, 0, 8640 // Sleep for 8.64 seconds since that is how long it is between
); // increments of 0.01 in base 10 time.
}
}