Ver Mensaje Individual
  #3 (permalink)  
Antiguo 22/07/2012, 03:02
Sephyneko
 
Fecha de Ingreso: junio-2012
Mensajes: 17
Antigüedad: 11 años, 10 meses
Puntos: 2
Respuesta: StackOverflowErro

Si estaba viendo eso, no había leído de la recursividad hasta ahora. pasa que si quiero separar los elementos visuales de las acciones de cada botón, me veo obligado a hacer eso.

tengo dos opciones:

o pongo en la misma clase los elementos visuales del frame y los eventos de cada uno.

o le pongo un limite de recursividad por ejemplo que esta de por si es un lio por que me tendria que poner a contar cuantas veces interactuan el primero con el segundo

hay otra forma?

dejo el codigo cambiado hasta ahora.

Aca escribiendo esto se me ocurrio que si puedo poner asignarle a los botones el "addActionListener" desde la clase eventos, podria separar botones de eventos no?
voy a ver si me sale


ejecutable que ya ni me acuerdo para que lo queria poner en una clase a parte:
Código Java:
Ver original
  1. public class Ejecutable extends MainVentana{
  2.    
  3.     public static void main(String [] args){
  4.         MainVentana a=new MainVentana();
  5.     }
  6. }

MainVentana - Saque todos los botones y solo deje los atributos del jframe y la posicion de los botones
Código Java:
Ver original
  1. public class MainVentana extends BotonesJuego{
  2.    
  3.     JFrame hola=new JFrame();
  4.     BotonesJuego boton=new BotonesJuego();
  5.  
  6.     public MainVentana(){
  7.        
  8.         //definir atributo del frame--------
  9.         hola.setTitle("TATETI - Tres En Raya");
  10.         hola.setLayout(null);
  11.         hola.setResizable(false);
  12.         hola.setBackground(Color.black);
  13.         hola.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14.         hola.setVisible(true);
  15.         hola.setBounds(400, 300, 350, 290);
  16.        
  17.         boton.BJuego3x3();
  18.         boton.BJuego4x4();
  19.         boton.menu();
  20.         menupantalla();
  21.         BJuego3x3();
  22.         BJuego4x4();
  23.        
  24.        
  25.     }  
  26.  
  27. //agrega los botnes y les da su posicion inicial-------------------------------
  28.     public void BJuego3x3(){
  29.        
  30.         boton.BJuego3x3();
  31.         for (int i=0;i<boton.tateti3.length;i++){
  32.             for (int j=0;j<boton.tateti3[i].length;j++){
  33.                 hola.add(boton.tateti3[i][j]);
  34.             }
  35.         }
  36.         boton.tateti3[0][0].setBounds(10, 10, 50, 50);
  37.         boton.tateti3[0][1].setBounds(70, 10, 50, 50);
  38.         boton.tateti3[0][2].setBounds(130, 10, 50, 50);
  39.         boton.tateti3[1][0].setBounds(10, 70, 50, 50);
  40.         boton.tateti3[1][1].setBounds(70, 70, 50, 50);
  41.         boton.tateti3[1][2].setBounds(130, 70, 50, 50);
  42.         boton.tateti3[2][0].setBounds(10, 130, 50, 50);
  43.         boton.tateti3[2][1].setBounds(70, 130, 50, 50);
  44.         boton.tateti3[2][2].setBounds(130, 130, 50, 50);
  45.        
  46.     }//fin de metodo BJuego3x3
  47.  
  48.     public void BJuego4x4(){
  49.         boton.BJuego4x4();
  50.         boton.tateti4[0][0].setBounds(10, 10, 30, 30);
  51.         boton.tateti4[0][1].setBounds(60, 10, 30, 30);
  52.         boton.tateti4[0][2].setBounds(110, 10, 30, 30);
  53.         boton.tateti4[0][3].setBounds(160, 10, 30, 30);
  54.         boton.tateti4[1][0].setBounds(10, 60, 30, 30);
  55.         boton.tateti4[1][1].setBounds(60, 60, 30, 30);
  56.         boton.tateti4[1][2].setBounds(110, 60, 30, 30);
  57.         boton.tateti4[1][3].setBounds(160, 60, 30, 30);
  58.         boton.tateti4[2][0].setBounds(10, 110, 30, 30);
  59.         boton.tateti4[2][1].setBounds(60, 110, 30, 30);
  60.         boton.tateti4[2][2].setBounds(110, 110, 30, 30);
  61.         boton.tateti4[2][3].setBounds(160, 110, 30, 30);
  62.         boton.tateti4[3][0].setBounds(10, 160, 30, 30);
  63.         boton.tateti4[3][1].setBounds(60, 160, 30, 30);
  64.         boton.tateti4[3][2].setBounds(110, 160, 30, 30);
  65.         boton.tateti4[3][3].setBounds(160, 160, 30, 30);
  66.    
  67.         for (int i=0;i<boton.tateti4.length;i++){
  68.             for (int j=0;j<boton.tateti4[i].length;j++){
  69.                 hola.add(boton.tateti4[i][j]);
  70.             }
  71.         }//fin de for
  72.     }//fin de metodo
  73.    
  74.     public void menupantalla(){
  75.         boton.menu();
  76.         boton.opciones[0].setBounds(200, 10, 120, 30);
  77.         boton.opciones[1].setBounds(200, 50, 120, 30);
  78.         boton.opciones[2].setBounds(200, 90, 120, 30);
  79.         boton.opciones[3].setBounds(200, 130, 120, 30);
  80.         boton.opciones[4].setBounds(200, 170, 120, 30);
  81.         boton.opciones[5].setBounds(200, 210, 120, 30);
  82.         Estado2.setBounds(10, 220, 170, 20);
  83.         Estado.setBounds(10, 190, 170, 20);
  84.         hola.add(Estado2);
  85.         hola.add(Estado);
  86.        
  87.         hola.add(boton.opciones[0]);
  88.         hola.add(boton.opciones[1]);
  89.         hola.add(boton.opciones[2]);
  90.         hola.add(boton.opciones[3]);
  91.         hola.add(boton.opciones[4]);
  92.         hola.add(boton.opciones[5]);
  93. }
  94. }

y los botones y el ActionListener, despues tengo otras 2 clases mas que es la parte ""logica""
Código Java:
Ver original
  1. public class BotonesJuego implements ActionListener{
  2.    
  3.  
  4.     JButton tateti3[][]=new JButton[3][3];
  5.     JButton tateti4[][]=new JButton[4][4];
  6.     JButton opciones[]=new JButton[6];
  7.     JTextField Estado=new JTextField();
  8.     JTextField Estado2=new JTextField();
  9.     String opt[]={"Un Jugador","Dos Jugadores","Tres X Tres","Cuatro X Cuatro","Nuevo Juego","Salir"};
  10.     Juego3x3 tres=new Juego3x3();
  11.     int a,b;
  12.     boolean unJug=false,movmaq=false;
  13.    
  14. //------------------atributos de botones---------------------------------------
  15.     public void BJuego3x3(){
  16.    
  17.         for (int i=0;i<tateti3.length;i++){
  18.             for (int j=0;j<tateti3[i].length;j++){
  19.                 tateti3[i][j]=new JButton(" * ");
  20.                 tateti3[i][j].setVisible(false);
  21.                 tateti3[i][j].addActionListener(this);
  22.                 }
  23.         }
  24.  
  25.     }
  26.  
  27.     public void BJuego4x4(){
  28.     for (int i=0;i<tateti4.length;i++){
  29.          for (int j=0;j<tateti4[i].length;j++){
  30.             tateti4[i][j]=new JButton(" * ");
  31.             tateti4[i][j].setVisible(false);
  32.             tateti4[i][j].addActionListener(this);
  33.          }
  34.      }
  35.  
  36.     }
  37.    
  38.     public void menu(){
  39.         for (int i=0;i<opciones.length;i++){
  40.             opciones[i]=new JButton(opt[i]);
  41.             opciones[i].setVisible(true);
  42.             opciones[i].addActionListener(this);
  43.         }
  44.         Estado.setEditable(false);
  45.         Estado.setVisible(true);
  46.         Estado2.setEditable(false);
  47.         Estado2.setVisible(true);
  48.     }
  49.  
  50.  
  51. //----------------- acciones de botones OPCIONES-------------------------------
  52.     public void unJugador(){
  53.         opciones[0].setEnabled(false);
  54.         opciones[1].setEnabled(false);
  55.         unJug=true;
  56.     }
  57.  
  58.     public void DosJugadores(){
  59.         opciones[0].setEnabled(false);
  60.         opciones[1].setEnabled(false);
  61.         unJug=false;
  62.     }
  63.  
  64.     public void tabla3x3(){
  65.         for (int i=0;i<tateti3.length;i++){
  66.             for (int j=0;j<tateti3[i].length;j++){
  67.                 tateti3[i][j].setVisible(true);
  68.         }}
  69.        
  70.         opciones[2].setEnabled(false);
  71.         opciones[3].setEnabled(false);
  72.         tres.inicializarTablero();
  73.     }
  74.        
  75.     public void tabla4x4(){
  76.         for (int i=0;i<tateti4.length;i++){
  77.              for (int j=0;j<tateti4[i].length;j++){
  78.                 tateti4[i][j].setVisible(true);
  79.              }
  80.          }
  81.         opciones[2].setEnabled(false);
  82.         opciones[3].setEnabled(false);
  83.     }
  84. //-------------------accion de botones 3x3---------------------------------------
  85.     public void mensajetxt3(){
  86.     if(tres.enJuego){  
  87.         if(tres.turno1){
  88.             Estado.setText(tres.tImpreso);
  89.             Estado.setForeground(Color.red);
  90.         }else{
  91.             Estado.setText(tres.tImpreso);
  92.             Estado.setForeground(Color.blue);
  93.         }
  94.     }else{
  95.             Estado.setText("Felicidades jugador " +tres.Ganador);
  96.             for (int i=0;i<tateti3.length;i++){
  97.                 for(int j=0;j<tateti3[i].length;j++){
  98.                     if (tres.tablero[i][j]==0){
  99.                         tateti3[i][j].setEnabled(false);
  100.                     }
  101.                 }
  102.             }
  103.     }
  104.     }
  105.    
  106.    
  107.     //-------------------ActionEvent----------------------------------------------
  108.    
  109.    
  110.     public void actionPerformed(ActionEvent e) {
  111.             if(opciones[0]==e.getSource()){unJugador();}
  112.             if(opciones[1]==e.getSource()){DosJugadores();}
  113.             if(opciones[2]==e.getSource()){tabla3x3();}
  114.             if(opciones[3]==e.getSource()){tabla4x4();}
  115.             if(opciones[4]==e.getSource()){unJugador();}
  116.             if(opciones[5]==e.getSource()){System.exit(0);}
  117.        
  118.     //------------------------accion botones juego 3x3----------------------------
  119.         if(unJug){ 
  120.             for (int i=0;i<tateti3.length;i++){
  121.                 for (int j=0;j<tateti3[i].length;j++){
  122.                     if(tateti3[i][j]==e.getSource()){
  123.                         if(tres.turno1){
  124.                             tres.tablero[i][j]=1;
  125.                             tateti3[i][j].setEnabled(false);
  126.                             tateti3[i][j].setBackground(Color.red);
  127.                             tres.cambiarJugador();
  128.                             tres.juegoTerminado();
  129.                             mensajetxt3();
  130.                            
  131.                         }else{
  132.                             tres.tablero[i][j]=2;
  133.                             tateti3[i][j].setEnabled(false);
  134.                             tateti3[i][j].setBackground(Color.blue);
  135.                             tres.cambiarJugador();
  136.                             tres.juegoTerminado();
  137.                             mensajetxt3();
  138.                         }
  139.                     }
  140.                 }
  141.             }
  142.         }else{
  143.             if (tres.turno1){
  144.                 for (int i=0;i<tateti3.length;i++){
  145.                     for (int j=0;j<tateti3[i].length;j++){
  146.                         if(tateti3[i][j]==e.getSource()){
  147.                             tres.tablero[i][j]=1;
  148.                             tateti3[i][j].setEnabled(false);
  149.                             tateti3[i][j].setBackground(Color.red);
  150.                             tres.cambiarJugador();
  151.                             tres.juegoTerminado();
  152.                             mensajetxt3();
  153.                         }
  154.                     }
  155.                 }
  156.             }else{
  157.                 movmaq=true;
  158.                 do{
  159.                     a=(1+(int)(Math.random()*3))-1;
  160.                     b=(1+(int)(Math.random()*3))-1;
  161.                     if(tres.tablero[a][b]==0){
  162.                         tres.tablero[a][b]=2;
  163.                         tateti3[a][b].setEnabled(false);
  164.                         tateti3[a][b].setBackground(Color.blue);
  165.                         tres.cambiarJugador();
  166.                         tres.juegoTerminado();
  167.                         mensajetxt3();
  168.                         movmaq=false;
  169.                     }
  170.                 }while(movmaq);
  171.             }
  172.            
  173.         }
  174.     //----------------------------------------------------------------------------
  175.     }
  176. }