如何从枚举中获取ImageIcon? [英] How to get ImageIcon from Enum?

查看:88
本文介绍了如何从枚举中获取ImageIcon?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Gui班级由于NullPointerException而拉起ExceptioninInitializerError.

My Gui class pulls up an ExceptioninInitializerError due to NullPointerException.

我的问题是,如何从枚举中获取信息(例如Rock的ImageIcon),并使用它来设置JPanel的setIcon ,而不会出现此错误.

My question is, how can I get information from my enum (such as the ImageIcon of Rock) and use it to setIcon of my JPanel without getting this error.

当我单击GUI上的Rock按钮时,这是出现的错误

When I click the Rock Button on the GUI, this is the error that is brought up

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;
    }
}


Gui


Gui

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);
}

推荐答案

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

Your program crashes because RockPaperScissors.class.getResource("Rock.jpg") fails to find the file specified. This also counts for the other images you're attempting to load. Java tries to look for them in the root folder of the project or the same directory as the JAR file when you turn your program into a runnable JAR. As you pointed out, they are in the same folder as the enum.

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

I'd recommend creating a new folder in the root of your project, called "res", and moving all your image files into that folder. Next, replace:

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

作者:

new ImageIcon("res/Rock.jpg")

然后在要加载图像的每个位置执行该操作.

And do that for every place where you're loading images.

不相关的注释是,由于RockPaperScissors枚举的图标不应该更改,因此最好(也更容易)将它们公开决赛.最终变量只能分配一次,并且在构造对象时必须分配.因此,它们充当常量.在代码中:

On an unrelated note, since the icons of the RockPaperScissors enum are not supposed to change, it's better (and easier) to make them public final. Final variables can only be assigned once, and have to be assigned when the object is constructed. They thus act as constants. In code:

public final ImageIcon icon;

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

只需摆脱getter和setter,您现在就可以直接进入图标字段.

And just get rid of the getter and setter, you can now get to the icon field directly.

这篇关于如何从枚举中获取ImageIcon?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆