【问题标题】:How do I use SplashScreen without throwing a NullPointerException?如何在不引发 NullPointerException 的情况下使用 SplashScreen?
【发布时间】:2013-04-04 13:34:15
【问题描述】:


无论我尝试什么,SplashScreen.getSplashScreen()总是null

通过在线搜索,我发现这是一个常见问题,并且与不给 SplashScreen 使用图像有关......因此,在导航方法时,我认为应该使用 setImageURL(URL) .这仍然行不通。

SO上有类似的问题,例如this,它们没有帮助,似乎建议使用无数的插件或从Frame扩展从头开始创建这样的类。即使Oracle tutorial 也很神秘,并且没有概述正确使用SplashScreen 的每个逻辑步骤......

如果不可能或不必要地难以使用SplashScreen,是否有其他选择?或者有没有人破解了一个简单的方法来解决这个问题?


这是我的尝试:
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.SplashScreen;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

/**
 */
public final class MainGUI implements ActionListener {

    /**
     * @throws IOException 
     * @throws IllegalStateException 
     * @throws NullPointerException 
     */
    private final static void showSplashScreen() throws NullPointerException, IllegalStateException, IOException {
        final SplashScreen splash = SplashScreen.getSplashScreen();
        Graphics2D graphics = splash.createGraphics();

        // adding image here:
        URL imageSource = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/7/76/Space_Shuttle_Atlantis_approaching_the_Kennedy_Space_Center_to_land_following_STS-122.jpg/800px-Space_Shuttle_Atlantis_approaching_the_Kennedy_Space_Center_to_land_following_STS-122.jpg");
        splash.setImageURL(imageSource);

        // coordinates and dimensions:
        int x = 100, y = x;
        int width = 500, height = width;

        // (x, y), width, height:
        graphics.create(x, y, width, height);

        graphics.setBackground(Color.BLUE);

        // adding and centering the text:
        graphics.drawString("centered text", (x + width)/2, (y + height)/2);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            showSplashScreen();
        } catch (NullPointerException | IllegalStateException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
} // end of MainGUI

【问题讨论】:

    标签: java null nullpointerexception splash-screen


    【解决方案1】:

    看看How to Create a Splash Screen

    您需要通过命令行或 Jar 清单提供图像

    更新一些基础知识

    您必须为启动画面提供图像。如果不这样做,它将始终为空。

    有两种方法可以做到这一点

    1。从命令行

    java -splash:path/to/image.jpg {other command line options}
    

    图像必须位于应用程序的类加载器的上下文中(例如嵌入式资源)。

    2。清单文件

    这有点困难,但会导致执行更简单(因为没有人需要记住添加命令行;))...

    查看Working with Manifest Files了解更多详情。

    如何创建它取决于您的环境。

    例如,在 Netbeans 中,您可以在项目属性对话框中的 Application 节点下设置启动画面属性。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 2013-05-11
    • 1970-01-01
    • 2010-12-30
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多