使用SplashScreen.setImageURL更改SplashScreen图像(链接);

时间:2014-04-24 15:26:55

标签: java url netbeans splash-screen

我在Oracle docs中找到了一个关于SplashScreen的例子。问题出在这个例子中,这里使用的图像的链接作为参数在命令行中传递。 我正在尝试更改代码,以便链接写入内部,我不需要使用命令行。

methode setImageURL(URL imageURL)应该可以为我做的工作,但它不接受我的参数(参数)。

我读过有关URL类的内容,好像需要协议!像http和ftp这样的协议?如果是这样的话,我的网址应该如何用于计算机中的文件?当我尝试从我的计算机上添加一个链接时(例如"C:\plash.gif"),它会显示illege excape character

我甚至尝试对图像使用http链接,但它在URL行中给出了这个错误:

non-static method setImageURL(URL) cannot be referenced from a static context

这是代码:

package misc;

import java.awt.*;
import java.awt.event.*;
import java.net.URL;

public class SplashDemo extends Frame implements ActionListener {
    static void renderSplashFrame(Graphics2D g, int frame) {
        final String[] comps = {"foo", "bar", "baz"};
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(120,140,200,40);
        g.setPaintMode();
        g.setColor(Color.BLACK);
        g.drawString("Loading "+comps[(frame/5)%3]+"...", 120, 150);
    }
    public SplashDemo() {
        super("SplashScreen demo");
        setSize(300, 200);
        setLayout(new BorderLayout());
        Menu m1 = new Menu("File");
        MenuItem mi1 = new MenuItem("Exit");
        m1.add(mi1);
        mi1.addActionListener(this);
        this.addWindowListener(closeWindow);

        MenuBar mb = new MenuBar();
        setMenuBar(mb);
        mb.add(m1);
        URL link= new URL("http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/SplashDemoProject/src/misc/images/splash.gif");
        SplashScreen.setImageURL(link);
        final SplashScreen splash = SplashScreen.getSplashScreen();
        if (splash == null) {
            System.out.println("SplashScreen.getSplashScreen() returned null");
            return;
        }

        Graphics2D g = splash.createGraphics();
        if (g == null) {
            System.out.println("g is null");
            return;
        }
        for(int i=0; i<100; i++) {
            renderSplashFrame(g, i);
            splash.update();
            try {
                Thread.sleep(90);
            }
            catch(InterruptedException e) {
            }
        }
        splash.close();
        setVisible(true);
        toFront();
    }
    public void actionPerformed(ActionEvent ae) {
        System.exit(0);
    }

    private static WindowListener closeWindow = new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            e.getWindow().dispose();
        }
    };

    public static void main (String args[]) {
        SplashDemo test = new SplashDemo();
    }
}

这是输出:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.net.MalformedURLException; must be caught or declared to be thrown
    at misc.SplashDemo.main(SplashDemo.java:103)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

没有任何反应。

P.S:我是Java的初学者,我正在使用NetBeans IDE 7.2.1

1 个答案:

答案 0 :(得分:4)

在Java虚拟机(JVM)启动之前,可以在应用程序启动时显示启动画面。

当Swing / AWT显示第一个窗口时,启动屏幕窗口会自动关闭(也可以使用Java API手动关闭,见下文)。

如果您的应用程序打包在jar文件中,则可以使用

清单文件中的

SplashScreen-Image 选项,以显示启动画面。
将图像放在jar存档中并在选项中指定路径 该路径不应该有一个前导斜杠。 例如,在manifest.mf文件中:

 Manifest-Version: 1.0
 Main-Class: Test
 SplashScreen-Image: filename.gif

SplashScreen类提供用于控制启动屏幕的API。 这个类可能用于

  • 关闭启动画面
  • 更改启动画面图像
  • 获取初始屏幕原始窗口位置/大小
  • 在闪屏中画画。

无法用于创建初始屏幕。

  • 此类无法实例化。
  • 只能存在此类的一个实例,并且可以使用getSplashScreen()静态方法获取它。

如果尚未通过命令行或清单文件选项在应用程序启动时创建启动画面

getSplashScreen方法返回null。

你的代码出了什么问题?

不是

SplashScreen.setImageURL(link);

splash.setImageURL(link);

错误的序列:在拥有splash对象之前设置ImageUrl

URL link= new URL("http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/SplashDemoProject/src/misc/images/splash.gif");
        SplashScreen.setImageURL(link);
        final SplashScreen splash = SplashScreen.getSplashScreen();

正确:获取启动然后设置ImageUrl

final SplashScreen splash = SplashScreen.getSplashScreen();
splash.setImageURL(link);

catch MalformedURLException以消除错误
MalformedURLException : must be caught

final SplashScreen splash = SplashScreen.getSplashScreen();
if (splash == null) {
       System.out.println("SplashScreen.getSplashScreen() returned null");
            return;
   }
URL link;
   try {
        link = new URL("http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/SplashDemoProject/src/misc/images/splash.gif");
       } catch (MalformedURLException ex) {
            System.out.println("MalformedURLException link:77");
            return;
       }
   try {
            splash.setImageURL(link);
       } catch (NullPointerException | IOException | IllegalStateException ex) {
            System.out.println("NullPointer or IO or IllegalState setImageUrl:85");
            return;
       }

识别本地图像文件与互联网上的图像文件之间的差异。我制作了一个本地蓝色的splash.gif文件。

程序如下。

  • 加载了本地图片。 (清单文件中的SplashScreen-Image选项)

enter image description here

enter image description here

  • 应用程序出现。

enter image description here


让它在Netbeans

中运行

并不总是收到错误SplashScreen.getSplashScreen() returned null

final SplashScreen splash = SplashScreen.getSplashScreen();
    if (splash == null) {
           System.out.println("SplashScreen.getSplashScreen() returned null");

您必须执行以下操作。

属性中的

指向您当地的.gif文件:-splash:src / misc / images / splash.gif

enter image description here

相关问题