从signed.jar访问图像

时间:2015-08-03 14:59:13

标签: java url system-tray

是否有快速方法从signed.jar中获取图像文件的内容?我有一个Java文件,在SystemTray中创建一个需要图像的TrayIcon。我的Java类在测试时工作正常,但是当我在我的软件中使用它时会出现问题,而且我的URL对象似乎就是问题所在。以前我使用TrayIconD.class.getResource(path);但它看起来不像我的signed.jar文件,因为它们有不同的文件名。有没有办法我仍然可以使用上述方法或使用getContent()的URL对象?这些方法更为虚弱的是

import java.awt.TrayIcon;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.AWTException;
import javax.swing.ImageIcon;
import java.net.URL;

public class TrayIconD {
    public static void main(String[] args) {
        createAndShowGUI();
    }

    private static void createAndShowGUI() {

        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }

        final SystemTray tray = SystemTray.getSystemTray();
        final TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif", "tray icon"));

        try {
          tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("TrayIcon could not be added.");
            return;
        }
            trayIcon.displayMessage("Sun TrayIcon Demo", "This is an info message", TrayIcon.MessageType.INFO);  
        }

    protected static Image createImage(String path, String description) {
       //URL imageURL = TrayIconD.class.getResource(path);

       URL imageURL = null;
       Object image = null;

        try{ 
        imageURL = new URL("/C:/Users/Documents/notifications/images/bulb.gif");

       // System.out.println(imageURL);
       // image = imageURL.getContent();
         }
         catch(Exception ep){
         System.out.println(ep.getMessage());
         }

        if (imageURL == null) {
            System.err.println("1111Resource not found: " + path);
            return null;
        } else {
            return (new ImageIcon(imageURL, description)).getImage();
        }
    }
} 

0 个答案:

没有答案