为了试图偶然发现问题,我已经玩了很多代码,但我完全不知道我哪里出错了。只用了几个月的java编程,所以对它还是很陌生。
package eliminationgame;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.event.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
public class EliminationGame extends JFrame implements ActionListener{
// creates buttons that represent numbers to be eliminated
JButton numberOne = new JButton("1");
JButton numberTwo = new JButton("2");
JButton numberThree = new JButton("3");
JButton numberFour = new JButton("4");
JButton numberFive = new JButton("5");
JButton numberSix = new JButton("6");
JButton numberSeven = new JButton("7");
JButton numberEight = new JButton("8");
JButton numberNine = new JButton("9");
JButton numberTen = new JButton("10");
JButton numberEleven = new JButton("11");
JButton numberTwelve = new JButton("12");
// creates buttons for rolling dice, resetting game and opening instructions
JButton rollDice = new JButton("Roll Dice");
JButton resetGame = new JButton("Reset");
JButton gameInstructions = new JButton("How to Play");
// creates array to hold previous game scores, and an integer to hold current game
int[] previousScores;
int currentScore = 78;
// creates lables for current score, previous scores, and both dice
JLabel displayCurrentScore = new JLabel("Current Score: " + currentScore );
JLabel displayPreviousScores = new JLabel("Previous Scores: ");
JLabel diceOne = new JLabel();
JLabel diceTwo = new JLabel();
JPanel pnlEast;
JPanel pnlSouth;
JPanel pnlWest;
JPanel pnlCenter;
JFrame appWindow;
public void EliminationGame()
{
// sets roll dice to be the default button
//appWindow.getRootPane().setDefaultButton(rollDice);
// creates components and panels
pnlEast = new JPanel();
pnlSouth = new JPanel();
pnlWest = new JPanel();
pnlCenter = new JPanel();
pnlEast.setLayout(new GridLayout(3,3));
pnlEast.add(numberOne).setEnabled(false);
pnlEast.add(numberTwo).setEnabled(false);
pnlEast.add(numberThree).setEnabled(false);
pnlEast.add(numberFour).setEnabled(false);
pnlEast.add(numberFive).setEnabled(false);
pnlEast.add(numberSix).setEnabled(false);
pnlEast.add(numberSeven).setEnabled(false);
pnlEast.add(numberEight).setEnabled(false);
pnlEast.add(numberNine).setEnabled(false);
pnlEast.add(numberTen).setEnabled(false);
pnlEast.add(numberEleven).setEnabled(false);
pnlEast.add(numberTwelve).setEnabled(false);
pnlSouth.setLayout(new GridLayout(1,3));
pnlSouth.add(resetGame);
pnlSouth.add(rollDice);
pnlSouth.add(gameInstructions);
pnlWest.setLayout(new GridLayout (1,3));
pnlWest.add(displayCurrentScore);
pnlWest.add(displayPreviousScores);
pnlCenter.setLayout(new GridLayout (1,3));
pnlCenter.add(diceOne);
pnlCenter.add(diceTwo);
// adds action listener to buttons
numberOne.addActionListener(this);
numberTwo.addActionListener(this);
numberThree.addActionListener(this);
numberFour.addActionListener(this);
numberFive.addActionListener(this);
numberSix.addActionListener(this);
numberSeven.addActionListener(this);
numberEight.addActionListener(this);
numberNine.addActionListener(this);
numberTen.addActionListener(this);
numberEleven.addActionListener(this);
numberTwelve.addActionListener(this);
rollDice.addActionListener(this);
gameInstructions.addActionListener(this);
resetGame.addActionListener(this);
appWindow = new JFrame("Elimination");
appWindow.add(pnlSouth, BorderLayout.SOUTH);
appWindow.add(pnlWest, BorderLayout.WEST);
appWindow.add(pnlCenter, BorderLayout.CENTER);
appWindow.add(pnlEast, BorderLayout.EAST);
appWindow.pack();
appWindow.setVisible(true);
}
public static void main(String[] args)
{
EliminationGame g1 = new EliminationGame();
g1.pack();
g1.setVisible(true);
}
public void actionPerformed(ActionEvent thisEvent)
{
String strButtonName = thisEvent.getActionCommand();
if (strButtonName.equalsIgnoreCase("numberOne"))
{
currentScore = currentScore - 1;
numberOne.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("numberTwo"))
{
currentScore = currentScore - 2;
numberTwo.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("numberThree"))
{
currentScore = currentScore - 3;
numberThree.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("numberFour"))
{
currentScore = currentScore - 4;
numberFour.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("numberFive"))
{
currentScore = currentScore - 5;
numberFive.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("numberSix"))
{
currentScore = currentScore - 6;
numberSix.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("numberSeven"))
{
currentScore = currentScore - 7;
numberSeven.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("numberEight"))
{
currentScore = currentScore - 8;
numberEight.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("numberNine"))
{
currentScore = currentScore - 9;
numberNine.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("numberTen"))
{
currentScore = currentScore - 10;
numberTen.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("numberEleven"))
{
currentScore = currentScore - 11;
numberEleven.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("numberTwelve"))
{
currentScore = currentScore - 12;
numberTwelve.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("resetGame"))
{
currentScore = 78;
numberOne.setEnabled(false);
numberTwo.setEnabled(false);
numberThree.setEnabled(false);
numberFour.setEnabled(false);
numberFive.setEnabled(false);
numberSix.setEnabled(false);
numberSeven.setEnabled(false);
numberEight.setEnabled(false);
numberNine.setEnabled(false);
numberTen.setEnabled(false);
numberEleven.setEnabled(false);
numberTwelve.setEnabled(false);
}
else
if (strButtonName.equalsIgnoreCase("gameInstructions"))
{
}
else
if (strButtonName.equalsIgnoreCase("rollDice"))
{
}
}
}