Lego Games
Lego Games

Reputation: 25

Unable to figure out the problem in my program

I have a built tictactoe in java but due to some reason the reset and the exit buttons are not working. I am unable to solve the problem. I have searched through other questions but unable to figure out the problem with my code. The code is in separate class. There is also main class that calls the object to run the program.

The array of buttons are working fine. O and X are exactly working as I want them to but the 2nd panel with the reset and the exit buttons is not working. The program is in GUI.

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

public class Frame1 implements ActionListener {

    private JButton[] board;
    private int turn;
    private JButton reset;
    private JButton exit;

    public Frame1() {
        turn = 1;
        JFrame frm = new JFrame("Chess");
        JButton reset = new JButton("Reset");
        JButton exit = new JButton("Exit");
        frm.setSize(300, 300);

        JPanel LowerPanel = new JPanel();
        LowerPanel.add(reset);
        LowerPanel.add(exit);
        exit.addActionListener(this);
        reset.addActionListener(this);
        board = new JButton[9];
        JPanel CenterPanel = new JPanel();
        CenterPanel.setLayout(new GridLayout(3, 3));
        for (int i = 0; i < 9; i++) {
            board[i] = new JButton();
            board[i].setFont(new Font("Arial", Font.BOLD, 72));
            CenterPanel.add(board[i]);
            board[i].addActionListener(this);
        }
        frm.add(CenterPanel, BorderLayout.CENTER);
        frm.add(LowerPanel, BorderLayout.SOUTH);

        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == reset) {

            for (int i = 0; i > 9; i++) {
                board[i].setText(" ");
                board[i].setEnabled(true);
                turn = 1;
            }

        }
        if (e.getSource() == exit) {
            System.exit(0);

        }

        for (int i = 0; i < 9; i++) {

            if (e.getSource() == board[i]) {
                if (turn == 1) {

                    board[i].setText("X");
                    board[i].setEnabled(false);
                } else {
                    board[i].setText("O");
                    board[i].setEnabled(false);

                }

                turn = (turn + 1) % 2;
                return;

            }

        }

    }

}

I want the buttons to work. if you could help me out to figure out the problem that would be a great help

Upvotes: 0

Views: 69

Answers (2)

Sam
Sam

Reputation: 162

The actionPerformed method cannot access the reset and exit variables because they being instantiated as local variables by Frame1

JButton reset = new JButton("Reset");
JButton exit = new JButton("Exit");

As @Meini suggested try

reset = new JButton("Reset");
exit = new JButton("Exit");

This will set your global variables of reset and exit equal to the desired JButtons and allow the actionPerformed method to access them.

Also, fix your loop inside of the e.getSource() == reset if statement. The reset function won't work until you have addressed the i > 9 code. Since i starts at 0, the loop will not run since 0 is not greater than 9.

Upvotes: 0

Meini
Meini

Reputation: 77

Have a look at

        JButton reset = new JButton("Reset");
        JButton exit = new JButton("Exit");

You are assigning the buttons to local variables but in actionPerformed() you are accessing object properties.

Try

        reset = new JButton("Reset");
        exit = new JButton("Exit");

Upvotes: 2

Related Questions