Purchase Solution

Design a Swing GUI

Not what you're looking for?

Ask Custom Question

Design and code a Swing GUI for a two-player tic-tac-toe game on a 3 by 3 game board. The JFrame should use a BorderLayout with a JLabel in the NORTH region to display messages (e.g. who won the game), and a JPanel in the CENTER region to display the game board. For the game board in the JPanel, use a GridLayout manager with a 3 by 3 layout of JButtons in each cell to display the game board. The button labels should initially be blank. When a player clicks on an empty button an appropriate "X" or "O" should be placed in the label field of the button. If there is a winner (three in a row) then the program should display the winner in the JLabel located at the top of the window. If all nine cells have been filled without a winner the program should indicate that there was a tie.

The declaration of the TicTacToe class specifies that it implements the ActionListener method. You should provide an implementation of the method:

public void actionPerformed(ActionEvent evt) {
// ...
}
that handles checking for a tie or winner on clicking a button.
Make sure that your implementation correctly handles the case where the user clicks a button for a cell that already contains an X or O.
--------------------------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
* Simple tic-tac-toe application.
*/
public class TicTacToe extends JFrame implements ActionListener {
// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------

// --------------------------------
// --------- END USER CODE --------
// --------------------------------
public static void main(String[] args) {
TicTacToe ttt = new TicTacToe();
ttt.setSize(400, 400);
ttt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ttt.setVisible(true);
}
}

Purchase this Solution

Solution Summary

This tutorial provides guidelines to design and code a Swing GUI for a tic-tac-toe game.

Purchase this Solution


Free BrainMass Quizzes
Javscript Basics

Quiz on basics of javascript programming language.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Basic Networking Questions

This quiz consists of some basic networking questions.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.