Probleme mit GridBagLayout

HeinerPyt

Erfahrenes Mitglied
Hallo ich habe folgenden Code:

Code:
   static void addComponent( Panel cont, 
            GridBagLayout gbl, 
            Component c, 
            int x, int y, 
            int width, int height, 
            double weightx, double weighty) 
	{ 
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.NORTHWEST;
		gbc.gridx = x; 
		gbc.gridy = y; 
		gbc.gridwidth = width; 
		gbc.gridheight = height; 
		gbc.weightx = weightx; 
		gbc.weighty = weighty; 

		gbl.setConstraints( c, gbc ); 
		cont.add( c ); 
	} 
.
.
.
.
.
	public void main()
	{
	final JFrame  win = new bodenwert(600,300);
    win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    win.setTitle("Berechnet den Bodenwert");
 
    Panel panel1 = new Panel();
    panel1.setSize(300, 300);
    GridBagLayout GBL = new GridBagLayout();
    panel1.setLayout(GBL);
    panel1.setForeground(Color.MAGENTA);
    
    addComponent(panel1, GBL ,new JLabel("Grundstücksgröße"), 0, 1, 0, 0, 1.0, 0 );
    addComponent(panel1, GBL ,new JLabel("Hinterlandgrösse"), 0, 2, 0, 0, 0,0 );
    addComponent(panel1, GBL ,new JLabel("Vorderlandgrösse"), 0, 3, 0, 0, 1.0,0 );
   addComponent(panel1, GBL ,Tgesamtgroesse, 1, 0, 0, 0, 0,0 );
  
    win.add(panel1);
    win.setVisible( true ); 

	}

Wenn ich dies ausführe werden alle Komponenten an einer Stelle übereinander dargestellt. Wenn ch bei jeder Komponente die entsprechenden Angaben (gridx, gridy, etc.) seperat mache dann funktioniert es.

Hat jemand eine Ahnung woran es liegen kann, dass ich die einzelnen Labels nicht untereinander dargestellt bekomme?

Danke
Heiner
 
Das liegt vermutlich daran, dass alle Komponenten null höhe und null breite haben.

// posX posY width height weightX weightY
addComponent(panel1, GBL ,new JLabel("Grundstücksgröße"), 0, 1, 1, 1, 1.0, 0 );
 
Zurück