java - 从计算机URL加载图像

时间:2013-10-29 06:29:35

标签: java swing

这是我的代码,现在,当我加载程序时,它无法解决此错误

错误

java.net.MalformedURLException  at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)  at
 java.net.URL.<init>(Unknown Source)    at
 me.MyTaskbar.com.Image.<init>(Image.java:76)   at
 me.MyTaskbar.com.MyTaskbar.<init>(MyTaskbar.java:170)  at
 me.MyTaskbar.com.MyTaskbar$1.run(MyTaskbar.java:48)    at
 java.awt.event.InvocationEvent.dispatch(Unknown Source)    at
 java.awt.EventQueue.dispatchEventImpl(Unknown Source)  at
 java.awt.EventQueue.access$200(Unknown Source)     at
 java.awt.EventQueue$3.run(Unknown Source)  at
 java.awt.EventQueue$3.run(Unknown Source)  at
 java.security.AccessController.doPrivileged(Native Method)     at
 java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown
 Source)    at java.awt.EventQueue.dispatchEvent(Unknown Source)    at
 java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown
 Source)    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)  at
 java.awt.EventDispatchThread.run(Unknown Source) Caused by:
 java.lang.NullPointerException     ... 20 more

//image 
        URL url;
        try {
            url = new URL(prop.getProperty("database"));
               image1 = new ImageIcon(ImageIO.read(url));
        image1 = new ImageIcon(image1.getImage().getScaledInstance(java.awt.Toolkit.getDefaultToolkit().getScreenSize().width, heightLOCATION, BufferedImage.SCALE_SMOOTH));
        FaceImage = new JLabel(image1);
        FaceImage.setBounds(0,0, java.awt.Toolkit.getDefaultToolkit().getScreenSize().width, heightLOCATION);
        window.add(FaceImage);



        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

private ImageIcon image1;
private JLabel FaceImage;

我正在尝试从名为config.propeties的数据库加载url,该数据库包含一个url图像,例如C://image.jpg 但由于某些原因它不起作用,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

   File file;
    try {
        file= new File(prop.getProperty("database"));
        if(file.exists()) image1 = new ImageIcon(ImageIO.read(file));
     ......