Swing Problem mit GridBagLayout

hdi

Top Contributor
Hi,

also neulich bin ich noch mit dem GBL klargekommen, aber jetzt plötzlich raff ich wieder irgendwas nicht. Hab jetzt ewig überlegt aber ich glaub ich vergess da etwas aber komm einfach nicht drauf 🙁

Startet mal bitte folgendes KSKB:

Java:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GBLDemo extends JFrame {

    public GBLDemo() {
	JPanel content = new JPanel(new GridBagLayout());
	content.setBackground(Color.red);

	JPanel p1 = new JPanel(new FlowLayout());
	p1.add(new JButton("bla"));
	p1.add(new JButton("blub"));
	GridBagConstraints p1_c = new GridBagConstraints();
	p1_c .anchor = GridBagConstraints.WEST;
	p1_c .fill = GridBagConstraints.NONE;
	p1_c .gridx = 0;
	p1_c .gridy = 0;
	content.add(p1, p1_c );
	
	JPanel p2 = new JPanel(new FlowLayout());
	p2.add(new JButton("xyz"));
	p2.add(new JButton("abcd"));
	p2.add(new JButton("sfdsdf"));
	GridBagConstraints p2_c = new GridBagConstraints();
	p2_c .anchor = GridBagConstraints.WEST;
	p2_c .fill = GridBagConstraints.NONE;
	p2_c .gridx = 0;
	p2_c .gridy = 1;
	content.add(p2, p2_c );

	setLayout(new BorderLayout());
	getContentPane().add(content, BorderLayout.CENTER);
	pack();
	setLocationRelativeTo(null);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public static void main(String[] args) {
	SwingUtilities.invokeLater(new Runnable(){
	    @Override
	    public void run() {
	        new GBLDemo().setVisible(true);
	    }
	});
    }
}

Frage: Wieso bleiben die 2 Panels beim Vergrößern des Fensters nicht oben links im Content-Panel angedockt? Ich sag doch anchor = WEST, und fill = NONE, also versteh ich nich wieso sie sich in die Mitte schieben.

Was verpeil ich hier??

Danke!
 
Oh, ich hab irgendwie gedacht ich will ja eben dass es sich nicht vergrössert daher weight = 0. Aber habs jetzt verstanden 🙂 Thx!
 

Zurück
Oben