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 9068291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:09:24+00:00 2026-06-16T17:09:24+00:00

Suppose there is an array of the name buttons: private JButton buttons[] = new

  • 0

Suppose there is an array of the name buttons:
private JButton buttons[] = new JButton[9];
How can I set the color of all the buttons in this array to blue?

This is my entire code: It is a game of Tic Tac Toe with the use of buttons.

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

public class TicTacToe implements ActionListener {
private JButton buttons[] = new JButton[9];
private JFrame window = new JFrame("Tic Tac Toe");
private boolean win = false;
private int count = 0;
private int Xwins = 0, Owins = 0;
private String letter = "";
private int[][] winCombinations = new int[][] {
        {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
        {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //vertical wins
        {0, 4, 8}, {2, 4, 6}                     //diagonal wins
};
String name1 = JOptionPane.showInputDialog("Please enter first player's name");
String name2 = JOptionPane.showInputDialog("Please enter second player's name");

public TicTacToe(){
    JOptionPane.showMessageDialog(null,"Remember Player 1 is X and Player 2 is O.");
    window.setSize(300,300);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLayout(new GridLayout(3,3));
    window.setVisible(true);


      for(int i=0; i<=8; i++){
          buttons[i] = new JButton();
          window.add(buttons[i]);
          buttons[i].addActionListener(this);
          buttons[i].setBackground(Color.MAGENTA);
  }
      for (JButton button: buttons) {
           button.setBackground(Color.BLUE);
        }

}

public void actionPerformed(ActionEvent event) {
        count++;
        if(count % 2 == 0){
            letter = "O";

        }else{
            letter = "X";
        }
         JButton pressedButton = (JButton)event.getSource(); 
         pressedButton.setText(letter);
         pressedButton.setEnabled(false);
         pressedButton.setBackground(Color.WHITE);


    //Determine who won
    for(int i=0; i<=7; i++){
        if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) && 
                buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) && 
                buttons[winCombinations[i][0]].getText() != ""){
                win = true;
            }
        }
        if(win == true){
            if(letter == "X"){
                JOptionPane.showMessageDialog(null, name1 + " wins the game!");
            }else{
                JOptionPane.showMessageDialog(null, name2 + " wins the game!");
            }
            playAgain();
        }else if(count == 9 && win == false){
            JOptionPane.showMessageDialog(null, "The game is tied!");
            playAgain();
        }
    } 

public void playAgain(){
    if(letter == "X"){
        Xwins++;
    }else{
        Owins++;
    }
    JOptionPane.showMessageDialog(null, name1 + " has won this many times: " + Xwins);
    JOptionPane.showMessageDialog(null, name2 + " has won this many times: " + Owins);
    int response = JOptionPane.showConfirmDialog(null, "Would you like to play again?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

    if(response == JOptionPane.YES_OPTION){
        reset();
    }else{
        System.exit(0);
    }
}

  public void reset() {
      for(int i = 0; i<=8; i++) {
              buttons[i].setText("");
              buttons[i].setEnabled(true); 
      }
      win = false;
      count = 0;
  }  

public static void main(String[] args){
    TicTacToe play = new TicTacToe();
   }
}
  • 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-16T17:09:25+00:00Added an answer on June 16, 2026 at 5:09 pm

    You need to reset the button color to blue in the reset method’s for loop:

    buttons[i].setBackground(Color.blue);
    

    Here’s what the resulting application looks like on my machine and a Mac OSX machine. The buttons change to white after being selected:

    enter image description here  
    enter image description here

    If you’re on Mac OSX, you may also be having problems with the system Look & Feel. You can change this in your main method like this:

    public static void main(String[] args){
       try {
          // Set cross-platform Java L&F (also called "Metal")
          UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
          // alternatively, the following should load the default L&F for your system
          //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       } catch (Exception e) {}
    
       TicTacToe play = new TicTacToe();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't figure out how to create an array with all matches. I suppose I
Suppose I have this array of hashes: [ {href=>https://company.campfirenow.com, name=>Company, id=>123456789, product=>campfire}, {href=>https://basecamp.com/123456789/api/v1, name=>Company,
suppose there is a string of characters in an array of d[20] . How
Suppose there is a System A which gives some output. This output is used
Suppose I have a very large array of information for a user: $user=array( name=>john,
There is a variable, suppose item . It is an array holding two values
Suppose I am looping an array like this: foreach($cursor as $obj) { ... }
Suppose that we have this code (simplified example): $propertyName = 'items'; $foo = new
Suppose there is a text file containing these in the fallowing style: name: natalie,
Suppose i have a array like this : Array( '1' => Array( ID =>

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.