Duda con manejo de eventos y AWT
Publicado por Antonio Cobo (2 intervenciones) el 20/04/2012 19:50:53
Hola,
Estoy intentando hacer un juego de tres en raya con Eclipse. Antes de usar Swing para hacer la interfaz gráfica, prefiero hacerlo manualmente mediante el AWT.
El juego tiene dos radioButtons para elegir jugar contra la máquina o contra otra persona, un boton de reinicio del juego, una label para saber quien ha ganado el juego o si han quedado en tablas y un tablero con 9 botones.
Antes de nada, he querido hacer funcionar el boton de reinicio de la partida. La cuestion es que consigo que, al darle al boton, se me reinicie todo lo menciado excepto los 9 botones y no entiendo por qué.
Incluyo el código principal del juego y la clase que maneja los eventos, a ver si alguien pudiera localizar el problema y echarme una mano.
Engancho también el resto de clases por si alguien quiere probar cuál es el problema.
Espero que alguien pueda ayudarme.
Gracias
Estoy intentando hacer un juego de tres en raya con Eclipse. Antes de usar Swing para hacer la interfaz gráfica, prefiero hacerlo manualmente mediante el AWT.
El juego tiene dos radioButtons para elegir jugar contra la máquina o contra otra persona, un boton de reinicio del juego, una label para saber quien ha ganado el juego o si han quedado en tablas y un tablero con 9 botones.
Antes de nada, he querido hacer funcionar el boton de reinicio de la partida. La cuestion es que consigo que, al darle al boton, se me reinicie todo lo menciado excepto los 9 botones y no entiendo por qué.
Incluyo el código principal del juego y la clase que maneja los eventos, a ver si alguien pudiera localizar el problema y echarme una mano.
Clase con el código principal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.awt.*;
public class TresEnRaya extends Frame
{
/**
*
*/
private static final long serialVersionUID = 1L;
//VAR==========================================
private String[] fichas = new String[9];
private Panel ventana,tablero;
private Checkbox cbHvsO,cbHvsH;
//=============================================
//CONSTRUCTOR====================================
public void reiniciar()
{
this.setTitle("Tres en Raya");
this.setBounds(0,0,Cnt.WINDOW_WIDTH,Cnt.WINDOW_HEIGHT);
this.setLayout(null);
this.setBackground(Color.LIGHT_GRAY);
this.addWindowListener(new WindowListener()); //Esto es solo para cerrar la aplicacion
//pulsando sobre la X.
ventana = new Panel();
ventana.setLayout(null);
ventana.setBounds(Cnt.VENTANA_X,Cnt.VENTANA_Y,this.getWidth()-2*Cnt.VENTANA_X,
this.getHeight()-Cnt.VENTANA_Y-Cnt.VENTANA_X);
tablero = new Panel();
tablero.setBounds(0,0,ventana.getWidth()/2,ventana.getHeight());
tablero.setLayout(new GridLayout(3,3));
for (int i=1; i<=9; i++)
{
Button b = new Button();
b.addActionListener(new Event());
tablero.add(b);
fichas[i-1] = null;
}
CheckboxGroup cbg = new CheckboxGroup();
cbHvsO = new Checkbox("Humano vs Ordenador",cbg,true);
cbHvsH = new Checkbox("Humano vs Humano",cbg,false);
cbHvsO.setBounds(Cnt.CBHVSO_X,Cnt.CBHVSO_Y,150,10);
cbHvsH.setBounds(Cnt.CBHVSO_X,Cnt.CBHVSH_Y,150,10);
Label labGanador = new Label("Ganador: Jugador 1");
labGanador.setBounds(Cnt.LABGANADOR_X,Cnt.LABGANADOR_Y,150,10);
Button btRestart = new Button("Reiniciar");
btRestart.setBounds(Cnt.BTRESTART_X,Cnt.BTRESTART_Y,Cnt.BTRESTART_WIDTH,Cnt.BTRESTART_HEIGHT);
btRestart.addActionListener(new Event());
ventana.add(tablero);
ventana.add(cbHvsO);
ventana.add(cbHvsH);
ventana.add(labGanador);
ventana.add(btRestart);
this.add(ventana);
}
//==================================================
public String getOponent()
{
if (cbHvsO.getState())
{
return "Ordenador";
}
else return "Humano";
}
}
Gestor de eventos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.awt.*;
import java.awt.event.*;
public class Event implements ActionListener
{
private String player ="Jugador";
@Override
public void actionPerformed(ActionEvent e)
{
if (((Button)e.getSource()).getLabel().equals("Reiniciar"))
{
Actividad.getJuego().removeAll();
Actividad.getJuego().reiniciar();
}
else
{
if (player.equals("Jugador"))
{
((Button)e.getSource()).setBackground(Color.RED);
}
}
}
}
Engancho también el resto de clases por si alguien quiere probar cuál es el problema.
Gestor de cierre del juego
1
2
3
4
5
6
7
8
9
import java.awt.event.*;
public class WindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
Constantes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public interface Cnt
{
public static final int WINDOW_WIDTH = 350;
public static final int WINDOW_HEIGHT = 200;
public static final int VENTANA_X = 8;
public static final int VENTANA_Y = 30;
public static final int CBHVSO_X = 180;
public static final int CBHVSO_Y = 20;
public static final int CBHVSH_Y = 45;
public static final int LABGANADOR_X = 190;
public static final int LABGANADOR_Y = 90;
public static final int BTRESTART_X = 210;
public static final int BTRESTART_Y = 120;
public static final int BTRESTART_WIDTH = 80;
public static final int BTRESTART_HEIGHT = 30;
}
MAIN
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Actividad
{
/**
* @param args
*/
private static TresEnRaya juego;
public static void main(String[] args)
{
// TODO Auto-generated method stub
juego = new TresEnRaya();
juego.setVisible(true);
juego.reiniciar();
}
public static TresEnRaya getJuego()
{
return juego;
}
}
Espero que alguien pueda ayudarme.
Gracias
Valora esta pregunta


0