在Java Swing中在菜单栏上显示图标。

huangapple go评论74阅读模式
英文:

Displaying icon on menu bar in Java Swing

问题

import javax.swing.*;

public class test {

    private JFrame f;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            test app = new test();
            app.createAndShowGUI();
        });
    }

    private void createAndShowGUI() {
        f = new JFrame("Test");
        JMenuBar menubar = new JMenuBar();

        Icon lupeIcon = new ImageIcon(getClass().getResource("/Resources/lupe_icon.png"));
        JButton j_searchButton = new JButton(lupeIcon);
        menubar.add(j_searchButton);

        f.setJMenuBar(menubar);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setSize(500, 500);
        f.setVisible(true);
    }
}
英文:

I have an application with a Swing GUI and I would like to add a search-field with a search-button (lupe icon) to the menu bar. However, the lupe icon won't display. Here is my code:

public class Ui_Frame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Ui_Frame::createAndShowGUI);
    }
    

    private static void createAndShowGUI() {
         f = new JFrame("Myframe"); 

         ...

         JMenuBar menubar = new JMenuBar();
         Icon lupeIcon = new ImageIcon("Resources/lupe_icon.png");
         JButton j_searchButton = new JButton(lupeIcon);
         menubar.add(j_searchButton);

         ...

         f.setJMenuBar(menubar);
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         f.pack();
         f.setVisible(true);

My project structure is like

Project 
       Src
          Ui_Frame.java
          Resources 
                 lupe_icon.png 

The resulting button shows no icon at all :

在Java Swing中在菜单栏上显示图标。

I do not get any error message, it compiles without problems. Even when I try to catch any exception from the new ImageIcon(...) I'm not getting any hint at what the error is.

Any ideas as to what Im doing wrong here?

EDIT:

Here is a minimal example:

import javax.swing.*;

public class test {

    private static JFrame f;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(test::createAndShowGUI);
    }

    private static void createAndShowGUI() {

        f = new JFrame("Test");
        JMenuBar menubar = new JMenuBar();

        Icon lupeIcon = new ImageIcon(test.class.getResource("/Resources/lupe_icon.png"));
        JButton j_searchButton = new JButton(lupeIcon);
        menubar.add(j_searchButton);

        f.setJMenuBar(menubar);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setSize(500,500);
        f.setVisible(true);
    }
}

As you can see I am now loading the image with class.getResource(...) as was suggested by @AndrewThompson and @SergiyMedvynskyy, but that does not solve the problem. Also I was told that my classes should not be static, but since my Main needs to be static for me to be able to run the program and I have been told that I should start the UI with SwingUtilities.invokeLater(test::createAndShowGUI);
which also forces createAndShowGUI() to be static, I do not know how to make it not static.

答案1

得分: 3

对于我来说,它可以在路径中不提及文件夹“Resources”而正常工作,就像这样:

Icon lupeIcon = new ImageIcon(test.class.getResource("/lupe_icon.png"));

但是,我不确定你的项目结构是否正确。

如果仍然不起作用,尝试将文件夹重命名为小写的“r”,或者可能还要更改项目结构,这是我的结构:

> \src\main\java\...(所有Java文件)
> 
> \src\main\resources(资源文件夹,对我来说显示为一个包)

注意 - 我认为最好检查一下为什么在调试中无法获取图像。

英文:

For me it's working without mention folder "Resources" in the path,
like that:

Icon lupeIcon = new ImageIcon(test.class.getResource("/lupe_icon.png"));

But, i'm not sure your project structure is correct.

If still doesn't work, try rename folder with small 'r', and maybe also change project structure, this is my structure:

> \src\main\java... (all java files)
>
> \src\main\resources (resources folder, it displayed for me like a package)

Note - i think best is to check why u can't get the image in debug

答案2

得分: 0

在切换到使用Icon lupeIcon = new ImageIcon(test.class.getResource("Resources/lupe_icon.png"));来加载图标,而不是使用new ImageIcon("Resources/lupe_icon.png");之后,我注意到尽管图像明显存在,但仍然出现了空指针异常。经过一些搜索,我发现资源文件夹显然不仅仅是普通的文件夹,而是必须被标记为资源根文件夹,正如这里所建议的那样。

在标记了这个文件夹之后,一切都正常工作了 在Java Swing中在菜单栏上显示图标。

英文:

After switching to Icon lupeIcon = new ImageIcon(test.class.getResource("Resources/lupe_icon.png")); to load the icon instead of new ImageIcon("Resources/lupe_icon.png"); I noticed that I was getting nullpointer-exceptions, despite the image clearly being there. After some googling I found out that the resource folder apparently is not just any folder but has to be marked as the resource root folder, as suggested here.

After marking the folder everything works fine 在Java Swing中在菜单栏上显示图标。

huangapple
  • 本文由 发表于 2020年9月1日 15:21:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63683112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定