【问题标题】:Can we change the scene in Java Scene Builder 2.0 without using button after 5 seconds我们可以在 Java Scene Builder 2.0 中在 5 秒后不使用按钮来更改场景吗
【发布时间】:2018-08-05 13:55:31
【问题描述】:

基本上我使用的是 JavaFX 场景构建器 2.0,我想在不使用任何按钮的情况下将场景从一个更改为另一个。 主文件 公共类 OurFirstProject 扩展应用程序 {

public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    stage.setFullScreenExitHint("");
    //stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
    Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
    Scene scene = new Scene(root, screenBounds.getWidth(), screenBounds.getHeight());
    stage.setScene(scene);
    stage.setFullScreen(true);
    stage.show();

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

}

} 公共类 FXMLDocumentController 实现 Initializable {

@FXML
private void change(ActionEvent event) throws IOException {
    Parent sceneChange = FXMLLoader.load(getClass().getResource("Change.fxml"));
    Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
    Scene changeScene = new Scene(sceneChange, screenBounds.getWidth(), screenBounds.getHeight());
    Stage Window = (Stage) ((Node) event.getSource()).getScene().getWindow();
    Window.setScene(changeScene);
    Window.setFullScreen(true);
    Window.show();
}

int a = 0;
@FXML
public Button helloButton;
@FXML
private Label ourLabel;

@FXML
private void printHello(ActionEvent e) {
    a++;
    if (a % 2 == 0) {
        ourLabel.setText("Hello World! Kyun" + a);
    } else {
        ourLabel.setText("Hello Dunia" + a);
    }
}

@Override
public void initialize(URL url, ResourceBundle rb) {

在此处输入代码 }

FXML 文件名中提到的场景“更改”,我想在不使用按钮的情况下运行这个场景我想在第一场景的五秒延迟上运行这个。

【问题讨论】:

    标签: scenebuilder


    【解决方案1】:

    你可以使用 Timer() 之类的,

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {// After this you can add your change.fxml load code
                        Parent root = null;
                        try {
                            root = fxmlLoader.load();
                        }catch(Exception e)
                        {
                            //Exception catch code here
                        }
                        primaryStage.show();//Here you can write your show code like window.show()
                    }
                });
            }
        },5000);// 5000- time delay in milliseconds
    

    【讨论】:

      猜你喜欢
      • 2016-08-08
      • 2015-10-08
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多