oi da uma olhda nesse codigo do jogo da velha aqui, acho que vai te ajudar bastante
package velha;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class JogoDaVelha extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public JButton[][] botoes = { { null, null, null }, { null, null, null },
{ null, null, null } };
public String jdv = "X";
public JPanel p;
public int control = 0, control_1 = 0;
public int numLinhas = botoes[0].length;
public int numColunas = numLinhas;
public boolean acabou = false;
public ActionEvent myEvent;
public boolean empate = false;
public JLabel od;
public Container container;
public JMenu jogo, ajuda;
public JMenuItem novo, sair, sobre;
public JMenuBar menu;
public int contLV = 0, contCV = 0, indice = 0;
public String vencedor = "";
public JogoDaVelha() {
container = this.getContentPane();
setTitle("Jogo Da Velha");
p = new JPanel();
p.setLayout(new GridLayout(3, 3));
od = new JLabel(new ImageIcon("OutDoor.jpeg"));
container.add(od, BorderLayout.CENTER);
Toolkit tk = Toolkit.getDefaultToolkit();
setIconImage(tk.getImage("iconJDV.jpeg"));
menu = new JMenuBar();
jogo = new JMenu("Jogo");
novo = new JMenuItem("Novo Jogo...");
novo.addActionListener(this);
jogo.add(novo);
jogo.addSeparator();
sair = new JMenuItem("Sair");
sair.addActionListener(this);
jogo.add(sair);
ajuda = new JMenu("Ajuda");
sobre = new JMenuItem("Sobre o Jogo da Velha Plus 0.1 - JVP");
sobre.addActionListener(this);
ajuda.add(sobre);
menu.add(jogo);
menu.add(ajuda);
setJMenuBar(menu);
for (int j = 0; j < numLinhas; j++) {
for (int i = 0; i < botoes[j].length; i++) {
botoes[j][i] = new JButton("");
botoes[j][i].setBackground(Color.white);
botoes[j][i].setFont(new Font("Times new Romam", Font.PLAIN,
120));
botoes[j][i].setToolTipText("Casa número: "
+ String.valueOf(i + 1) + " da linha "
+ String.valueOf(j + 1));
botoes[j][i].addActionListener(this);
p.add(botoes[j][i]);
}
}
setBounds(250, 100, 560, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}
public void actionPerformed(ActionEvent e) {
this.myEvent = e;
if (e.getSource() == novo) {
remove(od);
setBounds(200, 50, 650, 550);
container.add(p, BorderLayout.CENTER);
acabou = false;
limpar();
setVisible(true);
} else if (e.getSource() == sair) {
System.exit(0);
} else if (e.getSource() == sobre) {
JOptionPane
.showMessageDialog(
null,
"JOGO DA VELHA PLUS 0.1\n\nCopyright © 2005.1 - 2005.2 "
+ "\nPowered by Wesllhey Holanda\nCONTATO\n"
+ "E-Mail: [email removido]"
+ "\nMSN: [email removido]\nHome - "
+ "\nFone: 0 XX ([telefone removido]\n\nTodos os Direitos Reservados!!!",
"[.:Sobre o JDV PLUS 0.1:.]",
JOptionPane.INFORMATION_MESSAGE);
} else {
configJDV();
}
}
public void configJDV() {
int cont = 0;
if (!acabou) {
for (int i = 0; i < botoes.length; i++) {
for (int j = 0; j < botoes[i].length; j++) {
if (myEvent.getSource() == botoes[i][j]) {
if (!botoes[i][j].getText().equals("")) {
JOptionPane.showMessageDialog(this,
"Jogue em outra posição...",
"[.:POSIÇÃO JÁ OCUPADA!!!:.]",
JOptionPane.ERROR_MESSAGE);
} else {
if (this.jdv == "X") {
this.jdv = "0";
botoes[i][j].setForeground(Color.blue);
}
else {
this.jdv = "X";
botoes[i][j].setForeground(Color.red);
}
botoes[i][j].setText(this.jdv);
vencedor = this.jdv;
}
}
}
}
/*********************************************************
* VERIFICA LINHAS
*********************************************************/
if (
botoes[0][0].getText().equals(botoes[0][1].getText())
&& botoes[0][1].getText().equals(botoes[0][2].getText())
&& !botoes[0][0].getText().equals("")) {
pintarLinha(0, vencedor);
}
else if (
botoes[1][0].getText().equals(botoes[1][1].getText())
&& botoes[1][1].getText().equals(botoes[1][2].getText())
&& !botoes[1][0].getText().equals("")) {
pintarLinha(1, vencedor);
}
else if (
botoes[2][0].getText().equals(botoes[2][1].getText())
&& botoes[2][1].getText().equals(botoes[2][2].getText())
&& !botoes[2][0].getText().equals("")) {
pintarLinha(2, vencedor);
}
/*********************************************************
* VERIFICA LINHAS
*********************************************************/
else if (
botoes[0][0].getText().equals(botoes[1][0].getText())
&& botoes[1][0].getText().equals(botoes[2][0].getText())
&& !botoes[0][0].getText().equals("")) {
pintarColuna(0, vencedor);
}
else if (
botoes[0][1].getText().equals(botoes[1][1].getText())
&& botoes[1][1].getText().equals(botoes[2][1].getText())
&& !botoes[0][1].getText().equals("")) {
pintarColuna(1, vencedor);
}
else if (
botoes[0][2].getText().equals(botoes[1][2].getText())
&& botoes[1][2].getText().equals(botoes[2][2].getText())
&& !botoes[0][2].getText().equals("")) {
pintarColuna(2, vencedor);
} else if (
botoes[0][0].getText().equals(botoes[1][1].getText())
&& botoes[1][1].getText().equals(botoes[2][2].getText())
&& !botoes[0][0].getText().equals("")) {
pintarDiagonalPrinc();
} else if (
botoes[2][0].getText().equals(botoes[0][2].getText())
&& botoes[0][2].getText().equals(botoes[1][1].getText())
&& !botoes[2][0].getText().equals("")) {
pintarDiagonalSec();
} else {
for (int i = 0; i < botoes.length; i++) {
for (int j = 0; j < botoes.length; j++) {
if (!botoes[i][j].getText().equals("")) {
cont++;
}
}
if (cont == 9) {
empatar();
}
}
}
} else {
JOptionPane.showMessageDialog(this, "[.:Reinicie o Jogo...:.]",
"JOGO FINALIZADO!", JOptionPane.ERROR_MESSAGE);
}
}
public void empatar() {
this.acabou = true;
for (int i = 0; i < botoes.length; i++) {
for (int j = 0; j < botoes[i].length; j++) {
botoes[i][j].setBackground(Color.black);
botoes[i][j].setForeground(Color.gray);
}
}
JOptionPane.showMessageDialog(this, "[.:Jogo Acabou empatado!:.]",
"EMPATE", JOptionPane.WARNING_MESSAGE);
}
public void pintarLinha(int indice, String vencedor) {
this.acabou = true;
for (int i = 0; i < botoes[indice].length; i++) {
botoes[indice][i].setBackground(Color.blue);
botoes[indice][i].setForeground(Color.white);
}
parabéns();
}
public void pintarColuna(int indice, String vencedor) {
this.acabou = true;
for (int i = 0; i < botoes[indice].length; i++) {
botoes[i][indice].setBackground(Color.blue);
botoes[i][indice].setForeground(Color.white);
}
parabéns();
}
public void pintarDiagonalPrinc() {
this.acabou = true;
for (int i = 0; i < botoes.length; i++) {
for (int j = 0; j < botoes[1].length; j++) {
if (j == i && j == 1) {
botoes[i - 1][j - 1].setBackground(Color.blue);
botoes[i][j].setBackground(Color.blue);
botoes[i + 1][j + 1].setBackground(Color.blue);
botoes[i - 1][j - 1].setForeground(Color.white);
botoes[i][j].setForeground(Color.white);
botoes[i + 1][j + 1].setForeground(Color.white);
}
}
}
parabéns();
}
public void pintarDiagonalSec() {
this.acabou = true;
// Pinta a diagonal principal
for (int i = 0; i < botoes.length; i++) {
for (int j = 0; j < botoes[i].length; j++) {
if (j == i && j == 1) {
botoes[0][i * 2].setBackground(Color.blue);
botoes[i * 2][0].setBackground(Color.blue);
botoes[i][j].setBackground(Color.blue);
botoes[0][i * 2].setForeground(Color.white);
botoes[i * 2][0].setForeground(Color.white);
botoes[i][j].setForeground(Color.white);
}
}
}
parabéns();
}
public boolean estáAcabado() {
if (acabou)
return acabou;
else
acabou = false;
return acabou;
}
public void parabéns() {
JOptionPane.showMessageDialog(this, vencedor + " Ganhou!",
"[.:PARABÉNS:.]", JOptionPane.WARNING_MESSAGE);
}
public void limpar() {
for (int i = 0; i < botoes.length; i++) {
for (int j = 0; j < botoes[i].length; j++) {
botoes[i][j].setText("");
botoes[i][j].setBackground(Color.white);
}
}
}
public void setJDV() {
if (this.control % 2 == 0) {
this.jdv = "X";
}
else
this.jdv = "0";
}
public static void main(String as[]) {
new JogoDaVelha();
}
}
espero ter ajudado!!!