Estou desenvolvendo um simples Jogo da Velha, e como os controles (Botões, Labels etc) ficam em outros JPanels não consigo evelouir no projeto.
Preciso que alguém me ajude a:
A cada click nos botoes do jogo, o JLabel (lbl3) que fica no pnlTopo receba o valor sinal (letra do jogador. Acredito que os demais problemas eu possa dar sequencia sozinho.
Obrigado por qualquer ajuda. Sei que parece ser simples, mas realmente eu, não estou conseguindo e preciso chegar ao fim do projeto.
Segue o Codigo completo:
pnlCentro.java
package jogodavelha4;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class pnlCentro extends JPanel {
public final TelaJogo telajogo;
private final JPanel jpfundo = new JPanel();
private static final Font Gra_72 = new Font("Action Jackson", Font.BOLD, 72);
int nBotoes = 9;//qtde de botoes para o jogo
public JButton[] bt = new JButton[nBotoes];//matriz de botoes para o jogo
public String mensagem = "";//aviso (no topo) para indicar a vez
int Jogadas[][] = new int [3][3];//guarda a posição das jogadas
int QtdeJogadas;
String sinal = "X"; //sinal do jogador atual
String ganhador; //guarda o jogador que venceu
String jogador1; //guarda o jogador 1
String jogador2; //guarda o jogador 2
String lblEtiqueta;
//==================================================
public pnlCentro(TelaJogo telajogo) {
this.telajogo = telajogo;
Dimension size = new Dimension(450, 320);
jpfundo.setPreferredSize(new Dimension(size));
jpfundo.setBackground(java.awt.Color.black);
jpfundo.setLayout(new GridLayout(3,3));
add(jpfundo);
//--------------
CriaBotoes();
TestaJogadas();
}
//==================================================
pnlCentro() {
throw new UnsupportedOperationException("Not supported yet.");
}
//==================================================
public void PegaEtiqueta(String lbl) {
}
//==================================================
public void EscreveSinal(int botao) {
bt[botao].setText(sinal);
bt[botao].setEnabled(false);
QtdeJogadas++; //guarda a qtde de jogadas
lblEtiqueta = sinal;
//
//topo.lbl3.setText(lblEtiqueta);
//
System.out.println("lblEtiqueta: " + lblEtiqueta);
}
//==================================================
public void TrocarSinal(int botao) { //Controle das jogadas
if (sinal == "X") {
//topo. lbl3.setText("Vez do Jogador 2"); <---- este é o meu problema
sinal = "O";
} else {
sinal = "X";
//pnlt.lbl3.setText("Vez do Jogador 1"); <----
}
}
//==================================================
public void Ganhou(String sinal) {
DesligaBotoes();
//pnltopo.lbl2.setText("O Ganhador foi: ");
//pnltopo.lbl3.setText(sinal);
}
//==================================================
public void CriaBotoes() {
for (int i = 0; i < nBotoes; i++) {
bt[i] = new JButton();
bt[i].setFont(Gra_72);
jpfundo.add(bt[i]);
}
}
//==================================================
private void DeuVelha() {
DesligaBotoes();
//topo. //pnlt.lbl3.setText("---- Deu Velha ----"); <----
//baixo.NovoJogo.setEnabled(true);
}
//==================================================
public void DesligaBotoes() {
for (int n = 0;n < nBotoes; n++) {
bt[n].setEnabled(false);
} }
//==================================================
public void LigaBotoes() {
for (int n = 0;n < nBotoes; n++) {
bt[n].setEnabled(true);
bt[n].setText("");
} }
//==================================================
private void VerificaSeGanhou(String sinal, int botao) {
//HORIZONTAIS(3) //----------------
if ((bt[0].getText().equals(sinal)) &&
(bt[1].getText().equals(sinal)) &&
(bt[2].getText().equals(sinal))) {
bt[0].setForeground(Color.green);
bt[1].setForeground(Color.green);
bt[2].setForeground(Color.green);
Ganhou(sinal);
}
//----------------
if ((bt[3].getText().equals(sinal)) &&
(bt[4].getText().equals(sinal)) &&
(bt[5].getText().equals(sinal))) {
bt[3].setForeground(Color.green);
bt[4].setForeground(Color.green);
bt[5].setForeground(Color.green);
Ganhou(sinal);
}
//----------------
if ((bt[6].getText().equals(sinal)) &&
(bt[7].getText().equals(sinal)) &&
(bt[8].getText().equals(sinal))) {
bt[6].setForeground(Color.green);
bt[7].setForeground(Color.green);
bt[8].setForeground(Color.green);
Ganhou(sinal);
}
//VERTICAIS(3) //----------------
if ((bt[0].getText().equals(sinal)) &&
(bt[3].getText().equals(sinal)) &&
(bt[6].getText().equals(sinal))) {
bt[0].setForeground(Color.green);
bt[3].setForeground(Color.green);
bt[6].setForeground(Color.green);
Ganhou(sinal);
}
//----------------
if ((bt[1].getText().equals(sinal)) &&
(bt[4].getText().equals(sinal)) &&
(bt[7].getText().equals(sinal))) {
bt[1].setForeground(Color.green);
bt[4].setForeground(Color.green);
bt[7].setForeground(Color.green);
Ganhou(sinal);
}
//----------------
if ((bt[2].getText().equals(sinal)) &&
(bt[5].getText().equals(sinal)) &&
(bt[8].getText().equals(sinal))) {
bt[2].setForeground(Color.green);
bt[5].setForeground(Color.green);
bt[8].setForeground(Color.green);
Ganhou(sinal);
}
//DIAGONAIS(2) //----------------
if ((bt[0].getText().equals(sinal)) &&
(bt[4].getText().equals(sinal)) &&
(bt[8].getText().equals(sinal))) {
bt[0].setForeground(Color.green);
bt[4].setForeground(Color.green);
bt[8].setForeground(Color.green);
Ganhou(sinal);
}
//----------------
if ((bt[2].getText().equals(sinal)) &&
(bt[4].getText().equals(sinal)) &&
(bt[6].getText().equals(sinal))) {
bt[2].setForeground(Color.green);
bt[4].setForeground(Color.green);
bt[6].setForeground(Color.green);
Ganhou(sinal);
}
//VELHA() //----------------
if (QtdeJogadas >= nBotoes) {
if ((bt[0].getText() != " ") &&
(bt[1].getText() != " ") &&
(bt[2].getText() != " ") &&
(bt[3].getText() != " ") &&
(bt[4].getText() != " ") &&
(bt[5].getText() != " ") &&
(bt[6].getText() != " ") &&
(bt[7].getText() != " ") &&
(bt[8].getText() != " ")) {
for (int i = 0; i < nBotoes; i++) {
bt[i].setForeground(Color.red);
}
DeuVelha();
} } }
//==================================================
public void TestaJogadas() {
bt[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {EscreveSinal(0); VerificaSeGanhou(sinal, 0); TrocarSinal(0);}});
//--------------
bt[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { EscreveSinal(1); VerificaSeGanhou(sinal, 1); TrocarSinal(1);}});
//--------------
bt[2].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { EscreveSinal(2); VerificaSeGanhou(sinal, 2); TrocarSinal(2);}});
//================================================
bt[3].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { EscreveSinal(3); VerificaSeGanhou(sinal, 3); TrocarSinal(3);}});
//--------------
bt[4].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { EscreveSinal(4); VerificaSeGanhou(sinal, 4); TrocarSinal(4);}});
//--------------
bt[5].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { EscreveSinal(5); VerificaSeGanhou(sinal, 5); TrocarSinal(5);}});
//================================================
bt[6].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { EscreveSinal(6); VerificaSeGanhou(sinal, 6); TrocarSinal(6);}});
//--------------
bt[7].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { EscreveSinal(7); VerificaSeGanhou(sinal, 7); TrocarSinal(7);}});
//--------------
bt[8].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { EscreveSinal(8); VerificaSeGanhou(sinal, 8); TrocarSinal(8);}});
}
//==================================================
}
pnlTopo.java
package jogodavelha4;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class pnlTopo extends JPanel {
private final TelaJogo telajogo;
private static final Font Media_24 = new Font("Arial Narrow", Font.BOLD, 24);
private static final Font Grande_42 = new Font("Action Jackson", Font.BOLD, 42);
public JLabel lbl1 = new JLabel();
public JLabel lbl2 = new JLabel();
public JLabel lbl3 = new JLabel();
//==================================================
public pnlTopo(TelaJogo telajogo) {
this.telajogo = telajogo;
Dimension tam0 = new Dimension(500, 50);
JPanel pnllbl = new JPanel();
pnllbl.setPreferredSize(tam0);
Dimension tam1 = new Dimension(250, 30);
lbl1.setPreferredSize(tam1);
lbl1.setFont(Media_24);
lbl1.setHorizontalAlignment(javax.swing.JTextField.LEFT);
lbl1.setText("JOGO DA VELHA");
Dimension tam2 = new Dimension(150, 30);
lbl2.setPreferredSize(tam2);
lbl2.setFont(Media_24);
lbl2.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
lbl2.setText("JOGADOR - ");
Dimension tam3 = new Dimension(50, 50);
lbl3.setPreferredSize(tam3);
lbl3.setFont(Grande_42);
lbl3.setForeground(Color.GREEN);
lbl3.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
lbl3.setText(".");
pnllbl.add(lbl1);
pnllbl.add(lbl2);
pnllbl.add(lbl3);
add(pnllbl);
}
//==================================================
//pnlTopo() {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//}
//==================================================
pnlTopo() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
TelaAbre.java
package jogodavelha4;
import java.awt.*;
import javax.swing.*;
public final class TelaAbre extends JFrame {
private final JPanel jpDeCima = new JPanel();
private final JPanel jpDeBaixo = new JPanel();
private final JPanel jpDeMensagem = new JPanel();
private final Font Media_24 = new Font("Arial Narrow", Font.BOLD, 22);
private final JProgressBar BarraDeProgresso = new JProgressBar();
private final JButton btnIniciar = new JButton();
private JLabel lblImagem = new JLabel();
private final JLabel lblAvisos = new JLabel();
int FlagN; //trava dupla abertura da tela do jogo.
//==================================================
public TelaAbre() {
Dimension size = new Dimension(600, 500);
setSize(size);
setMinimumSize(size);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
FlagN = 0;
JPanel container = new JPanel();
container.setBackground(Color.BLUE);
container.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
container.setLayout(new BorderLayout());
add(container);
jpDeCima.setPreferredSize(new Dimension(600,300));
jpDeCima.setBackground(java.awt.Color.black);
jpDeCima.setLayout(new BorderLayout());
lblImagem = new JLabel(new ImageIcon("src/imagem/jogodavelha.png"));
jpDeCima.add(lblImagem, BorderLayout.CENTER);
jpDeBaixo.setPreferredSize(new Dimension(600,200));
jpDeBaixo.setBackground(java.awt.Color.white);
jpDeBaixo.setLayout(new BorderLayout());
BarraDeProgresso.setBackground(new Color(0,102,102,52));
BarraDeProgresso.setStringPainted(true);
btnIniciar.setText("Clique aqui para entrar");
btnIniciar.setSize(200,30);
btnIniciar.setFont(Media_24);
jpDeMensagem.setPreferredSize(new Dimension(600,50));
jpDeMensagem.setBackground(java.awt.Color.lightGray);
jpDeMensagem.setLayout(new GridLayout(2,1));
jpDeMensagem.add(btnIniciar);
jpDeMensagem.add(BarraDeProgresso);
jpDeBaixo.add(jpDeMensagem, BorderLayout.SOUTH);
MontaBarraDeProgresso();
container.add(jpDeCima, BorderLayout.NORTH);
container.add(jpDeBaixo, BorderLayout.SOUTH);
setContentPane(container);
btnIniciar.addActionListener(event -> CarregarJogo());
}
//==================================================
public void MontaBarraDeProgresso() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
CarregaBarra();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//==================================================
public void CarregaBarra() {
new Thread() {
@Override
public void run() {
for (int n = 0;n <= 100;n++) {
try {
sleep(100);
BarraDeProgresso.setValue(n);
if (BarraDeProgresso.getValue() <= 99) {
} else {
FlagN++;
CarregarJogo();
}
} catch (InterruptedException ex) {
}
}
}
}.start();
}
//==================================================
private void CarregarJogo() {
if (FlagN <= 1) {
btnIniciar.setEnabled(false);
BarraDeProgresso.setEnabled(false);
TelaJogo telajogo = new TelaJogo();
telajogo.setVisible(true);
dispose();
FlagN++;
}
}
//==================================================
}
pnlBaixo.java
package jogodavelha4;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JPanel;
public class pnlBaixo extends JPanel {
public final TelaJogo telajogo;
private static final Font Med_14 = new Font("Arial Narrow", Font.BOLD, 14);
private static final Font Med_24 = new Font("Arial Narrow", Font.BOLD, 24);
JButton VerPlacar = new JButton();
JButton NovoJogo = new JButton();
JButton Sair = new JButton();
//==================================================
public pnlBaixo(TelaJogo telajogo) {
this.telajogo = telajogo;
JPanel pnlbtn = new JPanel();
pnlbtn.setPreferredSize(new Dimension(500, 50));
Dimension size = new Dimension(150, 45);
VerPlacar.setPreferredSize(size);
NovoJogo.setPreferredSize(size);
Sair.setPreferredSize(size);
VerPlacar.setFont(Med_24);
NovoJogo.setFont(Med_24);
Sair.setFont(Med_24);
VerPlacar.setText("Ver Placares");
NovoJogo.setText("Novo Jogo");
Sair.setText("Sair");
pnlbtn.add(VerPlacar);
pnlbtn.add(NovoJogo);
pnlbtn.add(Sair);
VerPlacar.setEnabled(true);
NovoJogo.setEnabled(true);
Sair.setEnabled(true);
VerPlacar.addActionListener(event -> VerPlacar());
NovoJogo.addActionListener(event -> NovoJogo());
Sair.addActionListener(event -> Sair());
add(pnlbtn);
}
//==================================================
private void NovoJogo() {
//for (int n= 0;n <= centro.nBotoes;n++) {
//centro.bt[n].setEnabled(true);
//centro.bt[n].setText("");
//
//}
NovoJogo.setEnabled(false);
}
//==================================================
private void Sair() {
System.exit(0);
}
//==================================================
private void VerPlacar() {
}
//==================================================
pnlBaixo() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
//==================================================
}
pnlLado.java
package jogodavelha4;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class pnlLado extends JPanel {
//-----------------------
private final TelaJogo telajogo;
//------------------
private static final Font M1 = new Font("Arial Narrow", Font.BOLD, 28);
private static final Font M2 = new Font("Action Jackson", Font.BOLD, 50);
//------------------
private final JPanel jpfundo = new JPanel();
//------------------
private final JPanel jpQuadro1 = new JPanel();
private final JPanel jpQuadro2 = new JPanel();
private final JPanel jpQuadro3 = new JPanel();
//------------------
private final JLabel lblTit1 = new JLabel("JOGADOR 01");
public final JLabel lblSinal10 = new JLabel("x");
public final JLabel lblSinal11 = new JLabel("x");
public final JLabel lblSinal12 = new JLabel("x");
//------------------
private final JLabel lblTit2 = new JLabel("JOGADOR 02");
public final JLabel lblSinal20 = new JLabel("O");
public final JLabel lblSinal21 = new JLabel("O");
public final JLabel lblSinal22 = new JLabel("O");
//------------------
private final JLabel lblTit3 = new JLabel("VENCEDOR");
//------------------
public final JLabel lblSinalVence01 = new JLabel(".");
public final JLabel lblSinalVence02 = new JLabel(".");
public final JLabel lblSinalVence03 = new JLabel(".");
//==================================================
public pnlLado(TelaJogo telajogo) {
this.telajogo = telajogo;
jpfundo.setPreferredSize(new Dimension(150,350));
jpfundo.setBackground(java.awt.Color.red);
jpfundo.setLayout(new GridLayout(3,1));
//-----------------------
lblSinal10.setEnabled(false);
lblSinal11.setEnabled(false);
lblSinal12.setEnabled(false);
//------------------
lblSinal20.setEnabled(false);
lblSinal21.setEnabled(false);
lblSinal22.setEnabled(false);
//------------------
lblSinalVence01.setEnabled(false);
lblSinalVence02.setEnabled(false);
lblSinalVence03.setEnabled(false);
//------------------
MontaQuadro01();
MontaQuadro02();
MontaQuadro03();
//-----------------------
this.add(jpfundo);
}
//==================================================
pnlLado() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
//==================================================
public void MontaQuadro01() {
Dimension tam0 = new Dimension(150, 100);
Dimension tam1 = new Dimension(145, 20);
Dimension tam2 = new Dimension(50, 80);
//-----------------------
jpQuadro1.setPreferredSize(new Dimension(tam0));
jpQuadro1.setBackground(java.awt.Color.ORANGE);
jpQuadro1.setEnabled(false);
jpQuadro1.setLayout(new BorderLayout());
lblTit1.setPreferredSize(tam0);
lblTit1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
lblTit1.setFont(M1);
//-----------------------
lblTit1.setPreferredSize(tam1);
lblTit1.setFont(M1);
jpQuadro1.add(lblTit1);
//-----------------------
lblSinal10.setPreferredSize(tam2);
lblSinal11.setPreferredSize(tam2);
lblSinal12.setPreferredSize(tam2);
//-----------------------
lblSinal10.setFont(M2);
lblSinal11.setFont(M2);
lblSinal12.setFont(M2);
//-----------------------
JPanel jpFundo01 = new JPanel();
jpFundo01.setPreferredSize(new Dimension(145,70));
jpFundo01.setBackground(java.awt.Color.orange);
jpFundo01.setLayout(new GridLayout(1,3));
//-----------------------
jpFundo01.add(lblSinal10);
jpFundo01.add(lblSinal11);
jpFundo01.add(lblSinal12);
//-----------------------
jpQuadro1.add(lblTit1, BorderLayout.NORTH);
jpQuadro1.add(jpFundo01, BorderLayout.CENTER);
//-----------------------
jpfundo.add(jpQuadro1);
}
//==================================================
public void MontaQuadro02() {
Dimension tam0 = new Dimension(150, 100);
Dimension tam1 = new Dimension(145, 20);
Dimension tam2 = new Dimension(50, 80);
//-----------------------
jpQuadro2.setPreferredSize(new Dimension(tam0));
jpQuadro2.setBackground(java.awt.Color.YELLOW);
jpQuadro2.setEnabled(false);
jpQuadro2.setLayout(new BorderLayout());
//-----------------------
lblTit2.setPreferredSize(tam1);
lblTit2.setFont(M1);
jpQuadro2.add(lblTit2);
//-----------------------
lblSinal20.setPreferredSize(tam2);
lblSinal21.setPreferredSize(tam2);
lblSinal22.setPreferredSize(tam2);
//-----------------------
lblSinal20.setFont(M2);
lblSinal21.setFont(M2);
lblSinal22.setFont(M2);
//-----------------------
JPanel jpFundo02 = new JPanel();
jpFundo02.setPreferredSize(new Dimension(145,70));
jpFundo02.setBackground(java.awt.Color.yellow);
jpFundo02.setLayout(new GridLayout(1,3));
//-----------------------
jpFundo02.add(lblSinal20);
jpFundo02.add(lblSinal21);
jpFundo02.add(lblSinal22);
//-----------------------
jpQuadro2.add(lblTit2, BorderLayout.NORTH);
jpQuadro2.add(jpFundo02, BorderLayout.CENTER);
//-----------------------
jpfundo.add(jpQuadro2);
}
//==================================================
public void MontaQuadro03() {
Dimension tam0 = new Dimension(150, 100);
Dimension tam1 = new Dimension(145, 20);
Dimension tam2 = new Dimension(50, 80);
//-----------------------
jpQuadro3.setPreferredSize(new Dimension(tam0));
jpQuadro3.setBackground(java.awt.Color.PINK);
jpQuadro3.setEnabled(false);
jpQuadro3.setLayout(new BorderLayout());
//-----------------------
lblTit3.setPreferredSize(tam1);
lblTit3.setFont(M1);
jpQuadro3.add(lblTit3);
//-----------------------
lblSinalVence01.setPreferredSize(tam2);
lblSinalVence02.setPreferredSize(tam2);
lblSinalVence03.setPreferredSize(tam2);
//-----------------------
lblSinalVence01.setFont(M2);
lblSinalVence02.setFont(M2);
lblSinalVence03.setFont(M2);
//-----------------------
JPanel jpFundo03 = new JPanel();
jpFundo03.setPreferredSize(new Dimension(145,70));
jpFundo03.setBackground(java.awt.Color.pink);
jpFundo03.setLayout(new GridLayout(1,3));
//-----------------------
jpFundo03.add(lblSinalVence01);
jpFundo03.add(lblSinalVence02);
jpFundo03.add(lblSinalVence03);
//-----------------------
jpQuadro3.add(lblTit3, BorderLayout.NORTH);
jpQuadro3.add(jpFundo03, BorderLayout.CENTER);
//-----------------------
jpfundo.add(jpQuadro3);
}
//==================================================
}
package jogodavelha4;
import javax.swing.JFrame;
public class JogoDaVelha4 extends JFrame {
//pnlTopo topo = new pnlTopo();
pnlBaixo baixo = new pnlBaixo();
pnlLado lado = new pnlLado();
pnlCentro centro = new pnlCentro();
TelaAbre abertura;
//==================================================
public static void main(String[] args) {
TelaAbre abertura = new TelaAbre();
abertura.setVisible(true);
}
//==================================================
}
Pergunta
lc_PapaiLu
Estou desenvolvendo um simples Jogo da Velha, e como os controles (Botões, Labels etc) ficam em outros JPanels não consigo evelouir no projeto.
Preciso que alguém me ajude a:
A cada click nos botoes do jogo, o JLabel (lbl3) que fica no pnlTopo receba o valor sinal (letra do jogador. Acredito que os demais problemas eu possa dar sequencia sozinho.
Obrigado por qualquer ajuda. Sei que parece ser simples, mas realmente eu, não estou conseguindo e preciso chegar ao fim do projeto.
Segue o Codigo completo:
pnlCentro.java
package jogodavelha4; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JPanel; public class pnlCentro extends JPanel { public final TelaJogo telajogo; private final JPanel jpfundo = new JPanel(); private static final Font Gra_72 = new Font("Action Jackson", Font.BOLD, 72); int nBotoes = 9;//qtde de botoes para o jogo public JButton[] bt = new JButton[nBotoes];//matriz de botoes para o jogo public String mensagem = "";//aviso (no topo) para indicar a vez int Jogadas[][] = new int [3][3];//guarda a posição das jogadas int QtdeJogadas; String sinal = "X"; //sinal do jogador atual String ganhador; //guarda o jogador que venceu String jogador1; //guarda o jogador 1 String jogador2; //guarda o jogador 2 String lblEtiqueta; //================================================== public pnlCentro(TelaJogo telajogo) { this.telajogo = telajogo; Dimension size = new Dimension(450, 320); jpfundo.setPreferredSize(new Dimension(size)); jpfundo.setBackground(java.awt.Color.black); jpfundo.setLayout(new GridLayout(3,3)); add(jpfundo); //-------------- CriaBotoes(); TestaJogadas(); } //================================================== pnlCentro() { throw new UnsupportedOperationException("Not supported yet."); } //================================================== public void PegaEtiqueta(String lbl) { } //================================================== public void EscreveSinal(int botao) { bt[botao].setText(sinal); bt[botao].setEnabled(false); QtdeJogadas++; //guarda a qtde de jogadas lblEtiqueta = sinal; // //topo.lbl3.setText(lblEtiqueta); // System.out.println("lblEtiqueta: " + lblEtiqueta); } //================================================== public void TrocarSinal(int botao) { //Controle das jogadas if (sinal == "X") { //topo. lbl3.setText("Vez do Jogador 2"); <---- este é o meu problema sinal = "O"; } else { sinal = "X"; //pnlt.lbl3.setText("Vez do Jogador 1"); <---- } } //================================================== public void Ganhou(String sinal) { DesligaBotoes(); //pnltopo.lbl2.setText("O Ganhador foi: "); //pnltopo.lbl3.setText(sinal); } //================================================== public void CriaBotoes() { for (int i = 0; i < nBotoes; i++) { bt[i] = new JButton(); bt[i].setFont(Gra_72); jpfundo.add(bt[i]); } } //================================================== private void DeuVelha() { DesligaBotoes(); //topo. //pnlt.lbl3.setText("---- Deu Velha ----"); <---- //baixo.NovoJogo.setEnabled(true); } //================================================== public void DesligaBotoes() { for (int n = 0;n < nBotoes; n++) { bt[n].setEnabled(false); } } //================================================== public void LigaBotoes() { for (int n = 0;n < nBotoes; n++) { bt[n].setEnabled(true); bt[n].setText(""); } } //================================================== private void VerificaSeGanhou(String sinal, int botao) { //HORIZONTAIS(3) //---------------- if ((bt[0].getText().equals(sinal)) && (bt[1].getText().equals(sinal)) && (bt[2].getText().equals(sinal))) { bt[0].setForeground(Color.green); bt[1].setForeground(Color.green); bt[2].setForeground(Color.green); Ganhou(sinal); } //---------------- if ((bt[3].getText().equals(sinal)) && (bt[4].getText().equals(sinal)) && (bt[5].getText().equals(sinal))) { bt[3].setForeground(Color.green); bt[4].setForeground(Color.green); bt[5].setForeground(Color.green); Ganhou(sinal); } //---------------- if ((bt[6].getText().equals(sinal)) && (bt[7].getText().equals(sinal)) && (bt[8].getText().equals(sinal))) { bt[6].setForeground(Color.green); bt[7].setForeground(Color.green); bt[8].setForeground(Color.green); Ganhou(sinal); } //VERTICAIS(3) //---------------- if ((bt[0].getText().equals(sinal)) && (bt[3].getText().equals(sinal)) && (bt[6].getText().equals(sinal))) { bt[0].setForeground(Color.green); bt[3].setForeground(Color.green); bt[6].setForeground(Color.green); Ganhou(sinal); } //---------------- if ((bt[1].getText().equals(sinal)) && (bt[4].getText().equals(sinal)) && (bt[7].getText().equals(sinal))) { bt[1].setForeground(Color.green); bt[4].setForeground(Color.green); bt[7].setForeground(Color.green); Ganhou(sinal); } //---------------- if ((bt[2].getText().equals(sinal)) && (bt[5].getText().equals(sinal)) && (bt[8].getText().equals(sinal))) { bt[2].setForeground(Color.green); bt[5].setForeground(Color.green); bt[8].setForeground(Color.green); Ganhou(sinal); } //DIAGONAIS(2) //---------------- if ((bt[0].getText().equals(sinal)) && (bt[4].getText().equals(sinal)) && (bt[8].getText().equals(sinal))) { bt[0].setForeground(Color.green); bt[4].setForeground(Color.green); bt[8].setForeground(Color.green); Ganhou(sinal); } //---------------- if ((bt[2].getText().equals(sinal)) && (bt[4].getText().equals(sinal)) && (bt[6].getText().equals(sinal))) { bt[2].setForeground(Color.green); bt[4].setForeground(Color.green); bt[6].setForeground(Color.green); Ganhou(sinal); } //VELHA() //---------------- if (QtdeJogadas >= nBotoes) { if ((bt[0].getText() != " ") && (bt[1].getText() != " ") && (bt[2].getText() != " ") && (bt[3].getText() != " ") && (bt[4].getText() != " ") && (bt[5].getText() != " ") && (bt[6].getText() != " ") && (bt[7].getText() != " ") && (bt[8].getText() != " ")) { for (int i = 0; i < nBotoes; i++) { bt[i].setForeground(Color.red); } DeuVelha(); } } } //================================================== public void TestaJogadas() { bt[0].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {EscreveSinal(0); VerificaSeGanhou(sinal, 0); TrocarSinal(0);}}); //-------------- bt[1].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { EscreveSinal(1); VerificaSeGanhou(sinal, 1); TrocarSinal(1);}}); //-------------- bt[2].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { EscreveSinal(2); VerificaSeGanhou(sinal, 2); TrocarSinal(2);}}); //================================================ bt[3].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { EscreveSinal(3); VerificaSeGanhou(sinal, 3); TrocarSinal(3);}}); //-------------- bt[4].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { EscreveSinal(4); VerificaSeGanhou(sinal, 4); TrocarSinal(4);}}); //-------------- bt[5].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { EscreveSinal(5); VerificaSeGanhou(sinal, 5); TrocarSinal(5);}}); //================================================ bt[6].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { EscreveSinal(6); VerificaSeGanhou(sinal, 6); TrocarSinal(6);}}); //-------------- bt[7].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { EscreveSinal(7); VerificaSeGanhou(sinal, 7); TrocarSinal(7);}}); //-------------- bt[8].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { EscreveSinal(8); VerificaSeGanhou(sinal, 8); TrocarSinal(8);}}); } //================================================== }
pnlTopo.java
package jogodavelha4; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import javax.swing.JLabel; import javax.swing.JPanel; public class pnlTopo extends JPanel { private final TelaJogo telajogo; private static final Font Media_24 = new Font("Arial Narrow", Font.BOLD, 24); private static final Font Grande_42 = new Font("Action Jackson", Font.BOLD, 42); public JLabel lbl1 = new JLabel(); public JLabel lbl2 = new JLabel(); public JLabel lbl3 = new JLabel(); //================================================== public pnlTopo(TelaJogo telajogo) { this.telajogo = telajogo; Dimension tam0 = new Dimension(500, 50); JPanel pnllbl = new JPanel(); pnllbl.setPreferredSize(tam0); Dimension tam1 = new Dimension(250, 30); lbl1.setPreferredSize(tam1); lbl1.setFont(Media_24); lbl1.setHorizontalAlignment(javax.swing.JTextField.LEFT); lbl1.setText("JOGO DA VELHA"); Dimension tam2 = new Dimension(150, 30); lbl2.setPreferredSize(tam2); lbl2.setFont(Media_24); lbl2.setHorizontalAlignment(javax.swing.JTextField.RIGHT); lbl2.setText("JOGADOR - "); Dimension tam3 = new Dimension(50, 50); lbl3.setPreferredSize(tam3); lbl3.setFont(Grande_42); lbl3.setForeground(Color.GREEN); lbl3.setHorizontalAlignment(javax.swing.JTextField.RIGHT); lbl3.setText("."); pnllbl.add(lbl1); pnllbl.add(lbl2); pnllbl.add(lbl3); add(pnllbl); } //================================================== //pnlTopo() { // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. //} //================================================== pnlTopo() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }
TelaAbre.java
package jogodavelha4; import java.awt.*; import javax.swing.*; public final class TelaAbre extends JFrame { private final JPanel jpDeCima = new JPanel(); private final JPanel jpDeBaixo = new JPanel(); private final JPanel jpDeMensagem = new JPanel(); private final Font Media_24 = new Font("Arial Narrow", Font.BOLD, 22); private final JProgressBar BarraDeProgresso = new JProgressBar(); private final JButton btnIniciar = new JButton(); private JLabel lblImagem = new JLabel(); private final JLabel lblAvisos = new JLabel(); int FlagN; //trava dupla abertura da tela do jogo. //================================================== public TelaAbre() { Dimension size = new Dimension(600, 500); setSize(size); setMinimumSize(size); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); FlagN = 0; JPanel container = new JPanel(); container.setBackground(Color.BLUE); container.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); container.setLayout(new BorderLayout()); add(container); jpDeCima.setPreferredSize(new Dimension(600,300)); jpDeCima.setBackground(java.awt.Color.black); jpDeCima.setLayout(new BorderLayout()); lblImagem = new JLabel(new ImageIcon("src/imagem/jogodavelha.png")); jpDeCima.add(lblImagem, BorderLayout.CENTER); jpDeBaixo.setPreferredSize(new Dimension(600,200)); jpDeBaixo.setBackground(java.awt.Color.white); jpDeBaixo.setLayout(new BorderLayout()); BarraDeProgresso.setBackground(new Color(0,102,102,52)); BarraDeProgresso.setStringPainted(true); btnIniciar.setText("Clique aqui para entrar"); btnIniciar.setSize(200,30); btnIniciar.setFont(Media_24); jpDeMensagem.setPreferredSize(new Dimension(600,50)); jpDeMensagem.setBackground(java.awt.Color.lightGray); jpDeMensagem.setLayout(new GridLayout(2,1)); jpDeMensagem.add(btnIniciar); jpDeMensagem.add(BarraDeProgresso); jpDeBaixo.add(jpDeMensagem, BorderLayout.SOUTH); MontaBarraDeProgresso(); container.add(jpDeCima, BorderLayout.NORTH); container.add(jpDeBaixo, BorderLayout.SOUTH); setContentPane(container); btnIniciar.addActionListener(event -> CarregarJogo()); } //================================================== public void MontaBarraDeProgresso() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { CarregaBarra(); } catch (Exception e) { e.printStackTrace(); } } }); } //================================================== public void CarregaBarra() { new Thread() { @Override public void run() { for (int n = 0;n <= 100;n++) { try { sleep(100); BarraDeProgresso.setValue(n); if (BarraDeProgresso.getValue() <= 99) { } else { FlagN++; CarregarJogo(); } } catch (InterruptedException ex) { } } } }.start(); } //================================================== private void CarregarJogo() { if (FlagN <= 1) { btnIniciar.setEnabled(false); BarraDeProgresso.setEnabled(false); TelaJogo telajogo = new TelaJogo(); telajogo.setVisible(true); dispose(); FlagN++; } } //================================================== }
pnlBaixo.java
package jogodavelha4; import java.awt.Dimension; import java.awt.Font; import javax.swing.JButton; import javax.swing.JPanel; public class pnlBaixo extends JPanel { public final TelaJogo telajogo; private static final Font Med_14 = new Font("Arial Narrow", Font.BOLD, 14); private static final Font Med_24 = new Font("Arial Narrow", Font.BOLD, 24); JButton VerPlacar = new JButton(); JButton NovoJogo = new JButton(); JButton Sair = new JButton(); //================================================== public pnlBaixo(TelaJogo telajogo) { this.telajogo = telajogo; JPanel pnlbtn = new JPanel(); pnlbtn.setPreferredSize(new Dimension(500, 50)); Dimension size = new Dimension(150, 45); VerPlacar.setPreferredSize(size); NovoJogo.setPreferredSize(size); Sair.setPreferredSize(size); VerPlacar.setFont(Med_24); NovoJogo.setFont(Med_24); Sair.setFont(Med_24); VerPlacar.setText("Ver Placares"); NovoJogo.setText("Novo Jogo"); Sair.setText("Sair"); pnlbtn.add(VerPlacar); pnlbtn.add(NovoJogo); pnlbtn.add(Sair); VerPlacar.setEnabled(true); NovoJogo.setEnabled(true); Sair.setEnabled(true); VerPlacar.addActionListener(event -> VerPlacar()); NovoJogo.addActionListener(event -> NovoJogo()); Sair.addActionListener(event -> Sair()); add(pnlbtn); } //================================================== private void NovoJogo() { //for (int n= 0;n <= centro.nBotoes;n++) { //centro.bt[n].setEnabled(true); //centro.bt[n].setText(""); // //} NovoJogo.setEnabled(false); } //================================================== private void Sair() { System.exit(0); } //================================================== private void VerPlacar() { } //================================================== pnlBaixo() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } //================================================== }
pnlLado.java
package jogodavelha4; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import javax.swing.JLabel; import javax.swing.JPanel; public class pnlLado extends JPanel { //----------------------- private final TelaJogo telajogo; //------------------ private static final Font M1 = new Font("Arial Narrow", Font.BOLD, 28); private static final Font M2 = new Font("Action Jackson", Font.BOLD, 50); //------------------ private final JPanel jpfundo = new JPanel(); //------------------ private final JPanel jpQuadro1 = new JPanel(); private final JPanel jpQuadro2 = new JPanel(); private final JPanel jpQuadro3 = new JPanel(); //------------------ private final JLabel lblTit1 = new JLabel("JOGADOR 01"); public final JLabel lblSinal10 = new JLabel("x"); public final JLabel lblSinal11 = new JLabel("x"); public final JLabel lblSinal12 = new JLabel("x"); //------------------ private final JLabel lblTit2 = new JLabel("JOGADOR 02"); public final JLabel lblSinal20 = new JLabel("O"); public final JLabel lblSinal21 = new JLabel("O"); public final JLabel lblSinal22 = new JLabel("O"); //------------------ private final JLabel lblTit3 = new JLabel("VENCEDOR"); //------------------ public final JLabel lblSinalVence01 = new JLabel("."); public final JLabel lblSinalVence02 = new JLabel("."); public final JLabel lblSinalVence03 = new JLabel("."); //================================================== public pnlLado(TelaJogo telajogo) { this.telajogo = telajogo; jpfundo.setPreferredSize(new Dimension(150,350)); jpfundo.setBackground(java.awt.Color.red); jpfundo.setLayout(new GridLayout(3,1)); //----------------------- lblSinal10.setEnabled(false); lblSinal11.setEnabled(false); lblSinal12.setEnabled(false); //------------------ lblSinal20.setEnabled(false); lblSinal21.setEnabled(false); lblSinal22.setEnabled(false); //------------------ lblSinalVence01.setEnabled(false); lblSinalVence02.setEnabled(false); lblSinalVence03.setEnabled(false); //------------------ MontaQuadro01(); MontaQuadro02(); MontaQuadro03(); //----------------------- this.add(jpfundo); } //================================================== pnlLado() { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } //================================================== public void MontaQuadro01() { Dimension tam0 = new Dimension(150, 100); Dimension tam1 = new Dimension(145, 20); Dimension tam2 = new Dimension(50, 80); //----------------------- jpQuadro1.setPreferredSize(new Dimension(tam0)); jpQuadro1.setBackground(java.awt.Color.ORANGE); jpQuadro1.setEnabled(false); jpQuadro1.setLayout(new BorderLayout()); lblTit1.setPreferredSize(tam0); lblTit1.setHorizontalAlignment(javax.swing.JTextField.CENTER); lblTit1.setFont(M1); //----------------------- lblTit1.setPreferredSize(tam1); lblTit1.setFont(M1); jpQuadro1.add(lblTit1); //----------------------- lblSinal10.setPreferredSize(tam2); lblSinal11.setPreferredSize(tam2); lblSinal12.setPreferredSize(tam2); //----------------------- lblSinal10.setFont(M2); lblSinal11.setFont(M2); lblSinal12.setFont(M2); //----------------------- JPanel jpFundo01 = new JPanel(); jpFundo01.setPreferredSize(new Dimension(145,70)); jpFundo01.setBackground(java.awt.Color.orange); jpFundo01.setLayout(new GridLayout(1,3)); //----------------------- jpFundo01.add(lblSinal10); jpFundo01.add(lblSinal11); jpFundo01.add(lblSinal12); //----------------------- jpQuadro1.add(lblTit1, BorderLayout.NORTH); jpQuadro1.add(jpFundo01, BorderLayout.CENTER); //----------------------- jpfundo.add(jpQuadro1); } //================================================== public void MontaQuadro02() { Dimension tam0 = new Dimension(150, 100); Dimension tam1 = new Dimension(145, 20); Dimension tam2 = new Dimension(50, 80); //----------------------- jpQuadro2.setPreferredSize(new Dimension(tam0)); jpQuadro2.setBackground(java.awt.Color.YELLOW); jpQuadro2.setEnabled(false); jpQuadro2.setLayout(new BorderLayout()); //----------------------- lblTit2.setPreferredSize(tam1); lblTit2.setFont(M1); jpQuadro2.add(lblTit2); //----------------------- lblSinal20.setPreferredSize(tam2); lblSinal21.setPreferredSize(tam2); lblSinal22.setPreferredSize(tam2); //----------------------- lblSinal20.setFont(M2); lblSinal21.setFont(M2); lblSinal22.setFont(M2); //----------------------- JPanel jpFundo02 = new JPanel(); jpFundo02.setPreferredSize(new Dimension(145,70)); jpFundo02.setBackground(java.awt.Color.yellow); jpFundo02.setLayout(new GridLayout(1,3)); //----------------------- jpFundo02.add(lblSinal20); jpFundo02.add(lblSinal21); jpFundo02.add(lblSinal22); //----------------------- jpQuadro2.add(lblTit2, BorderLayout.NORTH); jpQuadro2.add(jpFundo02, BorderLayout.CENTER); //----------------------- jpfundo.add(jpQuadro2); } //================================================== public void MontaQuadro03() { Dimension tam0 = new Dimension(150, 100); Dimension tam1 = new Dimension(145, 20); Dimension tam2 = new Dimension(50, 80); //----------------------- jpQuadro3.setPreferredSize(new Dimension(tam0)); jpQuadro3.setBackground(java.awt.Color.PINK); jpQuadro3.setEnabled(false); jpQuadro3.setLayout(new BorderLayout()); //----------------------- lblTit3.setPreferredSize(tam1); lblTit3.setFont(M1); jpQuadro3.add(lblTit3); //----------------------- lblSinalVence01.setPreferredSize(tam2); lblSinalVence02.setPreferredSize(tam2); lblSinalVence03.setPreferredSize(tam2); //----------------------- lblSinalVence01.setFont(M2); lblSinalVence02.setFont(M2); lblSinalVence03.setFont(M2); //----------------------- JPanel jpFundo03 = new JPanel(); jpFundo03.setPreferredSize(new Dimension(145,70)); jpFundo03.setBackground(java.awt.Color.pink); jpFundo03.setLayout(new GridLayout(1,3)); //----------------------- jpFundo03.add(lblSinalVence01); jpFundo03.add(lblSinalVence02); jpFundo03.add(lblSinalVence03); //----------------------- jpQuadro3.add(lblTit3, BorderLayout.NORTH); jpQuadro3.add(jpFundo03, BorderLayout.CENTER); //----------------------- jpfundo.add(jpQuadro3); } //================================================== }
TelaJogo.java
package jogodavelha4; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; public class TelaJogo extends JFrame { //================================================== public TelaJogo() { Dimension size = new Dimension(600, 500); setTitle("JOGO DA VELHA"); setSize(size); setMinimumSize(size); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setLocationRelativeTo(null); JPanel container = new JPanel(); container.setBackground(Color.BLUE); container.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); container.setLayout(new BorderLayout(5, 5)); container.add(new pnlTopo(this), BorderLayout.NORTH); container.add(new pnlBaixo(this), BorderLayout.SOUTH); container.add(new pnlLado(this), BorderLayout.EAST); container.add(new pnlCentro(this), BorderLayout.CENTER); setContentPane(container); JPanel jpTabuleiro = new JPanel(); jpTabuleiro.setBackground(Color.cyan); jpTabuleiro.setLayout(new GridLayout(3,3)); } //================================================== }
JogoDaVelha4.java
package jogodavelha4; import javax.swing.JFrame; public class JogoDaVelha4 extends JFrame { //pnlTopo topo = new pnlTopo(); pnlBaixo baixo = new pnlBaixo(); pnlLado lado = new pnlLado(); pnlCentro centro = new pnlCentro(); TelaAbre abertura; //================================================== public static void main(String[] args) { TelaAbre abertura = new TelaAbre(); abertura.setVisible(true); } //================================================== }
Link para o comentário
Compartilhar em outros sites
0 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.