【问题标题】:Swing - Unable to change default taskbar icon to custom iconSwing - 无法将默认任务栏图标更改为自定义图标
【发布时间】:2014-06-18 14:50:13
【问题描述】:

我遇到了一个小问题,由于某种奇怪的原因,我似乎无法解决。

我很难理解为什么我尝试使用自定义任务栏图标而不是默认的 java 徽标作为我的程序图标不起作用。

基本上,我的程序从包含 JFrame 的 JDesktopPane 开始,单击 JFrame 上的按钮,JFrame 调用 JInternalFrame。

从我下面的代码中,您会注意到这是我尝试设置任务栏图标的方式:

        java.net.URL resource = getClass().getClassLoader().getResource("systrayicon.jpg");
    Image image = Toolkit.getDefaultToolkit().getImage(resource);
    TestJDesktopPaneFrame.setIconImage(image);

您会注意到,通过使用 getClass().getClassLoader().getResource("systrayicon.jpg") 我正在访问我打算从以下路径文件中用作任务栏图标的 imahge:/C:/Users /WorkPC/Documents/TEST/bin/systrayicon.jpg

我已经无数次使用 getClass().getClassLoader().getResource("") 并且它对我来说效果很好,例如在我用来调用 JInternalFrame 的按钮中,我使用 getClass().getClassLoader().getResource("") 来访问按钮的自定义图像。

下面是我的完整代码:

   package bge.applcs.dsa;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.*;

public class TestJDesktopPane extends JDesktopPane {

    public static TestJDesktopPane TestJDesktopPane;
    public static JFrame TestJDesktopPaneFrame = new JFrame("");
    public JButton btnJIFrame;

    public TestJDesktopPane() throws IOException {
        createPanel();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                showFrame();
            }
        });
    }

    public static void showFrame() {

        try {
            TestJDesktopPane = new TestJDesktopPane();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        TestJDesktopPaneFrame.setContentPane(TestJDesktopPane);

        TestJDesktopPaneFrame.setUndecorated(true);

        MoveMouseListener mml = new MoveMouseListener(TestJDesktopPane);
        TestJDesktopPane.addMouseListener(mml);
        TestJDesktopPane.addMouseMotionListener(mml);

        TestJDesktopPaneFrame.pack();

        TestJDesktopPaneFrame.setVisible(true);

        TestJDesktopPaneFrame.setResizable(false);

        TestJDesktopPaneFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        TestJDesktopPaneFrame.setLocationRelativeTo(null);

        TestJDesktopPaneFrame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 
                "Cancel");
        TestJDesktopPaneFrame.getRootPane().getActionMap().put("Cancel", new AbstractAction(){
            public void actionPerformed(ActionEvent x) {
                System.exit(0);                
            }
        });

    }

    public void createPanel() {

        setLayout(null);

        setPreferredSize(new Dimension(1000, 300));

        java.net.URL resource = getClass().getClassLoader().getResource("systrayicon.jpg");
        Image image = Toolkit.getDefaultToolkit().getImage(resource);
        TestJDesktopPaneFrame.setIconImage(image);     

        // ****************************** Minimize button ******************************
        // Prepare images
        ImageIcon btnJIFrameNonRollover = new ImageIcon((getClass().getClassLoader().getResource("btnjiframe.jpg")));
        ImageIcon btnJIFrameRollover = new ImageIcon((getClass().getClassLoader().getResource("btnjiframerollover.jpg")));

        // Create custom button
        btnJIFrame = new JButton(btnJIFrameNonRollover);
        btnJIFrame.setBorder(null);
        btnJIFrame.setContentAreaFilled(false);
        btnJIFrame.setBorderPainted(false);
        btnJIFrame.setFocusPainted(false);
        btnJIFrame.setBounds(100,100,100,100);
        btnJIFrame.setRolloverIcon(btnJIFrameRollover);
        btnJIFrame.setCursor(new Cursor(Cursor.HAND_CURSOR));
        btnJIFrame.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JInternalFrame logInJIFrame = new JInternalFrame();

                add(logInJIFrame);              
            }
        });

        //
        add(btnJIFrame);
    }

  }

编辑:

我尝试了以下操作,但出现错误:

    // 
java.net.URL resource = getClass().getClassLoader().getResource("/images/systrayicon.jpg");
Image image = Toolkit.getDefaultToolkit().getImage(resource);
signInDesktopPaneFrame.setIconImage(image);

这是错误:

    Uncaught error fetching image:
java.lang.NullPointerException
    at sun.awt.image.URLImageSource.getConnection(Unknown Source)
    at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
    at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
    at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
    at sun.awt.image.ImageFetcher.run(Unknown Source)

感谢您的任何建议。

【问题讨论】:

  • 将你的图片移动到你的源文件夹中就可以了

标签: java swing icons


【解决方案1】:

getResource 方法使类加载器在程序的类路径中查看目录和 JAR 文件以查找图像,因此它应该在您的 jar 或类路径中的其他 jar 之一中

http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource

我建议你在 src 文件夹中为图片创建文件夹,然后将图片移到那里

然后您可以指定文件夹名称/图像名称来访问它:

ImageIcon icon = ImageIcon(this.getClass().getResource("/images/filename.png"));

或者你可以使用:

ImageIcon icon = ImageIcon(this.getClass().getClassLoader().getResource("images/filename.png"));

它们都将从类路径的根目录开始

【讨论】:

  • 您实际上需要在images 前面加上/,因为搜索将从类位置(即包)开始。 / 将您带到 images 所在的根目录。没有它,搜索将在调用类的包内查找文件夹images。像操作一样使用getClassLoader(),您不需要/,因为类加载器从类路径的根目录开始搜索。这实际上使您对帖子的评论正确:-)
  • 谢谢,我不知道这个,了解问题后立即修复,再次感谢:)
  • @peeskillet:感谢您的回复。如果在将项目转换为 jar 文件然后将 jar 文件转换为 exe 时使用 images 文件夹,这些图像是否可以在 exe 文件中使用?
  • @WajdyEssam:感谢您的回复。我试试看。
  • @TokTok123 我不确定您是否在询问图像是否会内置到 jar 中,是否可以使用 Wajdy 提供的代码访问图像。第一个应该是正确的,因为您的构建没有任何问题。第二个也是正确的,因为图像在src/images 并且您使用第一个代码示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
相关资源
最近更新 更多