Prompt: Using your knowledge of Java GUI components create a tic-tac-toeprogram.
ID: 3615573 • Letter: P
Question
Prompt:Using your knowledge of Java GUI components create a tic-tac-toeprogram. For the text you may use an X/O or a 0/1.
The program must check across, diagonally, and down. If thereis no winner display this to the user.
Once a winner or no winner is determined, reset the game foranother play.
Note: Driver class in finished. Only the gameboard class needs tobe modified with several methods, such as buttonListener. I alreadyhave made the board layout to 3X3. All I need to do is allow theuser to be able to click the buttons and let the program check up,down and across and give feedback. Thanks!
______________________________________________________________________________
Driver Class:
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.event.*;
public class TicTacToeDriver {
/*
* The main method acts as a gamedriver. Panels are created
* in GamePlay class methods and returnedto the driver.
* The panels are added to the frame andthen displayed. All
* of the game is carried out via theGamePlay class.
*/
public static void main(String[] args)
{
GameBoard b = new GameBoard();
JFrame frame = new JFrame (" TicTac Toe Board");
frame.getContentPane().add(b, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack ();
frame.setVisible (true);
}
}
______________________________________________________________________________
Gameboard class:
import java.awt.*;
import javax.swing.*;
public class GameBoard extends JPanel
{
private JButton push;
private JLabel label;
public GameBoard()
{
setLayout (new GridLayout(3,3));
setBackground(Color.blue);
JButton b1 = new JButton(" ");
JButton b2 = new JButton(" ");
JButton b3 = new JButton(" ");
JButton b4 = new JButton(" ");
JButton b5 = new JButton(" ");
JButton b6 = new JButton(" ");
JButton b7 = new JButton(" ");
JButton b8 = new JButton(" ");
JButton b9 = new JButton(" ");
add (b1);
add (b2);
add (b3);
add (b4);
add (b5);
add (b6);
add (b7);
add (b8);
add (b9);
}
}
public PushPanel()
{
push.addActionListener(newButtonListener());
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEventevent)
{
if (event.getSource() ==left)
{
label.setText("Left");
}
else
{
label.setText("Right");
}
}
} BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack ();
frame.setVisible (true);
}
}
______________________________________________________________________________
Gameboard class:
import java.awt.*;
import javax.swing.*;
public class GameBoard extends JPanel
{
private JButton push;
private JLabel label;
public GameBoard()
{
setLayout (new GridLayout(3,3));
setBackground(Color.blue);
JButton b1 = new JButton(" ");
JButton b2 = new JButton(" ");
JButton b3 = new JButton(" ");
JButton b4 = new JButton(" ");
JButton b5 = new JButton(" ");
JButton b6 = new JButton(" ");
JButton b7 = new JButton(" ");
JButton b8 = new JButton(" ");
JButton b9 = new JButton(" ");
add (b1);
add (b2);
add (b3);
add (b4);
add (b5);
add (b6);
add (b7);
add (b8);
add (b9);
}
}
public PushPanel()
{
push.addActionListener(newButtonListener());
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEventevent)
{
if (event.getSource() ==left)
{
label.setText("Left");
}
else
{
label.setText("Right");
}
}
}
Explanation / Answer
x.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.