Ver Mensaje Individual
  #4 (permalink)  
Antiguo 05/07/2011, 16:55
Rymura
 
Fecha de Ingreso: febrero-2011
Ubicación: Madrid
Mensajes: 20
Antigüedad: 13 años, 2 meses
Puntos: 4
Respuesta: Peroblema al borrar JtextFields de un Panel

muchas gracias new, ya he implementado el cambio y funciona perfectamente. Tiene narices que no pensara en hacer un array y tuviera en mente usar una lista de punteros, madre mia ^^!

Por lo demas he aprovechado para mejorar algunas funciones de verificacion de datos y he creado una matriz tableroAux para hacer las futuras operaciones en el, ya que seran mas faciles sobre ella.

Os dejo el codigo por si os interesa :D

Código:
package calc;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class aplicacion extends JFrame implements ActionListener {

	
	
    private JTextField t;
    JPanel p = new JPanel();
    JPanel cp = (JPanel) this.getContentPane();
    JTextField[][] tablero = new JTextField[9][9];
    //tableroAux no tendra - sino que al ser int usara el 0 como referencia
    int [][] tableroAux = new int[9][9]; 

    
    
    public aplicacion() {
        super("Sudoku Finish 0.1");
        String labelButtons[] = {"Resolver", "Borrar"};
        cp.setLayout(new BorderLayout());
        p.setLayout(new GridLayout(10,9));
        //Casillas del sudoku
        for (int i = 0; i < 9; i++) {
        	
          for (int j = 0; j < 9; j++) {

        	tablero[i][j] = new JTextField();
            tablero[i][j].setText("-");
            tableroAux[i][j]=0;


          System.out.println("hola"+i+j);
           
            tablero[i][j].addFocusListener(new FocusListener()
            {
            	   public void focusLost(FocusEvent e) 
            	   {
            	      String s = ((JTextField)e.getSource()).getText();
            	      char ar[] = s.toCharArray();
            	      
            	      if ((ar[0] != '-'))
            	      {
            	      if (((ar[0] == '1') || (ar[0] == '2')  || (ar[0] == '3') || (ar[0] == '4') || (ar[0] == '5') || (ar[0] == '6') || (ar[0] == '7') || (ar[0] == '8') || (ar[0] == '9')) && (ar.length == 1))
            	      	{
            	    	  ((JTextField)e.getSource()).setText(s);
            	          t.setText("Numero Introducido Correctamente");
            	      	}
            	      else
            	      { 
            	    	((JTextField)e.getSource()).setText("-");
            	        t.setText("Error al introducir un numero");
            	      } 
            	     }
            	      else
            	      {
              	    	((JTextField)e.getSource()).setText("-");
            	        t.setText("Error al introducir un numero");  
            	      }
            	     
            	   }
            	   
            	   public void focusGained(FocusEvent e) {
            	      // No hacemos nada
            	   }
            });
            
            p.add(tablero[i][j]);
            
          }  
            
        }
        
        
        JPanel pz = new JPanel();
        //Botones del sudoku
        for (int i = 0; i < labelButtons.length; ++i) {
            JButton button = new JButton(labelButtons[i]);
            button.addActionListener(this);
            pz.add(button);
        }
        
        //cuadro de texto informativo
        t = new JTextField();
        t.setHorizontalAlignment(JTextField.RIGHT);
        t.setText("0");
        cp.add(t, BorderLayout.PAGE_START);
        cp.add(p, BorderLayout.CENTER);
        cp.add(pz, BorderLayout.SOUTH);
        this.setSize(500, 500);
        this.setVisible(true);
        this.setDefaultCloseOperation(aplicacion.EXIT_ON_CLOSE);
        
        
        
        
        
        
    }

    public static void main(String[] args) {
        new aplicacion();
    }

    

    
    
    public void actionPerformed(ActionEvent e) {
        char c = ((JButton) e.getSource()).getText().charAt(0);
        switch (c)
        {
           case 'B':
        	   
        	   //bucle doble FOR para cambiar el valor tanto de tableroAux como de tablero normal
        	   for (int i=0; i<9; i++ )
        	   {
        		   for (int j=0; j<9; j++ )
            	   {
        			  tableroAux[i][j]=0;
        			  tablero[i][j].setText("-");
            	   }
        	   }
        	   t.setText("Sudoku Borrado con Exito");
        	   break;
                    
                    
         }
        

      //  System.out.print(" ");
   
    }
}