0

Intento crear la matriz 7x5 en un JPanel pero no me sale, he buscado en Internet y no me sirvió espero que me pueden ayudarme, puse el código

 private void ajustesFinales(){       
    GridLayout gl= new GridLayout();
    JLabel [] etiqu =new JLabel[35];
    panel01.setLayout(gl);
    for (int i = 0; i < etiqu.length;i++){ 
    etiqu[i]= new JLabel( Integer.toString(i)); 
    etiqu[i].setOpaque(true);
    etiqu[i].setAlignmentX(CENTER_ALIGNMENT);
    panel01.add(etiqu[i]);
    }    
 }

Lo que intente con filas y columnas pero no funciono

private void ajustesFinales(){       
    GridLayout gl= new GridLayout();
    JLabel [][] etiqu =new JLabel[7][5];
    panel01.setLayout(gl);
    for (int i = 0; i < etiqu.length;i++){ 
        for (int j = 0; i < etiqu.length;j++){
    etiqu[i][j]= new JLabel( Integer.toString(i,j)); 
    etiqu[i][j].setOpaque(true);
    etiqu[i][j].setAlignmentX(CENTER_ALIGNMENT);
    panel01.add(etiqu[i][j]);
        }
    }
}
2
  • Qué es exactamente lo que no funcionó? tuviste algún error ?
    – FrEqDe
    el 20 ene. 2020 a las 22:30
  • no me funciono que salga 12345 de ahí salta a otra fila 678910 y así sucesivamente, espero tu ayuda el 20 ene. 2020 a las 23:25

1 respuesta 1

2

Revisa este posible implementacion de la matriz usando un jpanel y labels

public static void main(String[] args) {


    JFrame window = new JFrame("my app");
    window.setBounds(0,0,400,400);
    window.setLayout(new BorderLayout());


    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(7,5));

    int counter =1;
    for(int i=0;i<5;i++){
        for(int j=0;j<7;j++){
            JLabel label = new JLabel("My text "+counter);
            panel.add(label);
            counter++;
        }
    }
    window.add(panel);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

introducir la descripción de la imagen aquí

1
  • Muchas gracias ese es que necesitaba el 22 ene. 2020 a las 20:53

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.