用Java运行一个实例,总是抛出空指针异常,究竟问题出在那,请指教,实例如下:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.SplashScreen;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
public class SplashScreenTest {
private static void inital() {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
Toolkit toolkit = Toolkit.getDefaultToolkit();
final Image image = toolkit.getImage(splashScreen.getImageURL());
final JFrame splashFrame = new JFrame();
splashFrame.setUndecorated(true);
final JPanel splashPanel = new JPanel() {
/**
*
*/
private static final long serialVersionUID = -1289515968041228683L;
@Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
};
final JProgressBar progressBar = new JProgressBar();
progressBar.setStringPainted(true);
splashPanel.setLayout(new BorderLayout());
splashPanel.add(progressBar, BorderLayout.SOUTH);
splashFrame.add(splashPanel);
splashFrame.setSize(splashScreen.getSize().width, splashScreen.getSize().height + 20);
Dimension splashSize = new Dimension();
splashSize.width = splashScreen.getSize().width;
splashSize.height = splashScreen.getSize().height + 20;
Dimension screenSize = toolkit.getScreenSize();
splashFrame.setLocation((screenSize.width - splashSize.width) / 2, (screenSize.height - splashSize.height) / 2);
splashFrame.setVisible(true);
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
progressBar.setString("已经加载:" + i + "%");
progressBar.setValue(i);
splashPanel.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
splashFrame.setVisible(false);
}
}).start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
inital();
}
});
}
}
控制台出现z空指针异常情况如下:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.mingrisoft.splashscreen.SplashScreenTest.inital(SplashScreenTest.java:20)
at com.mingrisoft.splashscreen.SplashScreenTest.access$0(SplashScreenTest.java:17)
at com.mingrisoft.splashscreen.SplashScreenTest$3.run(SplashScreenTest.java:73)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
SplashScreenTest.inital(SplashScreenTest.java:20) 这个第20行是哪个函数,估计没有实例化出来
第二十行函数是:SplashScreen splashScreen = SplashScreen.getSplashScreen();就是不知道该如何做,还请指教,谢谢啦
@LIEBER_JIANG: http://stackoverflow.com/questions/15964323/how-do-i-use-splashscreen-without-throwing-a-nullpointerexception