java - TicTacToe 帮助 重画功能

标签 java swing repaint tic-tac-toe

我正在使用 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();
            }
        }
    }
}

最佳答案

您需要将文本设置为空

setText("");

//伪代码

foreach(Button temp in windows.allChild) {
    temp.setText("");
}
windows.repaint();

关于java - TicTacToe 帮助 重画功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21777459/

相关文章:

C++重绘窗口

java - 获取 "success"而没有收到任何 GCM 消息

Java:GridLayout 上的动画 Sprite 第 2 部分

java - 如何在启动时将 JFrame 定位到屏幕的右上角

java - 重绘在线程中不起作用

Java 6 : how to drag/move a line?

java - Mockito 的验证和参数捕获器的工作

java - 在 Play Framework 2.5.X (Java) 中安排异步任务

java - 如何将 NumberFormatException 设置为标签

java - 清除 Jframe 上的绘图