繁体   English   中英

超时后如何打开另一个窗口?

[英]how to open another window after time?

我正在 javaFX 中的文本编辑器上工作,并且在文本编辑器打开之前我想要一个小的启动窗口。 我希望启动窗口在 Time 之后关闭,然后文本编辑器窗口打开。 问题是,启动程序时没有显示启动窗口。 当时间结束时,TextEditor 窗口很快就会显示出来,但随后程序崩溃并向我显示语法错误:

调用目标异常

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

        MainWindow.openAnother(primaryStage);

        primaryStage.setScene(new Scene(root, 300, 200));
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

主窗口类:

package sample;

import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class MainWindow {

    public static void openAnother(Stage primaryStage) {
        try {
            Thread.sleep(4345);
            primaryStage.close();
            Fenster(primaryStage);
        } catch(InterruptedException e) { }
    }

    public static void Fenster(Stage nextStage) {
        StackPane root = new StackPane();

        //TODO TextEditor-Code

        Scene scene = new Scene(root);
        nextStage.setScene(scene);
        nextStage.setFullScreen(true);
        nextStage.show();
    }
}

使用时间线。

此示例启动一个初始屏幕阶段 (primaryStage),然后在三秒后显示 textEditorStage,然后在一秒钟后初始屏幕阶段将关闭。

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Example extends Application {

  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage primaryStage) throws Exception {
    primaryStage.setScene(new Scene(new AnchorPane(), 200, 200));
    primaryStage.setTitle("Splash Screen");
    primaryStage.setAlwaysOnTop(true);
    primaryStage.show();

    Timeline timeline = new Timeline(new KeyFrame(Duration.millis(3000), event -> {
      Stage textEditorStage = new Stage();
      textEditorStage.setScene(new Scene(new AnchorPane(), 400, 400));
      textEditorStage.setTitle("Text Editor");
      textEditorStage.show();
      Timeline timeline2 = new Timeline(new KeyFrame(Duration.millis(1000), event2 -> {
        primaryStage.close();
      }));
      timeline2.play();
    }));
    timeline.play();
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM