如何在java中获取图像的真实路径

时间:2015-05-21 10:54:24

标签: java image swing awt embedded-resource

private static void setToSystemTray(){
    //checking for support
    if (!SystemTray.isSupported())
    {
        System.out.println("System tray is not supported !!! ");
        return;
    }
    //get the systemTray of the system
    SystemTray systemTray = SystemTray.getSystemTray();



     //get default toolkit
    //Toolkit toolkit = Toolkit.getDefaultToolkit();
    //get image 
    //Toolkit.getDefaultToolkit().getImage("src/resources/busylogo.jpg");
    Image image = Toolkit.getDefaultToolkit().getImage("./src/com/xxx/xxx/icons/Cloud.png");

    //popupmenu
    PopupMenu trayPopupMenu = new PopupMenu();

    //1t menuitem for popupmenu
    MenuItem action = new MenuItem("Action");
    action.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            JOptionPane.showMessageDialog(null, "Action Clicked");
        }
    });
    trayPopupMenu.add(action);

    //2nd menuitem of popupmenu
    MenuItem close = new MenuItem("Close");
    close.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    });
    trayPopupMenu.add(close);

    //setting tray icon
    TrayIcon trayIcon = new TrayIcon(image, "Backup System", trayPopupMenu);
    //adjust to default size as per system recommendation 
    trayIcon.setImageAutoSize(true);

    try
    {
        systemTray.add(trayIcon);
    } catch (AWTException awtException)
    {
        awtException.printStackTrace();
    }
    System.out.println("end of main");
}

这是我用来获取图片的代码。当我在net beans中运行java项目时,这是有效的。但是当我执行项目的.jar文件时,这不起作用。

这是我用来获取图像的方法。

我在这段代码中犯了什么错误?

0 个答案:

没有答案