我有一个学校的任务,我将创建一个石头,纸,剪刀的游戏与计算机。游戏应该能够在游戏进行的过程中记录分数。
代码编译成功,但当我运行游戏时,"石头"、"纸 "和 "剪刀 "的按钮没有任何效果,尽管计算机能够成功地选择其中一个。我在游戏中加入了 addActionListener
, setActionCommand
并使之成为要么是石头剪刀布的形象变化,要么是石头剪刀布的形象变化。actionPerformed
通过if语句,将Icon设置为所选对象的图像。
理想情况下,计算机和用户都应该选择(计算机通过Math.random),然后将变量 results
应该说用户是否赢了。
我翻看了代码,也在网上找了解决方案,在修正了一些小错误后,同样的问题普遍存在:石头、纸和剪刀的JButton在用户按下后也不显示任何结果。是我的按钮缺失了一个方面还是我漏掉了什么明显的东西?
这是我的代码。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class attempt5 extends JPanel implements ActionListener {
JButton rock, paper, scissors, cont;
JLabel YOURscore2, COMPscore2, results, YOURpic, COMPpic;
int yours = 0;
int theirs = 0;
public static void main(String args[]) { //main
attempt5 content = new attempt5();
JFrame window = new JFrame("Rock, Paper Scissors");
window.setContentPane(content);
window.setSize(600, 1000);
window.setLocation(100, 100);
window.setVisible(true);
} //end main
public attempt5() { //constructor
JLabel title = new JLabel("Rock, Paper, Scissors");
title.setFont(new Font("Arial", Font.BOLD, 50));
title.setForeground(Color.red);
JLabel pick = new JLabel("Pick one:");
pick.setFont(new Font("Arial", Font.ITALIC, 50));
rock = new JButton("ROCK");
rock.addActionListener(this);
rock.setActionCommand("rock");
paper = new JButton("PAPER");
paper.addActionListener(this);
paper.setActionCommand("paper");
scissors = new JButton("SCISSORS");
scissors.addActionListener(this);
scissors.setActionCommand("scissors");
JLabel picture = new JLabel(createImageIcon("rockpaperscissors.png"));
cont = new JButton("Continue");
cont.setPreferredSize(new Dimension(350, 48));
cont.addActionListener(this);
cont.setActionCommand("continue");
JLabel YOURchoice = new JLabel("Your choice:");
YOURpic = new JLabel("--");
JLabel COMPchoice = new JLabel("Computer's choice:");
COMPpic = new JLabel("--");
results = new JLabel("--");
results.setFont(new Font("Arial", Font.BOLD, 40));
JLabel YOURscore = new JLabel("Your score: ");
pick.setFont(new Font("Arial", Font.ITALIC, 35));
YOURscore2 = new JLabel("--");
JLabel COMPscore = new JLabel("Computer's score: ");
pick.setFont(new Font("Arial", Font.ITALIC, 35));
COMPscore2 = new JLabel("--");
add(title);
add(picture);
add(pick);
add(rock);
add(paper);
add(scissors);
add(cont);
add(YOURchoice);
add(YOURpic);
add(COMPchoice);
add(COMPpic);
add(results);
add(YOURscore);
add(YOURscore2);
add(COMPscore);
add(COMPscore2);
} //end constructor
public void actionPerformed(ActionEvent e) {
int n = (int)(Math.random() * 3 + 1);
if (e.getActionCommand().equals("continue")) {
{
if (n == 1) {
COMPpic.setIcon(createImageIcon("rock.png"));
} else if (n == 2) {
COMPpic.setIcon(createImageIcon("newspaper.png"));
} else {
COMPpic.setIcon(createImageIcon("scissors.png"));
}
}
if (e.getActionCommand().equals("rock")) {
YOURpic.setIcon(createImageIcon("rock.png"));
if (n == 1)
results.setText("Tie");
else if (n == 2) {
results.setText("Computer wins");
theirs++;
} else if (n == 3) {
results.setText("You win");
yours++;
}
} else if (e.getActionCommand().equals("paper")) {
YOURpic.setIcon(createImageIcon("newspaper.png"));
if (n == 1) {
results.setText("You win");
yours++;
} else if (n == 2)
results.setText("Tie");
else if (n == 3) {
results.setText("You loose");
theirs++;
}
} else if (e.getActionCommand().equals("scissors")) {
YOURpic.setIcon(createImageIcon("scissors.png"));
if (n == 1) {
results.setText("You loose");
theirs++;
} else if (n == 2) {
results.setText("You win");
yours++;
} else if (n == 3)
results.setText("Computer wins");
}
YOURscore2.setText(" " + yours);
COMPscore2.setText(" " + theirs);
}
}
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = attempt5.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
} //end main
我不确定我是否发现了所有的问题。
我不知道是什么 Continue
按钮应该做的。
总之,这是我修复的GUI。
下面是我对代码的主要修改。
我在 事件派遣线程 通过将Swing组件的创建和执行放在一个对 SwingUtilities
invokeLater
方法。
我用的是 周转式布局管理器 来定位各种Swing组件。 这样就可以调整JFrame的大小。
我读取了一次文件图像,并将其保存为 ImageIcons
. 这样一来,游戏就不用一直从资源文件夹中读取图片了。
这是修改后的代码。
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Attempt5 extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
private ImageIcon rockIcon;
private ImageIcon paperIcon;
private ImageIcon scissorsIcon;
private JButton rock, paper, scissors, cont;
private JLabel YOURscore2, COMPscore2, results;
private JLabel YOURpic, COMPpic;
private int yours = 0;
private int theirs = 0;
public static void main(String args[]) { // main
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Attempt5 content = new Attempt5();
JFrame window = new JFrame(
"Rock, Paper Scissors");
window.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
window.setContentPane(content);
window.pack();
window.setLocationByPlatform(true);
window.setVisible(true);
}
});
} // end main
public Attempt5() { // constructor
this.rockIcon = createImageIcon("rock.png");
this.paperIcon = createImageIcon("newspaper.png");
this.scissorsIcon = createImageIcon("scissors.png");
setLayout(new GridBagLayout());
// top, left, bottom, right
Insets topInsets = new Insets(10, 10, 10, 10);
Insets insets = new Insets(0, 10, 10, 10);
Insets rightInsets = new Insets(0, 0, 10, 10);
int gridy = 0;
JLabel title = new JLabel("Rock, Paper, Scissors");
title.setHorizontalAlignment(JLabel.CENTER);
title.setFont(new Font("Arial", Font.BOLD, 50));
title.setForeground(Color.red);
addComponent(this, title, 0, gridy++, 4, 1,
topInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JLabel pick = new JLabel("Pick one:");
pick.setFont(new Font("Arial", Font.ITALIC, 50));
addComponent(this, pick, 0, gridy, 1, 1,
insets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(0, 3, 10, 10));
rock = new JButton("ROCK");
rock.setFont(new Font("Arial", Font.PLAIN, 20));
rock.addActionListener(this);
rock.setActionCommand("rock");
buttonPanel.add(rock);
paper = new JButton("PAPER");
paper.setFont(new Font("Arial", Font.PLAIN, 20));
paper.addActionListener(this);
paper.setActionCommand("paper");
buttonPanel.add(paper);
scissors = new JButton("SCISSORS");
scissors.setFont(new Font("Arial", Font.PLAIN, 20));
scissors.addActionListener(this);
scissors.setActionCommand("scissors");
buttonPanel.add(scissors);
addComponent(this, buttonPanel, 1, gridy++, 3, 1,
rightInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JLabel picture = new JLabel(
createImageIcon("rockpaperscissors.png"));
addComponent(this, picture, 0, gridy++, 4, 1,
insets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
cont = new JButton("Continue");
cont.setFont(new Font("Arial", Font.PLAIN, 20));
cont.addActionListener(this);
cont.setActionCommand("continue");
addComponent(this, cont, 0, gridy++, 4, 1,
insets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JLabel YOURchoice = new JLabel("Your choice:");
YOURchoice.setFont(new Font("Arial", Font.PLAIN, 20));
addComponent(this, YOURchoice, 0, gridy, 1, 1,
insets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
YOURpic = new JLabel("--");
addComponent(this, YOURpic, 1, gridy, 1, 1,
rightInsets, GridBagConstraints.LINE_END,
GridBagConstraints.HORIZONTAL);
JLabel COMPchoice = new JLabel("Computer's choice:");
COMPchoice.setFont(new Font("Arial", Font.PLAIN, 20));
addComponent(this, COMPchoice, 2, gridy, 1, 1,
rightInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
COMPpic = new JLabel("--");
addComponent(this, COMPpic, 3, gridy++, 1, 1,
rightInsets, GridBagConstraints.LINE_END,
GridBagConstraints.HORIZONTAL);
results = new JLabel("--");
results.setHorizontalAlignment(JLabel.CENTER);
results.setFont(new Font("Arial", Font.BOLD, 40));
addComponent(this, results, 0, gridy++, 4, 1,
insets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JLabel YOURscore = new JLabel("Your score: ");
YOURscore.setFont(new Font("Arial", Font.ITALIC, 35));
addComponent(this, YOURscore, 0, gridy, 1, 1,
insets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
YOURscore2 = new JLabel("--");
YOURscore2.setFont(new Font("Arial", Font.ITALIC, 35));
addComponent(this, YOURscore2, 1, gridy, 1, 1,
rightInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JLabel COMPscore = new JLabel("Computer's score: ");
COMPscore.setFont(new Font("Arial", Font.ITALIC, 35));
addComponent(this, COMPscore, 2, gridy, 1, 1,
rightInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
COMPscore2 = new JLabel("--");
COMPscore2.setFont(new Font("Arial", Font.ITALIC, 35));
addComponent(this, COMPscore2, 3, gridy++, 1, 1,
rightInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
} // end constructor
private void addComponent(Container container,
Component component, int gridx, int gridy,
int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(
gridx, gridy, gridwidth, gridheight,
1.0, 1.0, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
@Override
public void actionPerformed(ActionEvent e) {
int n = (int) (Math.random() * 3 + 1);
if (e.getActionCommand().equals("continue")) {
if (n == 1) {
COMPpic.setIcon(rockIcon);
} else if (n == 2) {
COMPpic.setIcon(paperIcon);
} else {
COMPpic.setIcon(scissorsIcon);
}
}
if (e.getActionCommand().equals("rock")) {
YOURpic.setIcon(rockIcon);
if (n == 1)
results.setText("Tie");
else if (n == 2) {
results.setText("Computer wins");
theirs++;
} else if (n == 3) {
results.setText("You win");
yours++;
}
} else if (e.getActionCommand().equals("paper")) {
YOURpic.setIcon(paperIcon);
if (n == 1) {
results.setText("You win");
yours++;
} else if (n == 2)
results.setText("Tie");
else if (n == 3) {
results.setText("You lose");
theirs++;
}
} else if (e.getActionCommand().equals("scissors")) {
YOURpic.setIcon(scissorsIcon);
if (n == 1) {
results.setText("You lose");
theirs++;
} else if (n == 2) {
results.setText("You win");
yours++;
} else if (n == 3)
results.setText("Computer wins");
}
YOURscore2.setText(" " + yours);
COMPscore2.setText(" " + theirs);
}
private ImageIcon createImageIcon(String path) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
} // end main