Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8759585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:50:36+00:00 2026-06-13T14:50:36+00:00

This tictactoe program is a 2player game. The GUI i made are frame and

  • 0

This tictactoe program is a 2player game. The GUI i made are frame and buttons after that i started coding. Actually my program is working in this kind of coding.

private String letter= " ";
private int count= 0;


    private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        count++;
                if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){

                letter = "X";

                } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){

                letter = "O";

                }

        if(evt.getSource() == btn1){

                        btn1.setText(letter);

                } else if(evt.getSource() == btn2){

                        btn2.setText(letter);

                } else if(evt.getSource() == btn3){

                        btn3.setText(letter);

                } else if(evt.getSource() == btn4){

                        btn4.setText(letter);

                } else if(evt.getSource() == btn5){

                        btn5.setText(letter);

                } else if(evt.getSource() == btn6){

                        btn6.setText(letter);

                } else if(evt.getSource() == btn7){

                        btn7.setText(letter);

                } else if(evt.getSource() == btn8){

                        btn8.setText(letter);

                } else if(evt.getSource() == btn9){

                        btn9.setText(letter);




    }         
    }                                    

    private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    count++;
                if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){

                letter = "X";

                } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){

                letter = "O";

                }

        if(evt.getSource() == btn1){

                        btn1.setText(letter);

                } else if(evt.getSource() == btn2){

                        btn2.setText(letter);

                } else if(evt.getSource() == btn3){

                        btn3.setText(letter);

                } else if(evt.getSource() == btn4){

                        btn4.setText(letter);

                } else if(evt.getSource() == btn5){

                        btn5.setText(letter);

                } else if(evt.getSource() == btn6){

                        btn6.setText(letter);

                } else if(evt.getSource() == btn7){

                        btn7.setText(letter);

                } else if(evt.getSource() == btn8){

                        btn8.setText(letter);

                } else if(evt.getSource() == btn9){

                        btn9.setText(letter);

Yeah, it is actually working but my problem with this codes is I need to put the codes in every button e.g. button7, button1 (these just the example of my buttons) which is the codes are the action performed of the button.

I want only a single command that each button perform and I dont want to copy paste in every buttons because they are identical codes. I think there is a code to do that, what do you think? Please help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:50:37+00:00Added an answer on June 13, 2026 at 2:50 pm

    So basically, you want to collapse you logic into a single handler. This handler needs a single reference to the button that it is acting on…

    Something like…

    public class ActionHandler implements ActionListener {
    
        private JButton master;
    
        public ActionHandler(JButton master) {
            this.master = master;
        }
    
        protected void setText(String text) {
            master.setText(text);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
    
            String text = null;
    
            count++;
            if (count / 2f == Math.round(count / 2f)) {
                text = "O";
            } else {
                text = "X";
            }
    
            setText(text);
    
        }
    
    }
    

    Now you setup code may be different then this, this is just an example…

    buttons = new JButton[9]; // You can use this to reset the board ;)
    setLayout(new GridLayout(3, 3));
    for (int index = 0; index < 9; index++) {
        JButton btn = new JButton(Integer.toString(index));
        buttons[index] = btn;
        btn.addActionListener(new ActionHandler(btn));
        add(btn);
    }
    

    The basic idea is that as you create a new button, you assign it’s own ActionHandler passing it a reference of the button. This allows the action handler to control the button based on the state of the game.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a TicTacToe Game! Just for fun. It works and all, but can't
I'm working on an ASP.NET based TicTacToe game. The problem I have with it
I built a TicTacToe game for android. And i want that there should be
This may be a wild strange dream. I dreampt that I could put a
So I'm working on a small TicTacToe application over the holidays to improve some
This is my first time creating a PHP form that will run a MySQL
Not to sure about why this code is not working as intended and hoping
This must be a very simple solution that has eluded me this last hour.
This is a super simple issue that I can't figure out. I want to
I'm making a tictactoe game where you can play against another person or an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.