我正在制作一个 SUPER Tic-Tac-Toe 游戏,一切正常,除非重新加载网页时添加了更多按钮。我四处寻找如何解决这个问题,但没有找到解决办法。添加更多按钮的错误很难解释,因此小程序的链接如下。重新加载该页面,你会明白我的意思。小程序:点击此处查看小程序页面
代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Frame extends JApplet implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
public static JFrame frame;
public static JPanel center = new JPanel();
public static JMenuBar menu = new JMenuBar();
public static JMenu file = new JMenu("File");
public static JMenu view = new JMenu("View");
public static JMenu help = new JMenu("Help");
public static JMenuItem newGame = new JMenuItem("New game");
public static JCheckBoxMenuItem showColor = new JCheckBoxMenuItem("Player Colors");
public static JMenuItem helpMenu = new JMenuItem("How to Play");
public static JMenuItem helpOpt = new JMenuItem("Options Help");
public static JCheckBoxMenuItem ending = new JCheckBoxMenuItem("Show Panel Results");
public static JCheckBoxMenuItem ai = new JCheckBoxMenuItem("Enable AI");
public static JMenuItem playerNames = new JMenuItem("Change player names");
public static JPanel playerIndicator = new JPanel();
public static JTextField f1 = new JTextField(20);
public static String player = "x";
public static TicTacToeButtonPanel[][] bg;
public static boolean done = false;
public static String player1 = "Player 1";
public static String player2 = "Player 2";
public static TicTacToeButtonPanel selected;
public static boolean playerMove = true;
public static JPanel right = new JPanel();
public static PlayerIndecator p1 = new PlayerIndecator(player1);
public static PlayerIndecator p2 = new PlayerIndecator(player2);
/**
* Called by Applet
* Constructs basic frame and sets up game
*/
@Override
public void init()
{
this.setVisible(false);
this.setSize(500,500);
this.setPreferredSize(new Dimension(500,500));
bg = new TicTacToeButtonPanel[3][3];
center.setLayout(new GridLayout(3,3));
playerIndicator.setLayout(new BorderLayout());
helpMenu.setActionCommand("help");
helpOpt.setActionCommand("opt");
helpMenu.addActionListener(new Frame());
helpOpt.addActionListener(new Frame());
int k = 0;
for(int i=0;i<3; i++)
for(int j=0; j<3; j++){
bg[i][j] = null;
bg[i][j] = new TicTacToeButtonPanel(k);
center.add(bg[i][j]);
newGame.addActionListener(bg[i][j]);
showColor.addActionListener(bg[i][j]);
bg[i][j].isSelected = true;
k++;
}
setJMenuBar(menu);
menu.add(file);
menu.add(view);
view.add(showColor);
view.add(ending);
view.add(playerNames);
menu.add(help);
help.add(helpMenu);
help.add(helpOpt);
file.add(newGame);
file.add(ai);
right.add(p1);
right.add(p2);
p1.setBorder(BorderFactory.createBevelBorder(1, Color.lightGray, Color.white, Color.gray, Color.lightGray));
p2.setBorder(BorderFactory.createBevelBorder(1, Color.gray,Color.white));
p1.setBackground(Color.white);
p2.setBackground(Color.white);
ai.setSelected(true);
newGame.setActionCommand("new game");
showColor.setActionCommand("color");
playerNames.setActionCommand("names");
playerNames.addActionListener(new Frame());
showColor.setSelected(true);
ending.setSelected(true);
add(center);
add(right, BorderLayout.PAGE_START);
add(playerIndicator, BorderLayout.PAGE_END);
f1.setBackground(Color.white);
playerIndicator.add(f1);
f1.setEditable(false);
this.setVisible(true);
return;
}
@Override
public void stop()
{
for(Component a: this.getContentPane().getComponents())
this.remove(a);
}
@Override
public void start()
{
return;
}
@Override
public void destroy()
{
for(Component a: this.getContentPane().getComponents())
this.remove(a);
}
/**
* Check on the BIG board if a player has won or a cats game
*/
public static void check()
{
checkSpace(0,0, 0,1, 0,2);
checkSpace(1,0, 1,1, 1,2);
checkSpace(2,0, 2,1, 2,2);
checkSpace(0,0, 1,0, 2,0);
checkSpace(0,1, 1,1, 2,1);
checkSpace(0,2, 1,2, 2,2);
checkSpace(0,0, 1,1, 2,2);
checkSpace(0,2, 1,1, 2,0);
checkCats();
}
/**
* Used to check if a player has won the game at a location
* @param ROW1
* @param COLUMN1
* @param ROW2
* @param COLUMN2
* @param ROW3
* @param COLUMN3
*/
public static void checkSpace(int ROW1, int COLUMN1, int ROW2, int COLUMN2, int ROW3, int COLUMN3)
{
if (bg[ROW1][COLUMN1].getOwner() == 1
&& bg[ROW2][COLUMN2].getOwner() == 1
&& bg[ROW3][COLUMN3].getOwner() == 1
&& !done)
winGame(1);
else if (bg[ROW1][COLUMN1].getOwner() == 2
&& bg[ROW2][COLUMN2].getOwner() == 2
&& bg[ROW3][COLUMN3].getOwner() == 2
&& !done)
winGame(2);
}
/**
* Used to check if game ends up as a cats game
*/
public static void checkCats()
{
if(bg[0][0].getOwner() != 0 && bg[0][1].getOwner() != 0
&& bg[0][2].getOwner() != 0 && bg[1][0].getOwner() != 0
&& bg[1][1].getOwner() != 0 && bg[1][2].getOwner() != 0
&& bg[2][0].getOwner() != 0 && bg[2][1].getOwner() != 0
&& bg[2][2].getOwner() != 0 && !done)
catsGame();
}
/**
* Called by checkCats()
* ends game and tells user that its a tie
*/
public static void catsGame()
{
JOptionPane.showMessageDialog(Frame.frame ,
"Aww! The game turned out to be \n a cats game. \n better luck next time",
"CATS GAME!",
JOptionPane.ERROR_MESSAGE);
done = true;
}
/**
* Ends game and tells player who won
* @param Winningplayer player who won the game
*/
public static void winGame(int Winningplayer)
{
String play;
String winPlayer;
if(Winningplayer == 1){
play = "x";
winPlayer = player1;
}
else{
play = "o";
winPlayer = player2;
}
for(int k=0; k<3; k++)
for(int t=0;t<3;t++)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
{
bg[k][t].board[i][j].setEnabled(false);
bg[k][t].isSelected = false;
if(showColor.isSelected() && Winningplayer == 1)bg[k][t].board[i][j].setForeground(Color.red);
if(showColor.isSelected() && Winningplayer == 2)bg[k][t].board[i][j].setForeground(Color.blue);
if(!showColor.isSelected())bg[k][t].board[i][j].setForeground(Color.black);
bg[k][t].board[i][j].setText(play);
bg[k][t].done = true;
}
done = true;
f1.setText(winPlayer + " WON THE GAME!");
JOptionPane.showMessageDialog(frame,
"Congradulations!\n"+
winPlayer +"\n"+
"YOU WON!",
"CONGRADULATIONS!",
JOptionPane.WARNING_MESSAGE);
}
/**
* Basic button actions
*/
public void actionPerformed(ActionEvent e)
{
if("help".equals(e.getActionCommand()))
{
JOptionPane.showMessageDialog(frame,
"How to play:\n"+
"SUPER Tic-Tac-Toe is regular Tic-Tac-Toe inside Tic-Tac-Toe. \n there are 9 Tic-Tac-Toe games inside one big Tic-Tac-Toe game \n"+
"if you win a game then you gain that panel for the big board. once the big board is won the game is over. \n Ways to tell who owns a panel: \n 1) a colored border around one of the games.\n 2) a big letter representing a player.",
"How to play",
JOptionPane.INFORMATION_MESSAGE);
}
if("opt".equals(e.getActionCommand()))
{
JOptionPane.showMessageDialog(frame,
"Options help:\n"+
"this game consists of 3 options: \n"+
"located in the VIEW menu are options to change player names, turn on or off player colors, or turn on or off panel end results. \n panel end results: ON means that once a panel is won the game will stay there. \n panel end results: OFF means that once a game is won, the game will turn into a big letter representing who won. \n to change player names please enter in the names in the dialog box popup and it ok",
"Options help",
JOptionPane.INFORMATION_MESSAGE);
}
if("names".equals(e.getActionCommand()))
{
String pl1 = (String)JOptionPane.showInputDialog(frame,
"Player 1: ",
"Change player names",
JOptionPane.QUESTION_MESSAGE);
String pl2 = (String)JOptionPane.showInputDialog(frame,
"Player 2: ",
"Change player names",
JOptionPane.QUESTION_MESSAGE);
player1 = pl1;
player2 = pl2;
if(pl1.length() > 15)
p1.setText(pl1.substring(0,15));
else{
String k = pl1;
for(int i= pl1.length(); i < 8; i++){
k = k + " ";
}
p1.setText(k);
}
if(pl2.length() > 15)
p2.setText(pl2.substring(0,15));
else{
String k = pl2;
for(int i= pl2.length(); i < 8; i++){
k = k + " ";
}
p2.setText(k);
}
f1.setText("Names have been changed");
}
}
public static void openSel()
{
for(int q=0; q<3; q++)
for(int h=0; h<3; h++)
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
{
if(bg[q][h].getOwner() == 0){
bg[q][h].isSelected = true;
bg[q][h].board[i][j].setBackground(Color.white);
bg[q][h].board[i][j].setBorder(BorderFactory.createBevelBorder(1, Color.lightGray, Color.white));
bg[q][h].board[i][j].setEnabled(true);
}
else
{
if(bg[q][h].getOwner() == 1){
bg[q][h].board[i][j].setBackground(Color.red);
bg[q][h].board[i][j].setBorder(BorderFactory.createBevelBorder(1,Color.darkGray,Color.gray));
bg[q][h].isSelected = false;
}
else{
bg[q][h].board[i][j].setBackground(Color.blue);
bg[q][h].board[i][j].setBorder(BorderFactory.createBevelBorder(1,Color.darkGray,Color.gray));
bg[q][h].isSelected = false;
}
bg[q][h].board[i][j].setEnabled(false);
}
}
selected.isSelected = false;
}
/**
* very important void in class!
* changes the selected panel
* @param panel panel to select
*/
public static void changeSel(TicTacToeButtonPanel panel)
{
for(int i=0; i<3; i++)for(int j=0; j<3; j++)if(bg[i][j].isSelected)bg[i][j].isSelected = false;
selected = panel;
panel.isSelected = true;
for(int q=0; q<3; q++)
for(int h=0; h<3; h++)
{
if(selected.getOwner() != 0)
{
openSel();
}
else if(!bg[q][h].isSelected && bg[q][h].getOwner() == 0)
{
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
{
bg[q][h].board[i][j].setEnabled(false);
bg[q][h].board[i][j].setBackground(Color.gray);
bg[q][h].board[i][j].setBorder(BorderFactory.createBevelBorder(1,Color.darkGray,Color.gray));
}
}
else if(bg[q][h].isSelected && bg[q][h].getOwner() == 0)
{
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
{
bg[q][h].board[i][j].setEnabled(true);
bg[q][h].board[i][j].setBackground(Color.white);
bg[q][h].board[i][j].setBorder(BorderFactory.createBevelBorder(1, Color.lightGray, Color.white));
}
}
}
}
}