我的课已经分配了一个石头,纸,剪刀的游戏。这是作业说明:

学习目标 :


练习使用枚举
创建一个包含构造函数,字段和方法的枚举
设计和实现自己的GUI
创建一个可运行的jar


描述:
编写程序在计算机上播放剪刀石头布

要求:


创建一个代表三个选项的枚举:石头,纸张和
剪刀。


包含ImageIcon类型的字段(可选地,更多字段)
包括一个构造函数
包括评估方法

创建一个可在计算机上播放Rock Paper Scissors的gui应用程序


允许用户选择三个选项之一。
用户做出选择后,计算机将随机选择
石头,纸或剪刀
对于这两个选择,将显示相应的图像
另外显示结果(谁击败了谁)
用户需要能够多次播放而无需重新启动
应用程序
创建一个包含源代码的可运行jar(4分)



*编辑

好的,我正处于困境中,大多数情况下真正需要的全部是validate()方法。关于如何去做有什么想法吗?在过去的一个小时里,我一直在动脑筋,但我还不太清楚。无论如何,这是更新的代码:

import javax.swing.ImageIcon;

public enum RPSChoice {
    ROCK(new ImageIcon(RPSChoice.class.getResource("rock.png"))),
    PAPER(new ImageIcon(RPSChoice.class.getResource("paper.png"))),
    SCISSORS(new ImageIcon(RPSChoice.class.getResource("scissors.png")));

        private ImageIcon imgChoice;

        private RPSChoice(ImageIcon imgChoice) {
            this.imgChoice = imgChoice;
        }

        public ImageIcon getImageIcon(){
            return imgChoice;
        }

    public static void evaluate(){
        //TODO
    }

    public static RPSChoice randomChoice(){
        return values()[(int) (Math.random() * values().length)];
    }
}


和gui

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

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

    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;

    public class RockPaperScissorsGUI extends JFrame {

        private JPanel contentPane;
        private JPanel humanDisplay = new JPanel();
        private JPanel computerDisplay = new JPanel();
        private JLabel winnerAnnouncement = new JLabel("Winner Shown Here");
        private JPanel choicePanel = new JPanel();

        private RPSChoice rock = RPSChoice.ROCK;
        private RPSChoice paper = RPSChoice.PAPER;
        private RPSChoice scissors = RPSChoice.SCISSORS;

        private RPSChoice randChoice;

        private final JPanel humOrCompPanel = new JPanel();
        private final JLabel lblHuman = new JLabel("Human");
        private final JLabel lblComputer = new JLabel("Computer");
        private JButton btnScissors = new JButton("Scissors");
        private JButton btnPaper = new JButton("Paper");
        private JButton btnRock = new JButton("Rock");

        /**
         * Launch the application.
         */
        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() {
            setTitle("Rock, Paper, Scissors");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 544, 366);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);

            JPanel choicePanel = addPanels();

            addButtons(choicePanel, rock, btnRock);
            addButtons(choicePanel, paper, btnPaper);
            addButtons(choicePanel, scissors, btnScissors);
        }

        private JPanel addPanels() {
            contentPane.add(humanDisplay, BorderLayout.WEST);
            humanDisplay.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

            contentPane.add(computerDisplay, BorderLayout.EAST);

            winnerAnnouncement.setHorizontalAlignment(SwingConstants.CENTER);
            contentPane.add(winnerAnnouncement, BorderLayout.CENTER);

            contentPane.add(choicePanel, BorderLayout.NORTH);

            contentPane.add(humOrCompPanel, BorderLayout.SOUTH);
            humOrCompPanel.setLayout(new GridLayout(1, 0, 0, 0));
            lblHuman.setHorizontalAlignment(SwingConstants.CENTER);

            humOrCompPanel.add(lblHuman);
            lblComputer.setHorizontalAlignment(SwingConstants.CENTER);

            humOrCompPanel.add(lblComputer);

            return choicePanel;
        }

        private void addButtons(JPanel choicePanel, RPSChoice choice, JButton button ){
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    humanDisplay.removeAll();
                    humanDisplay.add(new JLabel(choice.getImageIcon()));

                    computerDisplay.removeAll();
                    randChoice = RPSChoice.randomChoice();
                    computerDisplay.add(new JLabel(randChoice.getImageIcon()));

                    choicePanel.repaint();
                    choicePanel.revalidate();

                }
            });
            choicePanel.add(button);
        }
    }


谢谢大家到目前为止提供的所有帮助!

最佳答案

您可以向枚举添加变量ImageIcon,并将其图像与ROCK,PAPER,SCISSORS关联:

public enum RPSChoice {

    ROCK(new ImageIcon(RPSChoice.class.getResource("rock.png"))),
    PAPER(new ImageIcon(RPSChoice.class.getResource("paper.png"))),
    SCISSORS(new ImageIcon(RPSChoice.class.getResource("scissors.png")));

    ImageIcon img;

    private RPSChoice(ImageIcon img) {
        this.img = img;
    }

    public ImageIcon getImage() {
        return img;
    }

}

10-08 02:50