如何从Enum获取ImageIcon?

时间:2014-10-28 17:40:33

标签: java enums imageicon

由于ExceptioninInitializerError,我的Gui课程提取NullPointerException

我的问题是,如何从我的枚举中获取信息(例如Rock的ImageIcon)并将其用于我的JPanel的setIcon 而不会出现此错误。

编辑:当我点击GUI上的Rock按钮时,这是出现的错误

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at rockPaperScissors.RockPaperScissorsGui$2.actionPerformed(RockPaperScissorsGui.java:156)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at rockPaperScissors.RockPaperScissors.<clinit>(RockPaperScissors.java:7)
... 37 more

枚举

package rockPaperScissors;

import javax.swing.Icon;
import javax.swing.ImageIcon;


public enum RockPaperScissors {
    ROCK(new ImageIcon(RockPaperScissors.class.getResource("Rock.jpg"))),
    PAPER(new ImageIcon(RockPaperScissors.class.getResource("Paper.gif"))),
    SCISSORS(new ImageIcon(RockPaperScissors.class.getResource("Scissors.jpg")));

    private ImageIcon icon;
    private int humanScore;
    private int computerScore;


    private RockPaperScissors(ImageIcon icon) {
        setIcon(icon);
    }

    public String evaluate(int humanChoice, int computerChoice ) {
        if ((humanChoice == 1 && computerChoice == 2)
                ||(humanChoice == 2 && computerChoice == 3)
                ||(humanChoice == 3 && computerChoice == 1)) {
            computerScore += 1;
            return "You Lose";
        } else if ((humanChoice == 2 && computerChoice == 1)
                ||(humanChoice == 3 && computerChoice == 2)
                ||(humanChoice == 1 && computerChoice == 3)) {
            humanScore += 1;
            return "You Win";
        } else {
            return "You Tie";
        }
    }

    public int getHumanScore() {
        return humanScore;
    }

    public int getComputerScore() {
            return computerScore;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public ImageIcon getIcon() {
        return icon;
    }
}

package rockPaperScissors;


import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.GridLayout;

import javax.swing.BoxLayout;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.ImageIcon;

import java.awt.Dimension;
import java.awt.Component;

import javax.swing.Box;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Random;
import java.awt.FlowLayout;
import javax.swing.JTextPane;


public class RockPaperScissorsGui extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
private int humanChoice;
private int computerChoice;
private Random rand = new Random();
private ArrayList choices = new ArrayList<>();

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                RockPaperScissorsGui frame = new RockPaperScissorsGui();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public RockPaperScissorsGui() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 801, 525);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    ImageIcon background = new ImageIcon(RockPaperScissorsGui.class.getResource("Background.jpg"));


    choices.add(1);
    choices.add(2);
    choices.add(3);

    JLabel lblRockPaperScissors = new JLabel("Rock, Paper, Scissors");
    lblRockPaperScissors.setPreferredSize(new Dimension(103, 50));
    lblRockPaperScissors.setHorizontalAlignment(SwingConstants.CENTER);
    lblRockPaperScissors.setAlignmentX(Component.CENTER_ALIGNMENT);
    lblRockPaperScissors.setFont(new Font("Comic Sans MS", Font.PLAIN, 22));
    contentPane.add(lblRockPaperScissors, BorderLayout.NORTH);

    JPanel panel = new JPanel();
    contentPane.add(panel, BorderLayout.SOUTH);



    JPanel panel_1 = new JPanel();
    contentPane.add(panel_1, BorderLayout.CENTER);
    panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    Component horizontalStrut_2 = Box.createHorizontalStrut(20);
    horizontalStrut_2.setPreferredSize(new Dimension(110, 0));
    panel_1.add(horizontalStrut_2);

    JLabel userChoiceLabel = new JLabel("");
    userChoiceLabel.setPreferredSize(new Dimension(200, 200));
    panel_1.add(userChoiceLabel);

    Component horizontalStrut = Box.createHorizontalStrut(20);
    horizontalStrut.setPreferredSize(new Dimension(100, 0));
    panel_1.add(horizontalStrut);

    JLabel computerChoiceLabel = new JLabel("");
    computerChoiceLabel.setPreferredSize(new Dimension(200, 200));
    panel_1.add(computerChoiceLabel);

    Component horizontalStrut_1 = Box.createHorizontalStrut(20);
    horizontalStrut_1.setPreferredSize(new Dimension(110, 0));
    panel_1.add(horizontalStrut_1);

    Component horizontalStrut_4 = Box.createHorizontalStrut(20);
    horizontalStrut_4.setPreferredSize(new Dimension(740, 60));
    panel_1.add(horizontalStrut_4);

    JLabel lblUserScore = new JLabel("User Score");
    panel_1.add(lblUserScore);

    JTextPane txtpnUserScore = new JTextPane();
    txtpnUserScore.setText("User Score");
    panel_1.add(txtpnUserScore);

    Component horizontalStrut_3 = Box.createHorizontalStrut(20);
    horizontalStrut_3.setPreferredSize(new Dimension(100, 0));
    panel_1.add(horizontalStrut_3);

    JTextPane textPane = new JTextPane();
    panel_1.add(textPane);

    Component horizontalStrut_5 = Box.createHorizontalStrut(20);
    horizontalStrut_5.setPreferredSize(new Dimension(100, 0));
    panel_1.add(horizontalStrut_5);

    JLabel lblComputerScore = new JLabel("Computer Score");
    panel_1.add(lblComputerScore);

    JTextPane txtpnComputerScore = new JTextPane();
    txtpnComputerScore.setText("Computer Score");
    panel_1.add(txtpnComputerScore);

    JPanel panel_2 = new JPanel();
    contentPane.add(panel_2, BorderLayout.WEST);

    JPanel panel_3 = new JPanel();
    contentPane.add(panel_3, BorderLayout.EAST);

    JButton btnRock = new JButton("Rock");
    btnRock.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            userChoiceLabel.setIcon(RockPaperScissors.ROCK.getIcon());
        }
    });
    btnRock.setFont(new Font("Georgia", Font.PLAIN, 20));
    panel.add(btnRock);

    JButton btnPaper = new JButton("Paper");
    btnPaper.setFont(new Font("Georgia", Font.PLAIN, 20));
    panel.add(btnPaper);

    JButton btnScissors = new JButton("Scissors");
    btnScissors.setFont(new Font("Georgia", Font.PLAIN, 20));
    panel.add(btnScissors);
}

public int getHumanChoice() {
    return humanChoice;
}

public int getComputerChoice() {
    return computerChoice;
}

public void setComputerChoice() {
    computerChoice = rand.nextInt(choices.size());
    System.out.println(computerChoice);
}

1 个答案:

答案 0 :(得分:0)

您的程序崩溃,因为RockPaperScissors.class.getResource("Rock.jpg")无法找到指定的文件。这也适用于您尝试加载的其他图像。当您将程序转换为可运行的JAR时,Java会尝试在项目的根文件夹或与JAR文件相同的目录中查找它们。正如您所指出的,它们与枚举位于同一文件夹中。

我建议在项目的根目录中创建一个名为&#34; res&#34;的新文件夹,并将所有图像文件移动到该文件夹​​中。接下来,替换:

new ImageIcon(RockPaperScissors.class.getResource("Rock.jpg"))

由:

new ImageIcon("res/Rock.jpg")

对于您正在加载图片的每个地方都这样做。

在一个不相关的说明中,由于RockPaperScissors枚举的图标不应该改变,因此更好(并且更容易)使它们公开最终。最终变量只能分配一次,并且必须在构造对象时分配。因此它们充当常数。在代码中:

public final ImageIcon icon;

private RockPaperScissors(ImageIcon icon) {
    this.icon = icon;
}

只需摆脱吸气剂和设定器,您现在可以直接进入图标区域。