Ver Mensaje Individual
  #3 (permalink)  
Antiguo 30/03/2016, 11:51
jolut11
 
Fecha de Ingreso: marzo-2016
Mensajes: 19
Antigüedad: 8 años, 1 mes
Puntos: 0
Respuesta: Problema con GridBagLayout

Lo que pasa es que para hacer esto me estoy guiando de este ejemplo:

import javax.swing.*;
import java.awt.*;
public class demoGBL {
static void ShowGUI(){
//Sin este JFrame no hay apliocación
JFrame principal = new JFrame (“Demo – GridBagLayout”);
JLabel logo = new JLabel (new ImageIcon(“imagenes/dhc_logo_1.png”));
JLabel team = new JLabel (“DaHouseCat – http://blog.dahousecat.net”);
JButton b1 = new JButton (“Boton1”);
JButton b2 = new JButton (“Boton2”);
JButton b3 = new JButton (“Boton3”);
JButton b4 = new JButton (“Boton4”);
JButton b5 = new JButton (“Boton5”);
JButton b6 = new JButton (“Boton6”);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
//Los insets son los bordes externos de una aplicación, o sea el espacio entre cada componente, new Insets (arriba,izquierda,abajo,derecha);
gbc.insets = new Insets(3,3,3,3);
//Asignamos la constante EXIT_ON_CLOSE, cierra la ventana al pulsar la X.
principal.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);
//Asignamos al JFrame el Layout que usará, GridBagLayout
principal.setLayout (gridbag);
//Se agrega el primer boton al JFrame
gbc.weightx = 0.5;
gbc.gridx = 0;
gbc.gridy = 1;
principal.add (b1,gbc);
gbc.weightx = 0.5;
gbc.gridx = 1;
gbc.gridy = 1;
principal.add (b2,gbc);
gbc.weightx = 0.5;
gbc.gridx = 2;
gbc.gridy = 1;
principal.add (b3,gbc);
gbc.weightx = 0.5;
gbc.gridx = 0;
gbc.gridy = 2;
principal.add (b4,gbc);
gbc.weightx = 0.5;
gbc.gridx = 1;
gbc.gridy = 2;
principal.add (b5,gbc);
gbc.weightx = 0.5;
gbc.gridx = 2;
gbc.gridy = 2;
principal.add (b6,gbc);
//Ahora agregaremos el logotipo y el pie de la aplicacion.
gbc.weightx = 0.5;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 3;
//Se utilizan para acceder a costantes, esta significa que el componente debe llenar la celda a lo ancho.
gbc.fill = GridBagConstraints.HORIZONTAL;
principal.add (logo,gbc);
gbc.weightx = 0.5;
gbc.gridx = 0;
gbc.gridy = 3;
//Asignamos que la Label tenga un ancho de 3 celdas
gbc.gridwidth = 3;
//Alineación del texto dentro del componente Label
team.setHorizontalAlignment(JLabel.CENTER);
principal.add (team,gbc);
//Hace visible el panel
principal.setVisible(true);
//Ajusta la ventana a los componentes que haya
principal.pack();
}
public static void main (String[] args) {
ShowGUI();
}
}

Y ahí no hay problema con el setLayout, qué parte es la que evita el fallo?