Frage

I have a GUI created in my main class file

import java.awt.*;
import javax.swing.*;

public class TicTac extends JFrame {
TicTacEvent tictac = new TicTacEvent(this);
JPanel row1 = new JPanel();
JButton[][] boxes = new JButton[4][4];
JButton play = new JButton("Play");
JButton restart = new JButton("Restart");
JTextField blank1 = new JTextField();
JTextField blank2 = new JTextField();
JOptionPane win = new JOptionPane("Winner");
ImageIcon back = new ImageIcon("cardback.jpg");

public TicTac() {
    super ("Tic Tac Toe");
    setSize (800,650);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout layout = new FlowLayout();
    setLayout(layout);
    int name = 0;
    String newname;

    GridLayout layout1 = new GridLayout(5, 4, 10, 10);
    row1.setLayout(layout1);
    for (int x=0; x<=3; x++){
        for (int y=0; y<=3; y++){
            name = name + 1;
            newname = Integer.toString(name);
            boxes[x][y] = new JButton(newname);
            boxes[x][y].setIcon(back);
            row1.add(boxes[x][y]);
        }
    }
    row1.add(blank1);
    row1.add(play);
    row1.add(blank2);
    row1.add(restart);
    add (row1);

    play.addActionListener(tictac);
    for (int x=0; x<=3; x++){
        for (int y=0; y<=3; y++){
            boxes[x][y].addActionListener(tictac);
        }
    }


    setVisible(true);
}

public static void main(String[] arguments){
    TicTac frame = new TicTac();
}
}

And I have code using it here

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;



public class TicTacEvent implements ItemListener, ActionListener, Runnable {

TicTac gui;
Thread playing;
Thread restarting;
ImageIcon a = new ImageIcon("x.jpg");
ImageIcon b = new ImageIcon("o.jpg");
int clicks = 0;
int win = 0;
int winx = 0;
int winy = 0;
int cat = 0;

int[][] check = new int[4][4];

public TicTacEvent (TicTac in){

    gui = in;
    for (int row=0; row<=3; row++){
    for (int col=0; col<=3; col++){
           check[row][col]=0;
       }
   }
}
public void actionPerformed(ActionEvent event){

   String command = event.getActionCommand();

if (command.equals("Play")) {
startPlaying();
} else if (command.equals("Restart")) {
restart();
 }

   if (command.equals("1")) {
       b1();
   }
   if (command.equals("2")) {
       b2();
   }
   if (command.equals("3")) {
       b3();
   }
   if (command.equals("4")) {
       b4();
   }
   if (command.equals("5")) {
       b5();
   }
   if (command.equals("6")) {
       b6();
   }
   if (command.equals("7")) {
       b7();
   }
   if (command.equals("8")) {
       b8();
   }
   if (command.equals("9")) {
       b9();
   }
    if (command.equals("10")) {
       b10();
   }
    if (command.equals("11")) {
       b11();
   }
    if (command.equals("12")) {
       b12();
   }
    if (command.equals("13")) {
       b13();
   }
    if (command.equals("14")) {
       b14();
   }
   if (command.equals("15")) {
       b15();
   }
   if (command.equals("16")) {
       b16();
   }



    gui.blank1.setText("X Wins: " + winx + " Y Wins:" + winy);
    gui.blank2.setText("Cat Wins(Tie):" + cat);

}

void b1() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[0][0].setIcon(a);
        check[0][0] = 1;
    } else {
        gui.boxes[0][0].setIcon(b);
        check[0][0] = 2;
    }
    winner();

}
void b2() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[0][1].setIcon(a);
        check[0][1] = 1;
    } else {
        gui.boxes[0][1].setIcon(b);
        check[0][1] = 2;
    }
    winner();
}
void b3() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[0][2].setIcon(a);
        check[0][2] = 1;
    } else {
        gui.boxes[0][2].setIcon(b);
        check[0][2] = 2;
    }
    winner();
}
void b4() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[0][3].setIcon(a);
        check[0][3] = 1;
    } else {
        gui.boxes[0][3].setIcon(b);
        check[0][3] = 2;
    }
    winner();
}
void b5() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[1][0].setIcon(a);
        check[1][0] = 1;
    } else {
        gui.boxes[1][0].setIcon(b);
        check[1][0] = 2;
    }
    winner();
}
void b6() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[1][1].setIcon(a);
        check[1][1] = 1;
    } else {
        gui.boxes[1][1].setIcon(b);
        check[1][1] = 2;
    }
    winner();
}
void b7() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[1][2].setIcon(a);
        check[1][2] = 1;
    } else {
        gui.boxes[1][2].setIcon(b);
        check[1][2] = 2;
    }
    winner();
}
void b8() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[1][3].setIcon(a);
        check[1][3] = 1;
    } else {
        gui.boxes[1][3].setIcon(b);
        check[1][3] = 2;
    }
    winner();
}
void b9() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[2][0].setIcon(a);
        check[2][0] = 1;
    } else {
        gui.boxes[2][0].setIcon(b);
        check[2][0] = 2;
    }
    winner();
}
   void b10() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[2][1].setIcon(a);
        check[2][1] = 1;
    } else {
        gui.boxes[2][1].setIcon(b);
        check[2][1] = 2;
    }
    winner();
}
          void b11() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[2][2].setIcon(a);
        check[2][2] = 1;
    } else {
        gui.boxes[2][2].setIcon(b);
        check[2][2] = 2;
    }
    winner();
}
                 void b12() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[2][3].setIcon(a);
        check[2][3] = 1;
    } else {
        gui.boxes[2][3].setIcon(b);
        check[2][3] = 2;
    }
    winner();
}
    void b13() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[3][0].setIcon(a);
        check[3][0] = 1;
    } else {
        gui.boxes[3][0].setIcon(b);
        check[3][0] = 2;
    }
    winner();
}
   void b14() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[3][1].setIcon(a);
        check[3][1] = 1;
    } else {
        gui.boxes[3][1].setIcon(b);
        check[3][1] = 2;
    }
    winner();
}
       void b15() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[3][2].setIcon(a);
        check[3][2] = 1;
    } else {
        gui.boxes[3][2].setIcon(b);
        check[3][2] = 2;
    }
    winner();
}
    void b16() {
    clicks = clicks + 1;
    if ((clicks%2)==1){
        gui.boxes[3][3].setIcon(a);
        check[3][3] = 1;
    } else {
        gui.boxes[3][3].setIcon(b);
        check[3][3] = 2;
    }
    winner();
}

public void winner() {
    /** Check rows for winner */

    for (int x=0; x<=3; x++){
        if ((check[x][0]==check[x][1])&&(check[x][0]==check[x][2]) && (check[x][0]== check[x][3])) {

            if (check[x][0] ==1) {
                JOptionPane.showMessageDialog(null, "X is the winner");
                win = 1;
                winx +=1;

            } else if (check[x][0]==2){
                JOptionPane.showMessageDialog(null, "Y is the winner");
                win = 1;
                winy +=1;
            }
        }
    }

    /** Check columns for winner */
    for (int x=0; x<=3; x++){
        if ((check[0][x]==check[1][x])&&(check[0][x]==check[2][x])&& (check[0][x]== check[3][x])) {
            if (check[0][x]==1) {
                JOptionPane.showMessageDialog(null, "X is the winner");
                win = 1;
                winx +=1;


            } else if (check[0][x]==2) {
                JOptionPane.showMessageDialog(null, "Y is the winner");
                win = 1;
                winy +=1;

            }
        }
    }
        if ((check[1][1]== 1) && (check[3][3] == 1) && (check[2][2]==1) && (check[0][0]==1)
            || (check[3][0]==1) &&(check[1][2]==1) &&(check[0][3]==1))
        {
            JOptionPane.showMessageDialog(null, "X is the winner");
            win = 1;
            winx +=1;

        } else if ((check[1][1]== 2) && (check[3][3] == 2) && (check[2][2]==2) &&         (check[0][0]==2)
            || (check[3][0]==2) &&(check[1][2]==2) &&(check[0][3]==2)) {
            JOptionPane.showMessageDialog(null, "Y is the winner");
            win = 1;
            winy +=1;

        }

    //}

    /** Checks if the game is a tie */
    if ((clicks==16) && (win==0)) {
        JOptionPane.showMessageDialog(null, "The game is a tie");
       cat =+1;

    }


}

public void startPlaying() {
    playing = new Thread(this);
    playing.start();
    gui.play.setEnabled(false);
}
public void restart() {
TicTac restartok = new TicTac();
restartok.row1.add(restartok.blank1);
restartok.row1.add(restartok.play);
restartok.row1.add(restartok.blank2);
restartok.row1.add(restartok.restart);
restartok.add (restartok.row1);

restartok.play.addActionListener(restartok.tictac);
for (int x=0; x<=3; x++){
    for (int y=0; y<=3; y++){
        restartok.boxes[x][y].addActionListener(restartok.tictac);
    }
}
}


public void itemStateChanged(ItemEvent e) {
 //   throw new UnsupportedOperationException("Not supported yet.");
}

public void run() {
//restart();
    //throw new UnsupportedOperationException("Not supported yet.");

}


}

However my question is how I'd reset the GUI, I have some ideas..

The constructor "Public TicTac" creates the gui, however I am not sure how I'd get to happen again through another classfile. My understanding of OOP is that with a constructor I can call to by creating an object

TicTac restartok = new TicTac();

So assumedly, you'd think I'd be creating another JPanel/GUI everytime I called the restart(); method, no?

My OOP expeirence is limited, so I don't have a clue where to start or find out what logic is incorrect. Thanks.

War es hilfreich?

Lösung

To reset the game, just reset clicks to 0 and the buttons to their original state just as they were created:

boxes[x][y] = new JButton(newname);
boxes[x][y].setIcon(back);

Apart from that, you have many things to fix in your code. Here are some:

  • Your line boxes[x][y].addActionListener(tictac); is in a loop of its own where it should be in the initial loop. No reason to iterate twice over the same arrays.
  • Your layout decisions could be better (depending on what exactly you want to get).
  • Start the GUI with TicTac frame = new TicTac(); from inside an invokeLater method.
  • Create an ActionListener for each button with a unique functionality. In your case it's 1 for play, 1 for restart, and 1 for all the board buttons.
  • You're using event.getActionCommand but you did not call setActionCommand on any of the buttons.
  • Don't use multiple if when you check for the button pressed (action command name), use if else, or better - use a switch statement.
  • Don't use a separate function for each button. Use 1 function which receives the button as its argument.
  • Don't create fields when you can create local variables instead.

If you need clarifications leave a comment.

Edit: here is some modified code that resets the buttons (I couldn't stop myself from correcting other parts of the code, though there is still more to do).

public class TicTac extends JFrame {

    static JButton[][] boxes = new JButton[4][4];
    static int[][] check = new int[4][4];
    static JTextField blank1 = new JTextField();
    static JTextField blank2 = new JTextField();
    static int turns = 1;

    static ImageIcon back = new ImageIcon("cardback.jpg");
    static ImageIcon x = new ImageIcon("x.jpg");
    static ImageIcon o = new ImageIcon("o.jpg");

    public TicTac() {

        JPanel gamePanel = new JPanel(new GridLayout(5, 4, 10, 10));

        for (int x = 0; x<=3; x++) {
            for (int y = 0; y<=3; y++) {
                boxes[x][y] = new JButton(""+x+y, back);
                boxes[x][y].addActionListener(new XOActionListenr(x, y));
                gamePanel.add(boxes[x][y]);
            }
        }

        JButton play = new JButton("Play");
        JButton restart = new JButton("Restart");
        play.addActionListener(new PlayActionListenr());
        restart.addActionListener(new RestartActionListenr());

        setLayout(new FlowLayout());
        gamePanel.add(blank1);
        gamePanel.add(play);
        gamePanel.add(blank2);
        gamePanel.add(restart);
        add(gamePanel);

        setTitle("Tic Tac Toe");
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void winner() {

        // Check for winner
    }

    private class XOActionListenr implements ActionListener {

        private int row, col;

        private XOActionListenr(int row, int col) {

            this.row = row;
            this.col = col;
        }

        public void actionPerformed(ActionEvent e) {

            if (turns % 2 == 0) {
                boxes[row][col].setText("O");
                boxes[row][col].setIcon(o);
                check[row][col] = 2;
            }
            else {
                boxes[row][col].setText("X");
                boxes[row][col].setIcon(x);
                check[row][col] = 1;
            }
            winner();
            turns++;
        }
    }

    private class PlayActionListenr implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            // Not sure why you would need the "play" button at all,
            // but insert here what it's supposed to do
        }
    }

    private class RestartActionListenr implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            for (int x = 0; x<=3; x++) {
                for (int y = 0; y<=3; y++) {
                    boxes[x][y].setText(""+x+y);
                    boxes[x][y].setIcon(back);
                }
            }
        }
    }

    public static void main(String[] arguments) {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                new TicTac();
            }
        });
    }
}

Andere Tipps

You do not need to re-create whole GUI again and again. what you need to do is to clear textfields and whatever selection user has made till now. So you need to create a separate method in which you will reset the state of every button/textfields in your GUI.

for example: to clear textfield you will call setText("") method.

Hope this helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top