gpt4 book ai didi

Java:布、石头、剪刀 GUI

转载 作者:行者123 更新时间:2023-12-01 12:20:48 26 4
gpt4 key购买 nike

我的类(class)被分配了一个石头剪刀布游戏。以下是作业说明:

学习目标:

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

描述:编写一个程序与计算机玩剪刀石头布

要求:

  • 创建一个枚举来表示三种选择:石头、纸和剪刀。

    • 包含 ImageIcon 类型的字段(可选更多字段)
    • 包含构造函数
    • 包含一个评估方法
  • 创建一个与计算机玩石头剪刀布的 GUI 应用程序

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

*编辑

好吧,我已经到了最后冲刺阶段,大部分真正需要的是evaluate() 方法。关于如何去做有什么想法吗?过去一个小时我一直在绞尽脑汁,但还是想不出来。无论如何,这是更新后的代码:

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

还有图形用户界面

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

}

关于Java:布、石头、剪刀 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26662834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com