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

  • Home
  • SEARCH
  • 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 8000647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:51:59+00:00 2026-06-04T15:51:59+00:00

Not to sure about why this code is not working as intended and hoping

  • 0

Not to sure about why this code is not working as intended and hoping you could help me figure it out. I am working on a tictactoe game from a series of tutorials that use jbuttons. What i have is a method in my main class that “checksforwin” each time a button is clicked. Using print statements i found that my method does run each time a button is clicked however the getText().equals(“_”) conditions do not operate properly.

This code is part of my tictactoe.java and all works properly. this event is copied 9 times for each button.

public void button1ActionPerformed(ActionEvent e) {
    if(button1.getText().equals("")){
        if(Main.playerTurn==true){
            button1.setText("X");
            Main.checkforwin();
            Main.playerTurn = false;
        }else{
            button1.setText("O");
            Main.checkforwin();
            Main.playerTurn = true;
        }
    }
}

This code is part of my main.java which houses the checkforwin method. The check for win chunk of code is repeated multiple times for each possible win in tictactoe for both the player 1 and computer(player2).

public class Main {

public static boolean playerTurn = true;
public static boolean playerWon = false;    
public static boolean computerWon = false;

public static tictactoe board = new tictactoe();

public static void checkforwin(){
System.out.println("testing1");
    //horizontal row 1
    if(board.button1.getText().equals("X")){
        System.out.println("testing2");
        if(board.button2.getText().equals("X")){
            if(board.button3.getText().equals("X")){
                playerWon = true;
                computerWon = false;
                System.out.println("Player 1 won");
            }
        }
    }

}

The method will output testing1 every time a button is clicked, however it will never print inside the conditions.

Any help or advice would be greatly appreciated thanks!

  • 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-04T15:52:00+00:00Added an answer on June 4, 2026 at 3:52 pm

    My guess is its not picking up the button text change – after the button text change (button1.setText("X");) etc – you need to add it to the panel again, so panel.add(button1); or whatever they’re stored on

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

Sidebar

Related Questions

I am not professional programmer so i can not be sure about this.How many
I am not sure how to go about this. Right now I am counting
I'm still not sure this is the correct way to go about this, maybe
I am not sure if I am going about this the best way, but
Ok, I'm not quite sure how to go about this. Here's the scenario. 1)
I am not sure about the exact purpose of the following Rampion's code .
I have been thinking about this issue and I can't figure it out. Perhaps
I'm not sure if I'm just form building impaired or not thinking about this
Not sure if I'm thinking about this the right way. I have a jquery
I am not sure about some things in OOP. If I have Class1 ,

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.