Multithreading JavaFXML在后台进程完成后,我想隐藏splash并显示主场景
免责声明——请原谅我的术语,它可能不太准确 功能齐全的应用程序,I wan可提高线程功能和性能 运行时,它将显示大约40秒的启动屏幕,然后显示主场景布局。 显示splash时,它将启动一个命令行进程,将输出写入.txt文件。此过程需要30秒才能完成 问题——如果将控制传递到主场景时命令行进程未完成 主场景将无法从.txt文件中读取任何内容 我希望在命令行进程完成将输出写入文件后立即显示主场景布局。不要等待40秒,希望命令过程已经完成 主类加载器 公共类SplashLoader扩展了应用程序{Multithreading JavaFXML在后台进程完成后,我想隐藏splash并显示主场景,multithreading,performance,asynchronous,javafx,fxml,Multithreading,Performance,Asynchronous,Javafx,Fxml,免责声明——请原谅我的术语,它可能不太准确 功能齐全的应用程序,I wan可提高线程功能和性能 运行时,它将显示大约40秒的启动屏幕,然后显示主场景布局。 显示splash时,它将启动一个命令行进程,将输出写入.txt文件。此过程需要30秒才能完成 问题——如果将控制传递到主场景时命令行进程未完成 主场景将无法从.txt文件中读取任何内容 我希望在命令行进程完成将输出写入文件后立即显示主场景布局。不要等待40秒,希望命令过程已经完成 主类加载器 公共类SplashLoader扩展了应用程序{ @
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Splash.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.initStyle(StageStyle.UNDECORATED);
stage.setResizable(false);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Splash控制器
公共类SplashController实现可初始化{
static String ping = "";
@FXML AnchorPane ap;
//Loads the Speedtest.fxml into the rootpane to be displayed set to run 40 seconds after the splash screen has displayed
class ShowSpeedTestScreen extends Thread{
@Override
public void run(){
try {
Thread.sleep(30000);
Platform.runLater(() -> {
Stage stage = new Stage();
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("SpeedTest.fxml"));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setResizable(false);
stage.setTitle("It Help");
stage.show();
ap.getScene().getWindow().hide();
});
} catch (InterruptedException ex) {}
}
}
//On initialization of the splash screen
//Start network speedtest average 30 sec run time
//Create seperate thread to run in 40 seconds that sets the Speedtest.fxml layout to the stage via ShowSpeedTestScreen() class
//It is important that the runtime command has 30 seconds to finish its process else the speedtest will not be able to retrieve the download and upload speeds
@Override
public void initialize(URL url, ResourceBundle rb) {
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "mkdir C:\\Temp | fast -u > C:\\Temp\\speedtest.txt\"");
try {Process process = builder.start();}
catch (IOException ex) {}
ping = checkPing();
new ShowSpeedTestScreen().start();
}
//Nodes on the Layout from "SpeedTest.fxml"
@FXML
private Label lblDownloadNum;
@FXML
private Label lblPingNum;
@FXML
private Label lblTitle;
@FXML
private Label lblUploadNum;
@FXML
private Label lblmbps;
@FXML
private Label lblmbps2;
@FXML
private Label lblms;
@FXML
private AnchorPane rootPane;
AnchorPane netView;
String downloadSpeed = "";
String uploadSpeed = "";
@Override
public void initialize(URL url, ResourceBundle rb) {
lblPingNum.setText(getPing());
lblms.setVisible(true);
downloadSpeed = checkDownloadSpeed();
lblDownloadNum.setText(downloadSpeed);
lblmbps.setVisible(true);
uploadSpeed = checkUploadSpeed();
lblUploadNum.setText(uploadSpeed);
lblmbps2.setVisible(true);
try {netView = FXMLLoader.load(getClass().getResource("NetworkView.fxml"));}
catch(Exception ex){}
}
//Parsing a txt file for the first line first word
//Set selected node to a content holder
private void setNode(Node node) {
rootPane.getChildren().clear();
rootPane.getChildren().add((Node) node);
FadeTransition ft = new FadeTransition(Duration.millis(1500));
ft.setNode(node);
ft.setFromValue(0.1);
ft.setToValue(1);
ft.setCycleCount(1);
ft.setAutoReverse(false);
ft.play();
}
//Creates new Stage and scene to display second Gui Window
//Passes control to the NetworkViewController
@FXML
private void changeSceneButtonPushed(javafx.event.ActionEvent event) throws IOException {
setNode(netView);
}
}
主布局控制器
公共类SpeedTestController实现可初始化{
static String ping = "";
@FXML AnchorPane ap;
//Loads the Speedtest.fxml into the rootpane to be displayed set to run 40 seconds after the splash screen has displayed
class ShowSpeedTestScreen extends Thread{
@Override
public void run(){
try {
Thread.sleep(30000);
Platform.runLater(() -> {
Stage stage = new Stage();
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("SpeedTest.fxml"));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setResizable(false);
stage.setTitle("It Help");
stage.show();
ap.getScene().getWindow().hide();
});
} catch (InterruptedException ex) {}
}
}
//On initialization of the splash screen
//Start network speedtest average 30 sec run time
//Create seperate thread to run in 40 seconds that sets the Speedtest.fxml layout to the stage via ShowSpeedTestScreen() class
//It is important that the runtime command has 30 seconds to finish its process else the speedtest will not be able to retrieve the download and upload speeds
@Override
public void initialize(URL url, ResourceBundle rb) {
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "mkdir C:\\Temp | fast -u > C:\\Temp\\speedtest.txt\"");
try {Process process = builder.start();}
catch (IOException ex) {}
ping = checkPing();
new ShowSpeedTestScreen().start();
}
//Nodes on the Layout from "SpeedTest.fxml"
@FXML
private Label lblDownloadNum;
@FXML
private Label lblPingNum;
@FXML
private Label lblTitle;
@FXML
private Label lblUploadNum;
@FXML
private Label lblmbps;
@FXML
private Label lblmbps2;
@FXML
private Label lblms;
@FXML
private AnchorPane rootPane;
AnchorPane netView;
String downloadSpeed = "";
String uploadSpeed = "";
@Override
public void initialize(URL url, ResourceBundle rb) {
lblPingNum.setText(getPing());
lblms.setVisible(true);
downloadSpeed = checkDownloadSpeed();
lblDownloadNum.setText(downloadSpeed);
lblmbps.setVisible(true);
uploadSpeed = checkUploadSpeed();
lblUploadNum.setText(uploadSpeed);
lblmbps2.setVisible(true);
try {netView = FXMLLoader.load(getClass().getResource("NetworkView.fxml"));}
catch(Exception ex){}
}
//Parsing a txt file for the first line first word
//Set selected node to a content holder
private void setNode(Node node) {
rootPane.getChildren().clear();
rootPane.getChildren().add((Node) node);
FadeTransition ft = new FadeTransition(Duration.millis(1500));
ft.setNode(node);
ft.setFromValue(0.1);
ft.setToValue(1);
ft.setCycleCount(1);
ft.setAutoReverse(false);
ft.play();
}
//Creates new Stage and scene to display second Gui Window
//Passes control to the NetworkViewController
@FXML
private void changeSceneButtonPushed(javafx.event.ActionEvent event) throws IOException {
setNode(netView);
}
}请???