GridBagLayout und JPanel

Pherseus

Erfahrenes Mitglied
Ich habe folgendes Code-Beispiel:

Code:
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import util.Examples;

public class Bla extends JFrame{
    public Bla() {
        super("Graph");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
     
        Panel Panel = new Panel();
        add(Panel);
        setSize(600,600);
        setVisible(true);
    }
    
    public static void main(String[] args) {
        new Bla();
    }
}

class Panel extends JPanel {
    
    public Panel() { 
        setLayout(new GridBagLayout());
        setPreferredSize(new Dimension(1000,1000));
        addComponent(this, (GridBagLayout) getLayout(), new JLabel("das"), 0, 0, 2, 1, 0, 0);
        addComponent(this, (GridBagLayout) getLayout(), new JLabel("asdf"), 1, 0, 2, 1, 0, 0);    
    }
    
    static void addComponent( Container 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; 
        
        //position
        gbc.gridx = x; 
        gbc.gridy = y; 
        
        // größe der reihe/spalte
        gbc.gridwidth = width; 
        gbc.gridheight = height; 
        
        // bei zuviel platz
        gbc.weightx = weightx; 
        gbc.weighty = weighty; 
        
        gbl.setConstraints( c, gbc ); 
        cont.add( c ); 
    }

}
Leider werden auf dem JPanel nicht beide Labels angezeigt, sondern nur das zweite und dieses dazu in der mitte. Wo liegt mein Fehler?
 
Zuletzt bearbeitet:
Zurück