java - jmenu 中的图像来自 java 中的 jar 文件

标签 java swing jar awt jmenu

亲爱的 Stackoverflow 开发人员,我必须将图像作为图标从 jar 加载到 jmenu。 我正在使用以下代码。它抛出空指针异常,但是当我在 jframe 中使用 imageicon 类设置图标时,它在框架中显示图标

   try
   {

    JMenu menu= new JMenu("");
    Icon im1= new ImageIcon(getClass.getResource("path of the images");

   menu.setIcon(im1);
  }
  catch(Exception ex)
   {

   }

这段代码有什么问题请帮忙

最佳答案

  • 常见问题是 ImageIcon , Icon不返回任何异常,测试 Null value

  • Icon im1= new ImageIcon why??? ,这些是不同的Java Object ,必须测试是否 Iconinstanceof ImageIcon

  • 阅读有关 Icon 的教程和 JMenu / JMenuItem

  • 例如

enter image description here

代码

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;

public class MenuExample extends JPanel {

    private static final long serialVersionUID = 1L;
    private JTextPane pane;
    private JMenuBar menuBar;

    public MenuExample() {
        menuBar = new JMenuBar();
        JMenu formatMenu = new JMenu("Justify");
        formatMenu.setMnemonic('J');
        formatMenu.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
        MenuAction leftJustifyAction = new MenuAction("Left", UIManager.getIcon("OptionPane.errorIcon"));
        MenuAction rightJustifyAction = new MenuAction("Right", UIManager.getIcon("OptionPane.informationIcon"));
        MenuAction centerJustifyAction = new MenuAction("Center", UIManager.getIcon("OptionPane.warningIcon"));
        MenuAction fullJustifyAction = new MenuAction("Full", UIManager.getIcon("OptionPane.questionIcon"));
        JMenuItem item;
        item = formatMenu.add(leftJustifyAction);
        item.setMnemonic('L');
        item = formatMenu.add(rightJustifyAction);
        item.setMnemonic('R');
        item = formatMenu.add(centerJustifyAction);
        item.setMnemonic('C');
        item = formatMenu.add(fullJustifyAction);
        item.setMnemonic('F');
        menuBar.add(formatMenu);
        menuBar.add(createMenu("Menu 1"));
        menuBar.add(createMenu("Menu 2"));
        menuBar.add(createMenu("Menu 3"));
        menuBar.add(Box.createHorizontalGlue());
        menuBar.add(new JSeparator());
        menuBar.add(new JButton("   Seach ....  "));
        menuBar.add(new JLabel());
        menuBar.add(new JTextField("   Seach ....  "));
        menuBar.add(new JComboBox(new Object[]{"height", "length", "volume"}));
        menuBar.add(createMenu("About"));
        menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));

    }

    private JMenu createMenu(String title) {
        JMenu m = new JMenu(title);
        m.add("Menu item #1 in " + title);
        m.add("Menu item #2 in " + title);
        m.add("Menu item #3 in " + title);
        if (title.equals("About")) {
            m.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }
        return m;
    }

    class MenuAction extends AbstractAction {

        public MenuAction(String text, Icon icon) {
            super(text, icon);
        }

        public void actionPerformed(ActionEvent e) {
            try {
                pane.getStyledDocument().insertString(0,
                        "Action [" + e.getActionCommand() + "] performed!\n",
                        null);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String s[]) {
        MenuExample example = new MenuExample();
        example.pane = new JTextPane();
        example.pane.setPreferredSize(new Dimension(250, 250));
        example.pane.setBorder(new BevelBorder(BevelBorder.LOWERED));
        JFrame frame = new JFrame("Menu Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setJMenuBar(example.menuBar);
        frame.getContentPane().add(example.pane, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
}

关于java - jmenu 中的图像来自 java 中的 jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12929408/

上一篇:Java:导入父级包

下一篇:java - 安卓按钮:

相关文章:

java - 考虑重复,在数组中存储随机值

java - TableCellEditor 带有一个不更新值的对话框

Java JPanel,如何重叠它们(设置背景图像)

java - 如何从不同的表单更新 jtable?

java - 无法从 java 命令行加载 jar 的主类

java - 从 ubuntu 终端执行 Junit 时出错

java - 错误 :No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androideabi

java - ObjectInputStream.read(byte[], int, int) 缺少数据?

java - 具有依赖项的可运行 jar

java - 不同 Java 应用程序导出选项的奇怪行为