栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

正在加载图标资源错误

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

正在加载图标资源错误

new ImageIcon("src/Action-exit-icon.png");

String
一个构造函数
ImageIcon
假定表示一个字符串
File
路径。

该映像显然是应用程序资源,并且在部署时(在Jar中)将成为嵌入式资源。因此,必须

URL
从应用程序的运行时类路径访问它,如下所示:

new ImageIcon(getClass().getResource("/src/Action-exit-icon.png"));

全面检查代码,我得到以下信息:

import java.awt.Color;import javax.swing.*;public class JavaGui148 {    public JComponent getGUI() {        JPanel p = new JPanel();        p.setBackground(Color.GREEN);        return p;    }    public JMenuBar getMenuBar() {                JMenuBar menuBar;        JMenu menu;        JMenuItem menuItem;        // MENU BAR         menuBar = new JMenuBar();        menuBar.setVisible(true);        // MENU 1        menu = new JMenu("File");        menuBar.add(menu);        // MENU 1 ITEM        ImageIcon icon = new ImageIcon(getClass().getResource(     "/src/Action-exit-icon.png"));        menuItem = new JMenuItem("Exit Program", icon);        menu.add(menuItem);        return menuBar;    }    public static void main(String[] args) {        Runnable r = new Runnable() { @Override public void run() {     JavaGui148 gui = new JavaGui148();     Jframe f = new Jframe("Demo");     f.setJMenuBar(gui.getMenuBar());     f.add(gui.getGUI());     // Ensures JVM closes after frame(s) closed and     // all non-daemon threads are finished     f.setDefaultCloseOperation(Jframe.DISPOSE_ON_CLOSE);     // See http://stackoverflow.com/a/7143398/418556 for demo.     f.setLocationByPlatform(true);     // ensures the frame is the minimum size it needs to be     // in order display the components within it     f.pack();     // should be done last, to avoid flickering, moving,     // resizing artifacts.     f.setVisible(true); }        };        // Swing GUIs should be created and updated on the EDT        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html        SwingUtilities.invokeLater(r);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508002.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号