Here is my code. It's for a tic tac toe program and i'm trying to get my Listener to work so it knows what to display but it's throwing me an error.
Code first here it is:
import java.awt.event.*; import javax.swing.*; import java.awt.*; public class TicTacToeGame3 { public static void main(String[] args) { final int turn = 1; String Oplayer = "O"; String Xplayer = "X"; final JButton[] buttons; JFrame frame = new JFrame(); final JLabel label = new JLabel("Click New Game to Begin"); frame.add(label, BorderLayout.NORTH); //JPanel orderPanel = new JPanel(); final JButton button = new JButton("New Game"); JPanel southPanel = new JPanel(); southPanel.add(button); frame.add(southPanel, BorderLayout.SOUTH); buttons = new JButton[9]; for (int i=0; i<9; i++) { buttons[i]=new JButton(""); } JPanel buttonsPanel = new JPanel(); buttonsPanel.setLayout(new GridLayout(3, 3)); for(int i = 0; i < 9; i++) { buttonsPanel.add(buttons[i]); } frame.add(buttonsPanel, BorderLayout.CENTER); //Button Listener class ResetGame implements ActionListener { public void actionPerformed(ActionEvent event) { if (button.isSelected()); label.setText("X Starts The Game"); // buttons1.setText(""); // buttons1.setEnabled(true); } } ResetGame reset = new ResetGame(); button.addActionListener(reset); class ButtonListenerCenter implements ActionListener { public void actionPerformed(ActionEvent event) { /* if (buttons1.isSelected()); buttons1.setText("X"); buttons1.setEnabled(false); if (buttons2.isSelected()); buttons2.setText("O"); buttons2.setEnabled(false);*/ //even and odd for x's and o's //Has to be some sort of user1 and user2 click events } } //ButtonListenerCenter buttonListener2 = new ButtonListenerCenter(); //buttons.addActionListener(buttonListener2); class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { Object source = event.getSource(); for(int i=1; i<=9; i++) { if(source == buttons[i] && turn < 10) { // buttonsClicked = true; if(!(turn % 2 == 0)) buttons[i].setText("X"); else buttons[i].setText("O"); buttons[i].setEnabled(false); //pnlPlayingField.requestFocus(); //turn++; } } ActionListener clickListener = new ClickListener(); buttons.addActionListener(clickListener); } } frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.setVisible(true); } }
Here is my error that I'm getting I just don't know how to fix it and need to be pointed in the right direction
TicTacToeGame3.java:108: error: cannot find symbol
buttons.addActionListener(clickListener); This is the part I'm having trouble with if this is commented out the program will run.
^
symbol: method addActionListener(ActionListener)
location: variable buttons of type JButton[]
1 error
Working code that will compile if that part of code is commented out.
import java.awt.event.*; import javax.swing.*; import java.awt.*; public class TicTacToeGame3 { public static void main(String[] args) { final int turn = 1; String Oplayer = "O"; String Xplayer = "X"; final JButton[] buttons; JFrame frame = new JFrame(); final JLabel label = new JLabel("Click New Game to Begin"); frame.add(label, BorderLayout.NORTH); //JPanel orderPanel = new JPanel(); final JButton button = new JButton("New Game"); JPanel southPanel = new JPanel(); southPanel.add(button); frame.add(southPanel, BorderLayout.SOUTH); buttons = new JButton[9]; for (int i=0; i<9; i++) { buttons[i]=new JButton(""); } JPanel buttonsPanel = new JPanel(); buttonsPanel.setLayout(new GridLayout(3, 3)); for(int i = 0; i < 9; i++) { buttonsPanel.add(buttons[i]); } frame.add(buttonsPanel, BorderLayout.CENTER); //Button Listener class ResetGame implements ActionListener { public void actionPerformed(ActionEvent event) { if (button.isSelected()); label.setText("X Starts The Game"); // buttons1.setText(""); // buttons1.setEnabled(true); } } ResetGame reset = new ResetGame(); button.addActionListener(reset); class ButtonListenerCenter implements ActionListener { public void actionPerformed(ActionEvent event) { /* if (buttons1.isSelected()); buttons1.setText("X"); buttons1.setEnabled(false); if (buttons2.isSelected()); buttons2.setText("O"); buttons2.setEnabled(false);*/ //even and odd for x's and o's //Has to be some sort of user1 and user2 click events } } //ButtonListenerCenter buttonListener2 = new ButtonListenerCenter(); //buttons.addActionListener(buttonListener2); class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { Object source = event.getSource(); for(int i=1; i<=9; i++) { if(source == buttons[i] && turn < 10) { // buttonsClicked = true; if(!(turn % 2 == 0)) buttons[i].setText("X"); else buttons[i].setText("O"); buttons[i].setEnabled(false); //pnlPlayingField.requestFocus(); //turn++; } } ActionListener clickListener = new ClickListener(); //buttons.addActionListener(clickListener); } } frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.setVisible(true); } }
Any help is very much appreciated because I'm just not understanding and I should or need to. There is a lot of code that won't be there I'm just troubleshooting the ClickListener part at the moment. Thanks in advance.