Java Swing JButton getResource 不能正常工作

发布于 2024-12-18 14:46:54 字数 597 浏览 5 评论 0原文

此代码在 Windows XP 上运行正确。但在家里的 Windows 7 机器上我看不到图标,因为 getResource 返回 null。我已经尝试过绝对路径,但它也不起作用。我不知道该怎么办,我正在管理员模式下运行 Eclipse:

private static JButton createToolButton(String imgName, String altText, String toolTipText) {

    String imagePath = IMG_URL + "/" + imgName;

    URL imageUrl = SwingUtility.class.getResource(imagePath);
    JButton button = new JButton();
    button.setToolTipText(toolTipText);

    if(imageUrl != null) //Image trouvé
        button.setIcon(new ImageIcon(imageUrl, altText));
    else
        button.setText(altText);

    return button;
}

This code runs correctly on windows XP. But at home on my Windows 7 machine I can't see the icons because getResource returns null. I've tried an absolute path and it's not working either. I don't know what to do, I'm running Eclipse in Admin mode:

private static JButton createToolButton(String imgName, String altText, String toolTipText) {

    String imagePath = IMG_URL + "/" + imgName;

    URL imageUrl = SwingUtility.class.getResource(imagePath);
    JButton button = new JButton();
    button.setToolTipText(toolTipText);

    if(imageUrl != null) //Image trouvé
        button.setIcon(new ImageIcon(imageUrl, altText));
    else
        button.setText(altText);

    return button;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琴流音 2024-12-25 14:46:54

getResource() 方法首先会在父类加载器中搜索资源;如果父级为空,则搜索虚拟机内置的类加载器的路径。如果失败,此方法将调用 findResource(String) 来查找资源。因此,在所有这些之后,如果它返回 null,则问题出在 imagepath ergo IMG_URL 上。另请注意,如果找不到资源或调用者没有足够的权限来获取资源,它会返回 null。

The getResource() method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke findResource(String) to find the resource. So after all these, if it returns null, the problem is with imagepath ergo IMG_URL. Also note that it returns null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.

我早已燃尽 2024-12-25 14:46:54

这是我的解决方案,并不理想,但它有效。经过多次测试后,我只是觉得使用点符号向上进入当前目录并不能完全按照使用基本 java 实用程序的预期工作。最终只是获取了项目路径并将其分割到我想要的地方,尚未在其他机器上进行测试。

private static JButton createToolButton(String imgName, String altText, String toolTipText) 
{
    String imagePath = IMG_FOLDER_NAME + "\\" + imgName;
    BufferedImage img = null;
    JButton button = new JButton();

    try 
    {
        img = ImageIO.read(new File(projectPath() + imagePath));
    } 
    catch (IOException e)
    {
        e.printStackTrace();
    }


    button.setToolTipText(toolTipText);

    if(img != null)
       button.setIcon(new ImageIcon(img, altText));
    else
       button.setText(altText);

    return button;
}

private static String projectPath()
{
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL url = classLoader.getResource("");
    String pathArr[] = url.getPath().split(PATH_SEPARATOR);
    return pathArr[0];
}

Here's my solution, not ideal but it works. After many tests I just feel like using the dot notation to go up and into the current directory not work exactly as expected using the basic java utilities. Ended up just getting the project path and splitting it where I wanted, hasn't been tested on other machines yet.

private static JButton createToolButton(String imgName, String altText, String toolTipText) 
{
    String imagePath = IMG_FOLDER_NAME + "\\" + imgName;
    BufferedImage img = null;
    JButton button = new JButton();

    try 
    {
        img = ImageIO.read(new File(projectPath() + imagePath));
    } 
    catch (IOException e)
    {
        e.printStackTrace();
    }


    button.setToolTipText(toolTipText);

    if(img != null)
       button.setIcon(new ImageIcon(img, altText));
    else
       button.setText(altText);

    return button;
}

private static String projectPath()
{
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL url = classLoader.getResource("");
    String pathArr[] = url.getPath().split(PATH_SEPARATOR);
    return pathArr[0];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文