当我导出为Runnable JAR文件时,如何显示ImageIcon?

时间:2014-12-24 14:59:25

标签: java url jar runnable imageicon

好。我是初学者。我已经看过关于互联网的多个问题,但我无法解决这个问题。我尝试使用URL来显示图像路径,但没有任何效果。虽然,当我使用ImageIcons来显示路径时,图片并没有显示出来;但是当我使用URL时,JAR根本没有打开。然后当我在命令提示符下运行它时,我得到了这个 -

Exception in thread "main" java.lang.NullPointerException
     at javax.swing.ImageIcon.<init>(Unknown Source)
     at Package.ListenerClass.<init>(ListenerClass.java:47)
     at Package.Class.main(Class.java:17)

我从中得到的是文件&#34;不存在&#34;。但是当我在eclipse中编译它时,它工作正常。这是我的司机班:

package Package;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Class {

@SuppressWarnings("unused")
public static void main(String args[]) {

        JFrame frame = new JFrame("Click If You Dare");
        ImageIcon background = new ImageIcon();
        JLabel label = new JLabel(background);

        ListenerClass panel = new ListenerClass();

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        frame.pack();
        frame.setSize(425, 425);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setContentPane(panel);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            }

}

我的听众课程:

package Package;

import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class ListenerClass extends JPanel implements MouseListener  {

ImageIcon buttonOriginal;
ImageIcon buttonClick;
ImageIcon button100;
ImageIcon button100Click;
ImageIcon button200;
ImageIcon button200Click;

JButton button;
JButton credits;
JButton reset;
JButton cheats;

JLabel secondLabel;

int clicks = 0;

@SuppressWarnings("static-access")
ListenerClass() {

    buttonOriginal = new ImageIcon(getClass().getResource("/button.png"));
    buttonClick = new ImageIcon(getClass().getResource("/buttonClick.png"));
    button100 = new ImageIcon(getClass().getResource("/button100.png"));
    button100Click = new ImageIcon(getClass().getResource("/button100Click.png"));
    button200 = new ImageIcon(getClass().getResource("/button200.png"));
    button200Click = new ImageIcon(getClass().getResource("/button200Click.png"));

    button = new JButton(buttonOriginal);
    button.setAlignmentX(button.CENTER_ALIGNMENT);

    button.setOpaque(false);
    button.setContentAreaFilled(false);
    button.setBorderPainted(false);
    button.setFocusPainted(false);

    credits = new JButton("Credits");
    credits.setAlignmentX(credits.CENTER_ALIGNMENT);

    reset = new JButton("Reset Score");
    reset.setAlignmentX(reset.CENTER_ALIGNMENT);

    secondLabel = new JLabel("" + clicks);
    secondLabel.setAlignmentX(secondLabel.CENTER_ALIGNMENT);
    secondLabel.setFont(new Font("DS-Digital", Font.PLAIN, 85) );

    button.addMouseListener(new MouseListener() {

        @Override
           public void mouseClicked(MouseEvent e) {
            clicks += 1;
            secondLabel.setText("" + clicks);

            if(clicks == 100) {
            button.setIcon(button100);
            }
            else if(clicks == 200) {
                button.setIcon(button200);
                }
           }

        @Override
        public void mouseEntered(MouseEvent arg0) {}
        @Override
        public void mouseExited(MouseEvent arg0) {}
        @Override
        public void mousePressed(MouseEvent arg0) {
            if(clicks <=99) {
                button.setIcon(buttonClick);
                }
                else if(clicks >= 100 && clicks <= 199) {
                button.setIcon(button100Click);
                }
                else if(clicks >= 200 && clicks <= 299) {
                    button.setIcon(button200Click);
                }
        }
        @Override
        public void mouseReleased(MouseEvent arg0) {
            if(clicks <=99) {
                button.setIcon(buttonOriginal);
                }
                else if(clicks >= 100 && clicks <= 199) {
                button.setIcon(button100);
                }
                else if(clicks >= 200 && clicks <= 299) {
                    button.setIcon(button200);
                }
        }

    });
    credits.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
             JOptionPane.showMessageDialog(button, "Made by: Jonah Bobbitt and Wil Carpenter",
                                           null, JOptionPane.PLAIN_MESSAGE);
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {}
        @Override
        public void mouseExited(MouseEvent arg0) {}
        @Override
        public void mousePressed(MouseEvent arg0) {}
        @Override
        public void mouseReleased(MouseEvent arg0) {}

    });
    reset.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            clicks = 0;
            secondLabel.setText("" + clicks);
            button.setIcon(buttonOriginal);
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {}
        @Override
        public void mouseExited(MouseEvent arg0) {}
        @Override
        public void mousePressed(MouseEvent arg0) {}
        @Override
        public void mouseReleased(MouseEvent arg0) {}

    });

    this.add(secondLabel);
    this.add(button);
    this.add(credits);
    this.add(Box.createVerticalStrut(10));
    this.add(reset);

    }
@Override
public void mouseClicked(MouseEvent arg0) {}
@Override
public void mouseEntered(MouseEvent arg0) {}
@Override
public void mouseExited(MouseEvent arg0) {}
@Override
public void mousePressed(MouseEvent arg0) {}
@Override
public void mouseReleased(MouseEvent arg0) {}
}

帮助?

编辑:现在当我使用URL时,它在eclipse中编译时不再有效,但它显示了同样的错误。此外,图片位于构建路径中的文件夹中。

再次编辑......它现在正在运作。请参阅peeskillet的评论。

1 个答案:

答案 0 :(得分:0)

编辑: 如果图像在您的包中,您可以仅使用文件名加载资源(不带前导斜杠)

buttonOriginal = new ImageIcon(getClass().getResource("button.png"));

虽然最好将资源分开到单独的文件夹中。