java - 使用 SplashScreen.setImageURL(link) 更改 SplashScreen 图像;

标签 java url netbeans splash-screen

我在 Oracle 文档中找到了一个关于 SplashScreen 的示例。问题是在此示例中,此处使用的图像链接在命令行中作为参数传递。 我正在尝试更改代码,以便将链接写在里面,我不需要使用命令行。

方法 setImageURL(URL imageURL) 应该能够为我完成这项工作,但它不接受我的论点(参数)。

我读到有关 URL 类的内容,似乎它需要协议(protocol)!像 http 和 ftp 这样的协议(protocol)?如果是这样,我的计算机中的文件的 url 应该如何设置?当我尝试从我的计算机(例如:"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

最佳答案

初始屏幕可以在应用程序启动时显示,在 Java 虚拟机 (JVM) 启动之前。

一旦 Swing/AWT 显示第一个窗口,启动画面窗口就会自动关闭(也可以使用 Java API 手动关闭,见下文)。

如果你的应用程序被打包在一个jar文件中,你可以使用

list 文件中的

SplashScreen-Image选项,用于显示启动画面。
将镜像放入jar包中,并在选项中指定路径。
路径不应有前导斜线。 例如,在 manifest.mf 文件中:

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

SplashScreen 类提供了用于控制启动画面的 API。 这个类可以用来

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

它不能用于创建启动画面。

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

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

getSplashScreen 方法返回 null。

那么你的代码有什么问题吗?

SplashScreen.setImageURL(link);

好的

splash.setImageURL(link);

错误的顺序:在你有启动对象之前设置 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:必须被捕获

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;
       }

识别本 map 片文件和网上图片文件的区别。我在本地制作了一个蓝色的splash.gif文件。

过程如下。

  • 本 map 片已加载。 ( list 文件中的 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

关于java - 使用 SplashScreen.setImageURL(link) 更改 SplashScreen 图像;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23273267/

相关文章:

swift - 在 Swift 中获取远程文件顶部

netbeans - 脚本在不同系统上有不同的行为

git - Netbeans:加载现有的克隆 GIT 存储库

java - Eclipse中Cplex Java库设置路径问题

java - Android - MainActivity 扩展类的 onCreate() 未调用

php - 如何从此数组获取youtube下载链接

python - URL 文件夹系统 Django Python

java - 如何在netbeans平台中制作 "Open File..."窗口?

java - 查找具有共同值的 map 集条目

java - 做UDF时Pig报错1070