Search code examples
javainheritancejbutton

How do I use a JButton in more than 1 method?


So I've been coding my TicTacToe project as a side project for some time now, and have hit another obstacle. I want to use a JButton in more than one method, but I don't know how to go about doing that. Here's my code till now.

`

import java.util.Random ;
import java.util.Scanner ;
import javax.swing.JOptionPane ;
import javax.swing.JFrame ;
import javax.swing.JPanel ;
import java.util.InputMismatchException ;
import java.awt.BorderLayout ;
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.JTextArea ;
import javax.swing.JButton ;
import javax.swing.JRadioButton ;

class TicTacToe 
 {
    public int count = 1 ; 
    public String letter;
    public boolean b1bool = true ;
    public boolean b2bool = true ;
    public boolean b3bool = true ;
    public boolean b4bool = true ;
    public boolean b5bool = true ;
    public boolean b6bool = true ;
    public boolean b7bool = true ;
    public boolean b8bool = true ;
    public boolean b9bool = true ;
     public boolean win = false ;


    public void main(String []args) 
    {

        popupintroscreen();

    }
    public void popupintroscreen()
    {

        JTextArea introtext = new JTextArea("Welcome to TicTacToe v1.0. This is a Simple Tic Tac Toe app coded  by Abhishek Pisharody. Press the button below to play, or view the        instructions first, if you prefer. We hope you enjoy playing. Thank you.");
        introtext.setEditable(false);
        introtext.setLineWrap(true);

        JButton startgamebutton = new JButton("Start Game");
        startgamebutton.setToolTipText("Start a game of Tic-Tac-Toe");
        startgamebutton.addActionListener(new ActionListener()
        {
           @Override
           public void actionPerformed(ActionEvent gamestart)
           {
            JOptionPane.showMessageDialog(null, "Loading.....done!");
            tictactoe();
           }
        });

        JButton showinstructions = new JButton("Show Instructions");
        showinstructions.setToolTipText("View game instructions. Worth checking out even if you know how to play.");
        showinstructions.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent displayinstructionsprompt)
            {
              JOptionPane.showMessageDialog(null, "Nothing to see here..yet..off you go!");  
            }
        });

        JButton highscoresbutton = new JButton("High Scores");
        highscoresbutton.setToolTipText("Show high scores for the game");
        highscoresbutton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent highscoresbuttonclicked)
            {
                JOptionPane.showMessageDialog(null,"Not coded yet!");
            }
        });

        JButton quitgamebutton = new JButton("Quit Game");
        quitgamebutton.setToolTipText("Quit the game. But why? :(");
        quitgamebutton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent onquitgamebuttonclick)
            {
                JOptionPane.showConfirmDialog(null, "Really quit?");
                System.exit(0);
            }
        });


        JPanel gamebuttonsholder = new JPanel(new BorderLayout());
        gamebuttonsholder.setSize(400,100);
        gamebuttonsholder.add(introtext,BorderLayout.PAGE_START);
        gamebuttonsholder.add(startgamebutton,BorderLayout.PAGE_END);
        gamebuttonsholder.add(showinstructions,BorderLayout.LINE_START);
        gamebuttonsholder.add(highscoresbutton,BorderLayout.CENTER);
        gamebuttonsholder.add(quitgamebutton,BorderLayout.LINE_END);


        JFrame introscreen = new JFrame("Tic Tac Toe");
        introscreen.setSize(400,400);
        introscreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        introscreen.setLocationRelativeTo(null);
        introscreen.add(gamebuttonsholder);
        introscreen.setVisible(true);
    }

        public int tictactoe()
        {

            final JButton b1 = new JButton("");
            b1.setToolTipText("Mark this box");
            b1.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent onclickb1)
                {
                   if (b1bool == true){ count++;
                    if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
                    {
                      letter = "O" ;

                }
                else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
                {
                    letter = "X" ;                  

                }
                b1.setText(letter);
                b1bool = false ;
                calculatevictory();
                processturn();

            }}});

            final JButton b2 = new JButton("");
            b2.setToolTipText("Mark this box");
            b2.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent onclickb1)
                {
                   if (b2bool == true){ count++;
                    if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
                    {
                      letter = "O" ;

                }
                else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
                {
                    letter = "X" ;                  

                }
                b2.setText(letter);
                b2bool = false ;
                calculatevictory();
                processturn();
            }}
                });


            final JButton b3 = new JButton("");
            b3.setToolTipText("Mark this box");
            b3.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent onclickb1)
                {
                     if(b3bool == true){
                         count++;
                    if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
                    {
                      letter = "O" ;

                }
                else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
                {
                    letter = "X" ;                  

                }
                b3.setText(letter);
                b3bool = false ;
                calculatevictory();
                processturn();
            }}

            });

            final JButton b4 = new JButton("");
            b4.setToolTipText("Mark this box");
            b4.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent onclickb1)
                {
                    if(b4bool == true){

                    count++;
                        if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
                    {
                      letter = "O" ;

                }
                else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
                {
                    letter = "X" ;                  

                }
                    b4.setText(letter);
                    b4bool = false ;
                    calculatevictory();
                    processturn();
                }}
            });

            final JButton b5 = new JButton("");
            b5.setToolTipText("Mark this box");
            b5.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent onclickb1)
                {
                    if (b5bool == true){
                        count++;
                        if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
                    {
                      letter = "O" ;

                }
                else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
                {
                    letter = "X" ;                  

                }
                    b5.setText(letter);
                    b5bool = false ;
                    calculatevictory();
                    processturn();
                }}
            });

            final JButton b6 = new JButton("");
            b6.setToolTipText("Mark this box");
            b6.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent onclickb1)
                {
                    if (b6bool == true){
                        count++;
                        if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
                    {
                      letter = "O" ;

                }
                else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
                {
                    letter = "X" ;                  

                }
                    b6.setText(letter);
                    b6bool = false ;
                    calculatevictory();
                    processturn();
                }}
            });

            final JButton b7 = new JButton("");
            b7.setToolTipText("Mark this box");
            b7.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent onclickb1)
                {
                    if (b7bool == true){
                        count++;
                        if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
                    {
                      letter = "O" ;

                }
                else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
                {
                    letter = "X" ;                  

                }
                    b7.setText(letter);
                    b7bool = false ;
                    calculatevictory();
                    processturn();
                }}
            });

            final JButton b8 = new JButton("");
            b8.setToolTipText("Mark this box");
            b8.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent onclickb1)
                {
                    if(b8bool == true){
                        count++;
                        if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
                    {
                      letter = "O" ;

                }
                else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
                {
                    letter = "X" ;                  

                }
                    b8.setText(letter);
                    b8bool = false ;
                    calculatevictory();
                    processturn();
                }}
            });

            final JButton b9 = new JButton("");
            b9.setToolTipText("Mark this box");
            b9.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent onclickb1)
                {
                   if(b9bool == true){
                       count++;
                        if(count == 1||count == 3 || count == 5 || count == 7 || count == 9 || count == 11)
                    {
                      letter = "O" ;

                }
                else if ( count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
                {
                    letter = "X" ;                  

                }
                    b9.setText(letter);
                    b9bool = false ;
                    calculatevictory();
                    processturn();
                }}
            });

            GridLayout gamescreenlayout = new GridLayout(3,3);

            JPanel gamescreencontent = new JPanel();
            gamescreencontent.setLayout(gamescreenlayout);
            gamescreencontent.setSize(400,400);
            gamescreencontent.add(b1);
            gamescreencontent.add(b2);
            gamescreencontent.add(b3);
            gamescreencontent.add(b4);
            gamescreencontent.add(b5);
            gamescreencontent.add(b5);
            gamescreencontent.add(b6);
            gamescreencontent.add(b7);
            gamescreencontent.add(b8);
            gamescreencontent.add(b9);



            JFrame gamescreen = new JFrame("Tic-Tac-Toe");
            gamescreen.setSize(400,400) ;
            gamescreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gamescreen.setLocationRelativeTo(null);
            gamescreen.add(gamescreencontent);
            gamescreen.setVisible(true);

            int sexyshit = 1 ;
            return sexyshit ;
        }

        public void calculatevictory(){
            if (b1.getText() == b2.getText() && b2.getText() == b3.getText() && b1.getText() != "")
            {
                win = true ;
            }
            else if (b4.getText() == b5.getText() && b5.getText() == b6.getText() && b4.getText() != "")
            {
                win = true ;
            }
            else if (b7.getText() == b8.getText() && b8.getText() == b9.getText() && b7.getText() != "")
            {
                win = true ;
            }
            else if (b1.getText() == b4.getText() && b4.getText() == b7.getText() && b1.getText() != "")
            {
                win = true ;
            }
            else if (b2.getText() == b5.getText() && b5.getText() == b8.getText() && b2.getText() != "")
            {
                win = true ;
            }
            else if (b3.getText() == b6.getText() && b6.getText() == b9.getText() && b3.getText() != "")
            {
                win = true ;
            }
            else if (b1.getText() == b5.getText() && b5.getText() == b9.getText() && b1.getText() != "")
            {
                win = true ;
            }
            else if (b3.getText() == b5.getText() && b5.getText() == b7.getText() && b3.getText() != "")
            {
                win = true ;
            }
            else 
            {
                win = false ;
            }
        }




            public void processturn()
            {
            if (win == true)
            {
                JOptionPane.showMessageDialog(null, letter + "wins!");
            }
            else if ( count == 9 && win == false)
            {
                JOptionPane.showMessageDialog(null, "Tie Game");
            }
        }














    }`

When I try to run this, it tells me that the compiler couldn't find symbol b1. Would I have to make the buttons public(if so, how?), or use Inheritance? If it's the latter, please explain in simple terms, because I really haven't begun to learn inheritance yet.

Thank you in advance.


Solution

  • Declare your JButton outside the method, in the same place as where you are declaring your int, String, and booleans. In other words, something like

    class TicTacToe
    {
        //...
        JButton b1;
        //...
        public int TicTacToe()
        {
            b1 = new JButton(); //note that you don't need the ""
        }
    }
    

    Incidentally, I'd recommend using arrays and looping through them rather than having 9 of everything and having to copy-paste code lots of times.