gridlayout not functional in java se

0

First of all I create a JFrame with borderlayout distribution.

Then I create an object of class JMenubar by calling a method where a complete menu is built.

This object is located in the north zone of JFrame .

Then create a JPanel that is located in the central zone, this JPanel is composed by: JLabel , JTextField , etc.

When we place the focus on a concrete%% of%, a small JTextField is displayed with a default distribution.

To this JPanel we add a new one with distribution JPanel in which we place several buttons.

Later we add a new null with distribution JPanel .

In this last GridLayout we want to add to each cell a JPanel .

The problem manifests when we check that no JPanel appears in any cell, besides not behaving like a JLabel .

    
asked by manucego 22.01.2018 в 18:58
source

2 answers

0

What I wanted to say, is that the cells of the gridlayout, what I add are jlabel.

As soon as I can publish the code. thanks for answering. Greetings.

    
answered by 23.01.2018 в 19:07
0

What I wanted to say, is that the delgridlayout cells, what I add are jlabel.

As soon as I can publish the code. thanks for answering. Greetings.

There goes the code.

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

public class Calendar extends JPanel {     private int x, y;     private int width, height;     private JButton btnAdelantar, btnAtar;     private JLabel etiqMesAño;

Calendario(int x, int y){
    //situa y da tamaño al jpanel
    alto = Ventana.CoordenadaY(3.6F);
    ancho = Ventana.CoordenadaX(3.28F);
    setBounds(x, y, ancho, alto);

    CrearInterfaz();
}

private void CrearInterfaz(){
    //matriz de tres jpanel para construir las tres partes del calendario
    JPanel paneles[]=new JPanel[3];

    //divide el jpanel en ocho filas y siete columnas
    int a = alto/8, b = ancho/7;

    //matriz de los dias de la semana
    JLabel etiqDiasSemana[]=new JLabel[7];
    String strDiasSemana[]={"Lun","Mar","Mie","Jue","Vie","Sab","Dom"};

    //codigos de los iconos del primer panel
    String flechaAtras=String.valueOf('\u25C4');
    String flechaAdelante=String.valueOf('\u25BA');

    //construye los tres jpanel que se agregan al principal
    for(int i=0;i<paneles.length;i++){
        paneles[i]=new JPanel();
        add(paneles[i]);

        switch(i){
            //construccion del primer panel donde van los botones y el mes y año seleccionado
            case(0):
                paneles[i].setBounds(0, 0, ancho, a);
                paneles[i].setLayout(null);
                paneles[i].setBackground(Ventana.ColorEtiquetas);

                //calcula tamaño de los botones y su situacion en el jpanel superior del calendario 
                int altoBotones = 5*(a/6);
                int anchoBotones = 2*(b/3);
                int y1 = (a - altoBotones) / 2;
                int x1 = anchoBotones / 3;
                int x2 = ancho - x1 - anchoBotones;

                btnAtrasar=new JButton(flechaAtras);
                btnAtrasar.setBounds(x1, y1, anchoBotones, altoBotones);
                btnAtrasar.setBackground(Ventana.ColorEtiquetas);
                btnAtrasar.setBorder(Ventana.BordeBotones);
                btnAtrasar.setHorizontalAlignment(JButton.CENTER);
                btnAtrasar.setForeground(new Color(0,0,255));
                paneles[i].add(btnAtrasar);

                btnAdelantar=new JButton(flechaAdelante);
                btnAdelantar.setBounds(x2, y1, anchoBotones, altoBotones);
                btnAdelantar.setBackground(Ventana.ColorEtiquetas);
                btnAdelantar.setBorder(Ventana.BordeBotones);
                btnAdelantar.setHorizontalAlignment(JButton.CENTER);
                btnAdelantar.setForeground(new Color(0,0,255));
                paneles[i].add(btnAdelantar);

                etiqMesAño=new JLabel();
                etiqMesAño.setFont(Ventana.FuenteMenu);
                etiqMesAño.setBounds(ancho/4, a/4, ancho/2, a/2);                   
                etiqMesAño.setHorizontalAlignment(JLabel.CENTER);
                paneles[i].add(etiqMesAño);

                add(paneles[i]);
                break;

            case(1):
                paneles[i].setBounds(0, a, ancho, a);
                paneles[i].setLayout(new GridLayout(0,7));

                for(int j=0;j<etiqDiasSemana.length;j++){
                    etiqDiasSemana[j]=new JLabel(strDiasSemana[j]);                       
                    etiqDiasSemana[j].setBounds(0, a, b, a);
                    etiqDiasSemana[j].setFont(Ventana.FuenteMenu);
                    etiqDiasSemana[j].setHorizontalAlignment(JLabel.CENTER);
                    etiqDiasSemana[j].setBorder(Ventana.BordeBotones);
                    if(j==6)etiqDiasSemana[j].setForeground(Color.BLACK);
                    else etiqDiasSemana[j].setForeground(Color.RED);
                    paneles[i].add(etiqDiasSemana[j]);
                }

                add(paneles[i]);                    

                break;
            case(2):
                paneles[i].setLayout(new GridLayout(6,7));
                paneles[i].setBounds(0, 2*a, ancho, alto-2*a);
                paneles[i].setBackground(Color.WHITE);

                add(paneles[i]);
                break;
        }
    }
}

}

    
answered by 24.01.2018 в 18:25