My imageIcon is not working?(我的 imageIcon 不工作?)
问题描述
我正在尝试设置一个 JFrame imageIcon,但由于某种原因它没有显示在 JFrame 中.
I am trying to set a JFrame imageIcon and for some reason it is not displaying in the JFrame.
  ImageIcon img  = new ImageIcon("stop.jpg");
  frame.setIconImage(img.getImage());
我创建了一个 ImageIcon 变量,然后将该变量用于 getImage(),但它不起作用.有什么原因它不起作用吗?
I create an ImageIcon variable and then uses that variable to getImage() and it does not work. Is there a reason that it does not work?
问题:为什么 ImageIcon 不起作用?
类:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import java.awt.Toolkit;
public class TestMenu extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel myPanel;
    private static void setLookFeel() {
         try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
         } catch (Exception ex) { }
    }
    public static void main(String[] args) {
        setLookFeel();
        ImageIcon img  = new ImageIcon("stop.jpg");
        TestMenu frame = new TestMenu();
        frame.setIconImage(img.getImage());
        frame.setVisible(true);
    }
    public TestMenu() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu mnNewMenu = new JMenu("TestA");
        menuBar.add(mnNewMenu);
        JMenuItem Item1 = new JMenuItem("TestAA");
        Item1.addActionListener(new MyMenuListener(Item1));
        mnNewMenu.add(Item1);
        JMenu Item2 = new JMenu("TestB");
        menuBar.add(Item2);
        JMenu Item3 = new JMenu("TestBB");
        Item2.add(Item3);
        JMenuItem Item4 = new JMenuItem("TestBB-B");
        Item4.addActionListener(new MyMenuListener(Item4));
        Item3.add(Item4);
        myPanel = new JPanel();
        myPanel.setBorder(new EmptyBorder(25, 25, 25, 25));
        myPanel.setLayout(new BorderLayout(10, 10));
        setContentPane(myPanel);
    }
}
推荐答案
你的图片保存在哪里?您的图片路径可能不正确:
Where is your image saved? It is possible the path to your image is not correct:
ImageIcon img  = new ImageIcon("stop.jpg");
您应该使用基于您的班级的路径.这里例如图片存储在包图片中.
You should use a path that is based on your class. Here for example the image is stored in the package images.
new ImageIcon(MyClass.class.getResource("/images/image.png"));
                        这篇关于我的 imageIcon 不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我的 imageIcon 不工作?
				
        
 
            
        - 如何指定 CORS 的响应标头? 2022-01-01
 - 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
 - java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
 - 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
 - Eclipse 的最佳 XML 编辑器 2022-01-01
 - 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
 - 获取数字的最后一位 2022-01-01
 - 未找到/usr/local/lib 中的库 2022-01-01
 - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
 - 转换 ldap 日期 2022-01-01
 
						
						
						
						
						
				
				
				
				