使用SplashScreen.setImageURL更改SplashScreen图像(链接); [英] Changing SplashScreen Image with SplashScreen.setImageURL(link);

查看:603
本文介绍了使用SplashScreen.setImageURL更改SplashScreen图像(链接);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



I阅读URL类,似乎需要协议!像http和ftp这样的协议?如果是这样的话,我的网址应该如何用于计算机中的文件?当我尝试从我的电脑上放一个链接时(例如:C:\ plash.gif),它说非法逃避角色



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

 非静态方法setImageURL(URL)无法从静态上下文引用

这里是代码:

  package misc; 

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

公共类SplashDemo extends Frame实现ActionListener {
static void renderSplashFrame(Graphics2D g,int frame){
final String [] comps = {foo,bar, 巴兹};
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());
菜单m1 =新菜单(文件);
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(链接);
final SplashScreen splash = SplashScreen.getSplashScreen();
if(splash == null){
System.out.println(SplashScreen.getSplashScreen()返回null);
返回;
}

Graphics2D g = splash.createGraphics();
if(g == null){
System.out.println(g is null);
返回;
}
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();
}
}

这是输出:

 线程main中的异常java.lang.RuntimeException:无法编译的源代码 - 未报告的异常java.net.MalformedURLException;必须被捕获或声明被抛出
在misc.SplashDemo.main(SplashDemo.java:103)
Java结果:1
BUILD SUCCESSFUL(总时间:4秒)

没有任何反应。



PS:我是一个非常初学Java,我正在使用NetBeans IDE 7.2.1

解决方案

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



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

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



SplashScreen-Image 选项在清单文件中显示启动画面。

将图像放在jar存档中并在选项中指定路径。

路径不应该有前导斜杠。
例如,在manifest.mf文件中:

 清单 - 版本:1.0 
Main-Class :测试
SplashScreen-Image:filename.gif

SplashScreen类提供用于控制的API启动画面。
此类可用于




  • 关闭启动画面

  • 更改启动屏幕图像

  • 获取启动画面原生窗口位置/尺寸

  • 在启动画面中绘画。



它无法用于创建初始屏幕。




  • 此类无法实例化。

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



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



getSplashScreen方法返回null。



所以你的错误是什么代码?



NOT

  SplashScreen.setImageURL(link); 

确定

  splash.setImageURL(联系); 






错误的顺序:在发生冲突之前设置ImageUrl对象

  URL link = new URL(http://docs.oracle.com/javase/tutorial/uiswing/examples/杂项/ SplashDemoProject / SRC /杂项/图像/ splash.gif); 
SplashScreen.setImageURL(链接);
final SplashScreen splash = SplashScreen.getSplashScreen();

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



short

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

捕获MalformedURLException 以摆脱错误

MalformedURLException:必须被捕获

  final SplashScreen splash = SplashScreen.getSplashScreen(); 
if(splash == null){
System.out.println(SplashScreen.getSplashScreen()返回null);
返回;
}
网址链接;
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);
返回;
}
试试{
splash.setImageURL(link);
} catch(NullPointerException | IOException | IllegalStateException ex){
System.out.println(NullPointer或IO或IllegalState setImageUrl:85);
返回;
}

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



该程序如下。




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










  • 应用程序出现。








让它在Netbeans中工作



并不总是得到错误 SplashScreen.getSplashScreen()返回null

 最终SplashScreen splash = SplashScreen。那么getSplashScreen(); 
if(splash == null){
System.out.println(SplashScreen.getSplashScreen()返回null);

您必须执行以下操作。



在属性中指向您的本地.gif文件:-splash:src / misc / images / splash.gif




I've found an example in Oracle docs about SplashScreen. the problem is in this example the link of the image used here is passed as argument in the command line. I'm trying to change the code so the link is written inside and I don't need to use the command line.

methode setImageURL(URL imageURL) should be able to do the work for me, but it's not accepting my argument (parameter).

I read about URL class, seems like it needs protocol! protocol like http and ftp ? if that's the case, how should my url be for files in my computer ? when I try to put a link from my computer (ex: "C:\plash.gif") it says illege excape character

I even tried to use http link for an image but it give me this error within the URL line:

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

here's the code:

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

this is the output:

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)

and nothing happens.

P.S: I'm a very beginner with Java, I'm using NetBeans IDE 7.2.1

解决方案

The splash screen can be displayed at application startup, before the Java Virtual Machine (JVM) starts.

The splash screen window is closed automatically as soon as the first window is displayed by Swing/AWT (may be also closed manually using the Java API, see below).

If your application is packaged in a jar file, you can use the

SplashScreen-Image option in a manifest file to show a splash screen.
Place the image in the jar archive and specify the path in the option.
The path should not have a leading slash. For example, in the manifest.mf file:

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

The SplashScreen class provides the API for controlling the splash screen. This class may be used to

  • close the splash screen
  • change the splash screen image
  • get the splash screen native window position/size
  • paint in the splash screen.

It cannot be used to create the splash screen.

  • This class cannot be instantiated.
  • Only a single instance of this class can exist, and it may be obtained by using the getSplashScreen() static method.

In case the splash screen has not been created at application startup via the command line or manifest file option,

the getSplashScreen method returns null.

so what is wrong with your code?

NOT

SplashScreen.setImageURL(link);

OK

splash.setImageURL(link);


wrong sequence : Setting ImageUrl before you have a splash Object

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();

correct : Get splash and then set ImageUrl

short

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

long with catch MalformedURLException to get rid of the error
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;
       }

To recognize the difference between the local image file and the image file on the internet . I have made a local blue splash.gif file.

The proceeding is as follows.

  • Local image is loaded. (SplashScreen-Image option in the manifest file)

  • Application appears.


To get it to work in Netbeans

and not always get the error SplashScreen.getSplashScreen() returned null

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

you must do the following.

in properties point to your local .gif file : -splash:src/misc/images/splash.gif

这篇关于使用SplashScreen.setImageURL更改SplashScreen图像(链接);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆