问题
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.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
回答1:
Take a look at How to Create a Splash Screen
You either need supply the image via the command line or the Jar manifest
Update with some basics
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
1. From the command line
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).
2. The Manifest file
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 ;))...
Check out Working with Manifest Files for more details.
How you create it depends on your environment.
For example, in Netbeans, you can set the splash screen property in the project properties dialog, under the Application
node.
来源:https://stackoverflow.com/questions/15964323/how-do-i-use-splashscreen-without-throwing-a-nullpointerexception