我正在使用Java创建一个TicTacToe游戏,我想要一些帮助。
在玩家获胜后我希望游戏要求复赛。发生了这种情况,但按钮面板并不令人耳目一新。我使用了“重绘”功能,但它似乎不起作用。 提前谢谢
以下是代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
@SuppressWarnings("unchecked")
public class TicTacToeGUI extends JFrame implements ActionListener{
JButton btn1;
JButton btn2;
JButton btn3;
JButton btn4;
JButton btn5;
JButton btn6;
JButton btn7;
JButton btn8;
JButton btn9;
public JFrame window = new JFrame("Tic Tac Toe");
public int playerTurn = 0;
public boolean win = false;
String letter = "";
private final String[] yesno = {" ", "Yes", "No"};
public TicTacToeGUI(){
window.setTitle("Tic Tac Toe");
window.setSize(300,300);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation(dim.width/2-window.getSize().width/2, dim.height/2-window.getSize().height/2);
window.setLayout(new GridLayout(3,3));
this.addStuff();
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
window.setVisible(true);
window.setResizable(false);
}
public void addStuff(){
btn1 = new JButton("");
btn1.addActionListener(this);
btn2 = new JButton("");
btn2.addActionListener(this);
btn3 = new JButton("");
btn3.addActionListener(this);
btn4 = new JButton("");
btn4.addActionListener(this);
btn5 = new JButton("");
btn5.addActionListener(this);
btn6 = new JButton("");
btn6.addActionListener(this);
btn7 = new JButton("");
btn7.addActionListener(this);
btn8 = new JButton("");
btn8.addActionListener(this);
btn9 = new JButton("");
btn9.addActionListener(this);
window.add(btn1);
window.add(btn2);
window.add(btn3);
window.add(btn4);
window.add(btn5);
window.add(btn6);
window.add(btn7);
window.add(btn8);
window.add(btn9);
}
public void actionPerformed(ActionEvent e){
playerTurn ++;
if(playerTurn == 1 || playerTurn == 3 || playerTurn == 5 || playerTurn == 7 || playerTurn == 9) {
letter = "X";
}else if(playerTurn == 2 || playerTurn == 4 || playerTurn == 6 || playerTurn == 8 || playerTurn == 10){
letter = "O";
}
if(e.getSource() == btn1){
btn1.setText(letter);
btn1.setEnabled(false);
} else if(e.getSource() == btn2){
btn2.setText(letter);
btn2.setEnabled(false);
} else if(e.getSource() == btn3){
btn3.setText(letter);
btn3.setEnabled(false);
} else if(e.getSource() == btn4){
btn4.setText(letter);
btn4.setEnabled(false);
} else if(e.getSource() == btn5){
btn5.setText(letter);
btn5.setEnabled(false);
} else if(e.getSource() == btn6){
btn6.setText(letter);
btn6.setEnabled(false);
} else if(e.getSource() == btn7){
btn7.setText(letter);
btn7.setEnabled(false);
} else if(e.getSource() == btn8){
btn8.setText(letter);
btn8.setEnabled(false);
} else if(e.getSource() == btn9){
btn9.setText(letter);
btn9.setEnabled(false);
}
//when horizontal lines win
if (btn1.getText() == btn2.getText() && btn1.getText() == btn3.getText() && btn1.getText() != ""){
win = true;
}else if (btn4.getText() == btn5.getText() && btn4.getText() == btn6.getText() && btn4.getText() != ""){
win = true;
}else if (btn7.getText() == btn8.getText() && btn7.getText() == btn9.getText() && btn7.getText() != ""){
win = true;
}
//when vertical wins
if (btn1.getText() == btn4.getText() && btn1.getText() == btn7.getText() && btn1.getText() != ""){
win = true;
}else if (btn2.getText() == btn5.getText() && btn2.getText() == btn8.getText() && btn2.getText() != ""){
win = true;
}else if (btn3.getText() == btn6.getText() && btn4.getText() == btn9.getText() && btn3.getText() != ""){
win = true;
}
//when diagonal wins
if (btn1.getText() == btn5.getText() && btn1.getText() == btn9.getText() && btn1.getText() != ""){
win = true;
}else if (btn3.getText() == btn5.getText() && btn3.getText() ==btn7.getText() && btn3.getText() != ""){
win = true;
}
if(win == true){
JOptionPane.showMessageDialog(null, letter + " WON! ");
int n = JOptionPane.showConfirmDialog(null, "Do you want to play again?", "tictac",JOptionPane.YES_NO_OPTION);
if (n == 0) {
dispose();
repaint();
}
}
else if (playerTurn == 9 && win == false){
JOptionPane.showMessageDialog(null, "GAME IS TIED!");
int n = JOptionPane.showConfirmDialog(null, "Do you want to play again?", "tictac",JOptionPane.YES_NO_OPTION);
if (n == 0) {
dispose();
repaint();
}
}
}
}
答案 0 :(得分:2)
您需要将文本设置为空
setText("");
//伪代码
foreach(Button temp in windows.allChild) {
temp.setText("");
}
windows.repaint();