【问题标题】:I want close the splash screen in JavaFx. But It is not closing instead the 2nd the window is opening我想关闭 JavaFx 中的启动画面。但它没有关闭,而是第二个窗口正在打开
【发布时间】:2021-10-13 05:19:05
【问题描述】:

我有 2 个 FXML 页面。当主页作为启动屏幕显示时,我需要自动关闭启动屏幕,然后我想打开仪表板。但是这里它没有关闭 JavaFx 中的启动画面,而是第二个窗口正在打开,并且在后台启动画面仍然显示在那里。我尝试了一些方法,但它不起作用。

以下是启动屏幕主页的代码。在 FXML 中进行设计并在 Netbeans 12 中进行编码。

package abc123;


import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;


public class HomeController implements Initializable {
 
    private AnchorPane rootpane ;
    
    @FXML
    private AnchorPane pane;
    
    public void initialize(URL url, ResourceBundle rb) {
     splash();
    }    
    private void splash() {
        new Thread(){
            public void run(){
                try {
                    Thread.sleep(5000);
                    
                } catch (Exception e) {
                    System.out.println(""+e);
                }
                Platform.runLater(new Runnable(){
                    @Override
                    public void run()
                    {
                        try {
                            rootpane = FXMLLoader.load(getClass().getResource("Maindash.fxml"));
                            Stage stage = new Stage();
                           
                            Scene scene = new Scene(rootpane);
                            //pane.setVisible(false);
                            stage.setTitle("Dash");
                            stage.setScene(scene);
                            stage.show();

                        } catch (Exception e) {
                        }
                        
                    }
                });
                 
            }
        }.start();
        
        
    }
    
}

【问题讨论】:

  • 你在哪里关闭当前阶段?
  • minimal reproducible example 请 .. 并在代码格式方面付出一些努力以使其易于阅读
  • @James_D 这是主要的prblm,我可以在其中设置关闭代码..
  • 当您显示新窗口时,您肯定想完全关闭原始窗口(在Platform.runLater() runnable 中)。
  • @James_D 这是在'stage,show()'下面吗??

标签: java javafx netbeans


【解决方案1】:

在打开新窗口时隐藏当前窗口。

private void splash() {
    new Thread(){
        public void run(){
            try {
                Thread.sleep(5000);
                
            } catch (Exception e) {
                e.printStackTrace();
            }
            Platform.runLater(new Runnable(){
                @Override
                public void run() {
                    try {
                        rootpane = FXMLLoader.load(getClass().getResource("Maindash.fxml"));
                        Stage stage = new Stage();
                       
                        Scene scene = new Scene(rootpane);
                        stage.setTitle("Dash");
                        stage.setScene(scene);
                        stage.show();

                        // hide current window:
                        pane.getScene().getWindow().hide();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    
                }
            });
             
        }
    }.start();
    
    
}

【讨论】:

    猜你喜欢
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    相关资源
    最近更新 更多