Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is my code for the program, but for some reason I cannot get the label to d

ID: 3545710 • Letter: H

Question

Here is my code for the program, but for some reason I cannot get the label to display the correct string. Also I need to implement an actionListener with a Boolean to determine if a button is already pushed (so it can't be pushed again). I'm stumped, so any help would be great.


package tictactoe;


import java.awt.*;

import javax.swing.*;

import java.awt.event.*;


public class TicTacToePanel extends JPanel {

    

private JPanel ttt, text, primary;

private JLabel label;

private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9;

private int count = 0, bval;

private String letter = "";

    

public TicTacToePanel() {

    

    b1 = new JButton();

    b1.addActionListener(new ButtonListener());

    b2 = new JButton();

    b2.addActionListener(new ButtonListener());

    b3 = new JButton();

    b3.addActionListener(new ButtonListener());

    b4 = new JButton();

    b4.addActionListener(new ButtonListener());

    b5 = new JButton();

    b5.addActionListener(new ButtonListener());

    b6 = new JButton();

    b6.addActionListener(new ButtonListener());

    b7 = new JButton();

    b7.addActionListener(new ButtonListener());

    b8 = new JButton();

    b8.addActionListener(new ButtonListener());

    b9 = new JButton();

    b9.addActionListener(new ButtonListener());


    

    JPanel ttt = new JPanel();

         ttt.setPreferredSize (new Dimension(400, 200));

         ttt.setBackground (Color.yellow);

         //JLabel label1 = new JLabel ("");

         //ttt.add (label1);

         ttt.setLayout (new GridLayout (3, 3));

         ttt.add(b1);

         ttt.add(b2);

         ttt.add(b3);

         ttt.add(b4);

         ttt.add(b5);

         ttt.add(b6);

         ttt.add(b7);

         ttt.add(b8);

         ttt.add(b9);

         

   JPanel text = new JPanel();

         text.setPreferredSize (new Dimension(400, 50));

         text.setBackground (Color.blue);

         label = new JLabel ("");

         text.add (label);

         

         

   JPanel primary = new JPanel();

         primary.setLayout (new BoxLayout (primary, BoxLayout.Y_AXIS ));

         primary.add(ttt);

         primary.add(text);

         add(primary);  

}


private class ButtonListener implements ActionListener

{

    public void actionPerformed (ActionEvent event)

    {

        label.setText("A button was pushed.");

  /*Display X's or O's on the buttons*/

if(event.getSource() == b1) bval = 1;

    else if(event.getSource() == b2) bval = 2;

    else if(event.getSource() == b3) bval = 3;

    else if(event.getSource() == b4) bval = 4;

    else if(event.getSource() == b5) bval = 5;

    else if(event.getSource() == b6) bval = 6;

    else if(event.getSource() == b7) bval = 7;

    else if(event.getSource() == b8) bval = 8;

    else if(event.getSource() == b9) bval = 9;      


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";

}


    switch(bval)

        {

            case 1:

                if(count%2 == 0){

                b1.setText("O");

                label.setText("Place an X on a blank square by clicking on an empty square");

                }else

                    b1.setText("X");

                    label.setText("Place an O on a blank square by clicking on an empty square");

                break;

            case 2:

                if(count%2 == 0){

                b2.setText("O");

                label.setText("Place an X on a blank square by clicking on an empty square");

                }else

                    b2.setText("X");

                    label.setText("Place an O on a blank square by clicking on an empty square");

                break;

            case 3:

                if(count%2 == 0){

                b3.setText("O");

                label.setText("Place an X on a blank square by clicking on an empty square");

                }else

                    b3.setText("X");

                    label.setText("Place an O on a blank square by clicking on an empty square");

                break;

            case 4:

                if(count%2 == 0){

                b4.setText("O");

                label.setText("Place an X on a blank square by clicking on an empty square");

                }else

                    b4.setText("X");

                    label.setText("Place an O on a blank square by clicking on an empty square");

                break;

            case 5:

                 if(count%2 == 0){

                b5.setText("O");

                label.setText("Place an X on a blank square by clicking on an empty square");

                }else

                    b5.setText("X");

                    label.setText("Place an O on a blank square by clicking on an empty square");

                break;

            case 6:

                if(count%2 == 0){

                b6.setText("O");

                label.setText("Place an X on a blank square by clicking on an empty square");

                }else

                    b6.setText("X");

                    label.setText("Place an O on a blank square by clicking on an empty square");

                break;

            case 7:

                if(count%2 == 0){

                b7.setText("O");

                label.setText("Place an X on a blank square by clicking on an empty square");

                }else

                    b7.setText("X");

                    label.setText("Place an O on a blank square by clicking on an empty square");

                break;

            case 8:

                if(count%2 == 0){

                b8.setText("O");

                label.setText("Place an X on a blank square by clicking on an empty square");

                }else

                    b8.setText("X");

                    label.setText("Place an O on a blank square by clicking on an empty square");

                break;

            case 9:

                if(count%2 == 0){

                b9.setText("O");

                label.setText("Place an X on a blank square by clicking on an empty square");

                }else

                    b9.setText("X");

                    label.setText("Place an O on a blank square by clicking on an empty square");

                break;

                  

        }  



    }

}

}

Explanation / Answer

Made some additions to the code. Label is changing now but I dont think that the logic for determination of winner is implemented yet. Let me know if u need that. Please rateif this answer works for u



import java.awt.Color;

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;


public class TicTacToePanel {


private JFrame frame;

private JPanel ttt, text, primary;

private JLabel label;

private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9;

private int count = 0, bval;

private String letter = "";


/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

TicTacToePanel window = new TicTacToePanel();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}


/**

* Create the application.

*/

public TicTacToePanel() {

initialize();

}


/**

* Initialize the contents of the frame.

*/

private void initialize() {

frame = new JFrame();

frame.setBounds(100, 100, 450, 300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




b1 = new JButton();

b1.addActionListener(new ButtonListener());

b2 = new JButton();

b2.addActionListener(new ButtonListener());

b3 = new JButton();

b3.addActionListener(new ButtonListener());

b4 = new JButton();

b4.addActionListener(new ButtonListener());

b5 = new JButton();

b5.addActionListener(new ButtonListener());

b6 = new JButton();

b6.addActionListener(new ButtonListener());

b7 = new JButton();

b7.addActionListener(new ButtonListener());

b8 = new JButton();

b8.addActionListener(new ButtonListener());

b9 = new JButton();

b9.addActionListener(new ButtonListener());


ttt = new JPanel();

ttt.setPreferredSize (new Dimension(400, 200));

ttt.setBackground (Color.yellow);

//JLabel label1 = new JLabel ("");

//ttt.add (label1);

ttt.setLayout (new GridLayout (3, 3));

ttt.add(b1);

ttt.add(b2);

ttt.add(b3);

ttt.add(b4);

ttt.add(b5);

ttt.add(b6);

ttt.add(b7);

ttt.add(b8);

ttt.add(b9);

text = new JPanel();

text.setPreferredSize (new Dimension(400, 50));

text.setBackground (Color.blue);

label = new JLabel ("");

text.add (label);

primary = new JPanel();

primary.setLayout (new BoxLayout (primary, BoxLayout.Y_AXIS ));

primary.add(ttt);

primary.add(text);

frame.add(primary);

}

private class ButtonListener implements ActionListener

{

public void actionPerformed (ActionEvent event)

{

label.setText("A button was pushed.");

/*Display X's or O's on the buttons*/

if(event.getSource() == b1) bval = 1;

else if(event.getSource() == b2) bval = 2;

else if(event.getSource() == b3) bval = 3;

else if(event.getSource() == b4) bval = 4;

else if(event.getSource() == b5) bval = 5;

else if(event.getSource() == b6) bval = 6;

else if(event.getSource() == b7) bval = 7;

else if(event.getSource() == b8) bval = 8;

else if(event.getSource() == b9) bval = 9;


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";

}


switch(bval)

{

case 1:

if(count%2 == 0){

b1.setText("O");

label.setText("Place an X on a blank square by clicking on an empty square");

}else

b1.setText("X");

label.setText("Place an O on a blank square by clicking on an empty square");

break;

case 2:

if(count%2 == 0){

b2.setText("O");

label.setText("Place an X on a blank square by clicking on an empty square");

}else

b2.setText("X");

label.setText("Place an O on a blank square by clicking on an empty square");

break;

case 3:

if(count%2 == 0){

b3.setText("O");

label.setText("Place an X on a blank square by clicking on an empty square");

}else

b3.setText("X");

label.setText("Place an O on a blank square by clicking on an empty square");

break;

case 4:

if(count%2 == 0){

b4.setText("O");

label.setText("Place an X on a blank square by clicking on an empty square");

}else

b4.setText("X");

label.setText("Place an O on a blank square by clicking on an empty square");

break;

case 5:

if(count%2 == 0){

b5.setText("O");

label.setText("Place an X on a blank square by clicking on an empty square");

}else

b5.setText("X");

label.setText("Place an O on a blank square by clicking on an empty square");

break;

case 6:

if(count%2 == 0){

b6.setText("O");

label.setText("Place an X on a blank square by clicking on an empty square");

}else

b6.setText("X");

label.setText("Place an O on a blank square by clicking on an empty square");

break;

case 7:

if(count%2 == 0){

b7.setText("O");

label.setText("Place an X on a blank square by clicking on an empty square");

}else

b7.setText("X");

label.setText("Place an O on a blank square by clicking on an empty square");

break;

case 8:

if(count%2 == 0){

b8.setText("O");

label.setText("Place an X on a blank square by clicking on an empty square");

}else

b8.setText("X");

label.setText("Place an O on a blank square by clicking on an empty square");

break;

case 9:

if(count%2 == 0){

b9.setText("O");

label.setText("Place an X on a blank square by clicking on an empty square");

}else

b9.setText("X");

label.setText("Place an O on a blank square by clicking on an empty square");

break;

}



}

}



}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote