Positionierung der Elemente beim GridBagLayout

Aurelie

Grünschnabel
Hi ihr!

Mein Problem ist kurz beschieben und ich hoffe auch genau schnell zu lösen.
Ich benutze das GridBagLayout und möchte, dass die Elemente oben kleben bleiben, wenn man die Größe des Fensters verändert.
Das sollte ja eigentlich mit achor gehen, aber das tut es bei mir nicht. Benutze ich es falsch?

Hier mein Code und ein Bild, wie es bei mir aussieht.

Code:
public class Hauptfenster
extends JPanel {

	public Hauptfenster(boolean appletMode) {
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc;
		setLayout(gbl);
		
		gbc = makegbc(0,0,1,1);
		gbc.weighty = 100;
		gbc.fill = GridBagConstraints.VERTICAL;
		gbc.anchor = GridBagConstraints.NORTH;
		JLabel label1 = new JLabel("Hello");
		gbl.setConstraints(label1,gbc);
		add(label1);

		gbc = makegbc(1,0,1,1);
		gbc.weightx = 100;
		gbc.weighty = 100;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.anchor = GridBagConstraints.NORTH;
		JLabel label2 = new JLabel("Kitty");
		gbl.setConstraints(label2,gbc);
		add(label2);
	}

	private GridBagConstraints makegbc (int x, int y, int width, int height) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.insets = new Insets(1,1,1,1);
		return gbc;
	}
}

Lieben Gruß
Aurelie
 

Anhänge

  • AnchorProb.gif
    AnchorProb.gif
    4,3 KB · Aufrufe: 160
Hallo!

Schau mal hier:
Code:
 package de.tutorials;
 
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 
 public class GridBagLayoutExample extends JFrame {
 
 	public GridBagLayoutExample() {
 		super("GridBagLayoutExample");
 		setDefaultCloseOperation(EXIT_ON_CLOSE);
 
 		GridBagLayout gbl = new GridBagLayout();
 		GridBagConstraints gbc;
 		setLayout(gbl);
 
 		gbc = new GridBagConstraints();
 		gbc.gridx = 0;
 		gbc.gridy = 0;
 		gbc.weightx = 1.0;
 		gbc.weighty = 1.0;
 		gbc.fill = GridBagConstraints.NONE;
 		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
 		JLabel label1 = new JLabel("Hello");
 		gbl.setConstraints(label1, gbc);
 		add(label1);
 
 		gbc = new GridBagConstraints();
 		gbc.gridx = 1;
 		gbc.gridy = 0;
 		gbc.weightx = 1.0;
 		gbc.weighty = 1.0;
 		gbc.fill = GridBagConstraints.NONE;
 		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
 		JLabel label2 = new JLabel("Kitty");
 		gbl.setConstraints(label2, gbc);
 		add(label2);
 
 		pack();
 		setVisible(true);
 	}
 
 	/**
 	 * @param args
 	 */
 	public static void main(String[] args) {
 		new GridBagLayoutExample();
 	}
 
 }

Gruß Tom
 

Neue Beiträge

Zurück