如何在不抛出 NullPointerException 的情况下使用 SplashScreen?

编程入门 行业动态 更新时间:2024-10-21 17:33:11
本文介绍了如何在不抛出 NullPointerException 的情况下使用 SplashScreen?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

无论我尝试什么,SplashScreen.getSplashScreen() 都是总是 null.通过在线搜索,我发现这是一个常见问题,并且它与不给 SplashScreen 使用的图像有关...因此,在导航这些方法时,我认为 setImageURL(URL).这仍然不起作用.SO上有类似的问题,例如this,这些问题没有帮助,似乎建议使用无数的插件或从头开始创建这样一个从 Frame 扩展的类.甚至 Oracle 教程 也很神秘,没有概述正确使用 SplashScreen 的每个逻辑步骤...如果使用 SplashScreen 不可能或不必要地困难,是否有其他替代方法?或者有没有人破解了一个简单的方法来解决这个问题?这是我的尝试:

No matter what I try, SplashScreen.getSplashScreen() is always null. From searching online I see that this is a common issue, and that it has something to do with not giving the SplashScreen an image to use... Therefore, in navigating the methods it seemed to me that setImageURL(URL) should be used. This is still not working. There are similar questions on SO, such as this, which are not helpful and seem to suggest using a myriad of plugins or creating such a class from scratch extending from Frame. Even the Oracle tutorial is cryptic, and doesn't outline each logical step in using SplashScreen correctly... If it is not possible or needlessly difficult to use SplashScreen, is there any alternative to doing this? Or has anyone hacked out a simple approach to this problem?


Here is my attempt:

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.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("upload.wikimedia/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

推荐答案

看看 如何创建启动画面

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

You either need supply the image via the command line or the Jar manifest

更新一些基础知识

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

You must supply an image for the splash screen to. If you don't, it will always be null.

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

There are two ways to do this

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

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

The image must reside within the context of the class loader for the application (such as an embedded resource).

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

This is a little more difficult, but will result in a much simpler execution (as no one needs to remember to add the command line ;))...

查看使用清单文件了解更多详情.

创建方式取决于您的环境.

How you create it depends on your environment.

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

For example, in Netbeans, you can set the splash screen property in the project properties dialog, under the Application node.

更多推荐

如何在不抛出 NullPointerException 的情况下使用 SplashScreen?

本文发布于:2023-11-02 14:40:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抛出   情况下   如何在   NullPointerException   SplashScreen

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!