Java - matriz de botones en java

 
Vista:
sin imagen de perfil

matriz de botones en java

Publicado por jose (1 intervención) el 02/11/2013 06:40:20
hola, estoy creando una matriz de botones en java, mi duda es :

el usuario ingresa el tamaño de la matriz(debe ser cuadrado), esto lo hago, pero lo que quiero ademas es que el usuario pueda ingresar los numeros que quiera para cada posicion.
hice una pequeña prueba con 2 botones al hacerles click surge un joptionpane ingreso un numero y este se guarda. lo que llevo es lo sig: lo que pido es una pequeña orientacion de como hacer lo que hice en esos 2 botones individuales hacerlo en toda esa matriz.

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel ;
import javax.swing.JLabel ;
import java.awt.GridLayout;


class Interfaz extends JFrame{

JButton [][]bMatriz;
JButton bPrueba;
JButton bPrueba2;
JPanel Panel ;
JLabel Texto;
Ventana venta;

public Interfaz(){
venta = new Ventana ();
add(getjPanel());
add(getTexto());
add(getbPrueba());
add(getbPrueba2());
inicializadorInterfaz();
}
public void inicializadorInterfaz(){

setLayout(null);
setSize(600,600);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public JButton getbPrueba(){

bPrueba = new JButton();
bPrueba.setBounds(380,50,100,70);

return bPrueba;
}
public JButton getbPrueba2(){

bPrueba2 = new JButton();
bPrueba2.setBounds(250,50,100,70);

return bPrueba2;
}



public JLabel getTexto(){

Texto = new JLabel("matriz");
Texto.setBounds(10,10,70,80);
return Texto;


}

public JPanel getjPanel(){

int num =(int)(Math.random()*10);
int filas =venta.ingresarFilas();
int columnas = filas;
JButton bMatriz [][] = new JButton[filas][columnas];
JPanel Panel= new JPanel();
Panel.setLayout(new GridLayout(filas,columnas));

for (int i=0; i< filas;i++) {
for (int j=0; j<columnas;j++) {
bMatriz[i][j] = new JButton(String.valueOf(num));
bMatriz[i][j].setBounds(20,10,360,360);

Panel.setBounds(100,100,270,300);
Panel.add(bMatriz[i][j]);
}
}

return Panel;
}
}
---------------------------------------------------------------------------------------------------------------
import javax.swing.JOptionPane;
class Ventana{

public Ventana(){}


public int ingresarFilas(){

int filas = Integer.parseInt(JOptionPane.showInputDialog(null,"Digite tamaño deseado (sera tamaño x tamaño)","Matriz cuadrada",JOptionPane.QUESTION_MESSAGE));

return filas;

}
}
-------------------------------------------------------------------------------------------------------------------------
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
class Controladora implements ActionListener{



Interfaz inter ;
Ventana venta ;
public Controladora(){

inter = new Interfaz();
//venta = new Ventana();


}

public void inicializarAcciones(){
inter.bPrueba2.addActionListener(this);
inter.bPrueba.addActionListener(this);
inter.bMatriz[0][0].addActionListener(this);
}
public void actionPerformed(ActionEvent e) {

if(e.getSource()==inter.bMatriz[0][0]){


JOptionPane.showMessageDialog(null,"hola");
}
}
}
--------------------------------------------------------------------------------------------------------

class Principal{


public static void main(String args[]){


Controladora control = new Controladora();
control.inicializarAcciones();


}



}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder