簡體   English   中英

圖像作為 JavaFX 應用程序的啟動畫面不會自動隱藏

[英]Image as splashscreen for JavaFX application not hiding automatically

我有一個簡單的 .png 圖像文件,我希望在加載 JavaFX 應用程序時顯示它。

我使用 NetBeans 作為我的 IDE,我知道可以像這樣添加啟動畫面:項目屬性 -> 運行 -> VM 選項:-splash:path-to-image

現在啟動畫面很好地啟動,但在我的應用程序啟動后它不會關閉。 只是坐在屏幕上,直到我完全關閉我的應用程序。 正如文檔所說( http://docs.oracle.com/javase/7/docs/api/java/awt/SplashScreen.html)“Swing/ AW”。 JavaFX 不是 Swing 或 AWT 應用程序。 那么我該如何關閉它呢?

任何幫助表示贊賞!

好的,回答我自己的問題。

當在 VM 選項中設置飛濺時:-splash:path-to-image。 在 JavaFX 我能夠像這樣關閉它:

//Get the splashscreen
final SplashScreen splash = SplashScreen.getSplashScreen();

//Close splashscreen
    if (splash != null) {
        System.out.println("Closing splashscreen...");
        splash.close();
    }

希望這對其他人也有所幫助! ;)

在 AdoptOpenJDK 14 上,調用SplashScreen.getSplashScreen()會拋出HeadlessException

為了解決這個問題,我做了:

    System.setProperty("java.awt.headless", "false");
    Optional.ofNullable(SplashScreen.getSplashScreen()).ifPresent(SplashScreen::close);
    System.setProperty("java.awt.headless", "true");

雖然接受的答案是正確的,但仍有改進的余地。 您不應該使用final關鍵字,因為這會使啟動圖像掛起。 此外,您可以使用內置的isVisible()方法來檢查splash是否可見,而不是使用null

import java.awt.*;

public class MainApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        SplashScreen splash = SplashScreen.getSplashScreen();

        if (splash.isVisible()) {
            System.out.println("Is visible");

            splash.close();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM