0


无论我尝试什么SplashScreen.getSplashScreen()总是。 通过在线搜索,我发现这是一个常见问题,并且与不提供要使用的图像有关......因此,在导航我认为应该使用的方法时。这仍然行不通。 SO上有类似的问题,例如this,这些问题没有帮助,似乎建议使用无数插件或从头开始创建这样的类,扩展自. 即使是Oracle 教程也很神秘,并没有概述正确使用的每个逻辑步骤…… 如果不可能或不必要地难以使用 null

SplashScreensetImageURL(URL)

FrameSplashScreen

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
4

1 回答 1

1

看看如何创建启动画面

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

更新一些基础知识

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

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

1.从命令行

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

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

2. 清单文件

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

查看使用清单文件了解更多详细信息。

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

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

于 2013-04-12T06:14:18.583 回答