GridBagLayout

soa

Erfahrenes Mitglied
Hallo zusammen,

ich beschäftige mich seit einiger Zeit mit dem GridbagLayout.
Ich habe ein kleines Formular erstellt. Mein Problem ist, dass
unerklärlicherweise einige Labels und Textfields nicht dort
stehen wo sie stehen sollen.

Ich habe mir die folgende Funktion erstellt, um alle Komponenten platzieren zu können:

PHP:
void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy)
  {
   gbc.gridx = gx;
   gbc.gridy = gy;
   gbc.gridwidth = gw;
   gbc.gridheight = gh;
   gbc.weightx = wx;
   gbc.weighty = wy; 
  }

Am besten gebe ich nachstehend einmal den kompletten Code an:

PHP:
import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.awt.Font;
 import java.awt.Insets;
 import javax.swing.*;


 public class Gridbag extends JFrame { 

  void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy)
  {
   gbc.gridx = gx;
   gbc.gridy = gy;
   gbc.gridwidth = gw;
   gbc.gridheight = gh;
   gbc.weightx = wx;
   gbc.weighty = wy; 
  }
  
  
  
     public Gridbag() { 
      super("Gridbag Test");
     
      setSize(780,400); 
      
      //Layout im Kopfbereich 
      GridBagConstraints constraints = new GridBagConstraints();
       GridBagLayout grid = new GridBagLayout();
       JPanel p = new JPanel();
       p.setLayout(grid);
       
       
       constraints.gridwidth = GridBagConstraints.REMAINDER;
       buildConstraints(constraints, 1, 1, 2, 2, 10, 40);
       //constraints.insets = new Insets(1, 7, 1, 7);
       JLabel header = new JLabel("Meine Überschrift");
       header.setFont(new Font("Sans Serif", Font.BOLD, 16));
       grid.setConstraints(header, constraints);
       p.add(header);

       
        //anrede label
         buildConstraints(constraints, 0, 7, 1, 1, 20, 20);
         constraints.fill = GridBagConstraints.NONE;
         constraints.anchor = GridBagConstraints.EAST;
         JLabel anredeLabel = new JLabel("Anrede: ", JLabel.LEFT);
         p.add( anredeLabel);
         
         //anrede Textfeld
         buildConstraints(constraints, 1, 7, 1, 1, 20, 20);
         constraints.fill = GridBagConstraints.NONE;
         constraints.anchor = GridBagConstraints.WEST;
         JTextField anrede = new JTextField(10);
         p.add( anrede);
        
        //vorname label
        buildConstraints(constraints, 2, 8, 1, 1, 20, 20);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.EAST;
        JLabel vornameLabel = new JLabel("Vorame: ", JLabel.LEFT);
        p.add( vornameLabel);
        
        //vorname Textfeld
        buildConstraints(constraints, 3, 8, 1, 1, 20, 20);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.WEST;
        JTextField vorname = new JTextField(15);
        p.add( vorname);
        
          //name label
        buildConstraints(constraints, 0, 8, 1, 1, 20, 20);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.EAST;
        JLabel nameLabel = new JLabel("Name: ", JLabel.LEFT);
        p.add( nameLabel, constraints);
        
          //name Textfeld
        buildConstraints(constraints, 1, 8, 1, 1, 20, 20);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.WEST;
        JTextField name = new JTextField(20);
        p.add( name, constraints);
        
        
         //strasse label
        buildConstraints(constraints, 0, 9, 1, 1, 10, 20);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.EAST;
        JLabel strasseLabel = new JLabel("Strasse: ", JLabel.LEFT);
        p.add( strasseLabel, constraints);
        
          //strasse Textfeld
        buildConstraints(constraints, 1, 9, 1, 1, 30, 20);
        constraints.fill = GridBagConstraints.HORIZONTAL;
        JTextField strasse = new JTextField(17 );
        p.add( strasse, constraints);
        
        //plz label
        buildConstraints(constraints, 0, 10, 1, 1, 10, 40);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.EAST;
        JLabel plzLabel = new JLabel("Plz: ", JLabel.LEFT);
        p.add( plzLabel, constraints);
        
          //plz Textfeld
        buildConstraints(constraints, 1, 10, 1, 1, 90, 40);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.WEST;
        JTextField plz = new JTextField(15);
        p.add( plz, constraints);
        
        
         //ort label
        buildConstraints(constraints, 2, 10, 1, 1, 10, 40);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.EAST;
        JLabel ortLabel = new JLabel("Ort: ", JLabel.LEFT);
       p.add( ortLabel, constraints);
        
          //ort Textfeld
        buildConstraints(constraints, 3, 10, 1, 1, 90, 0);
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.anchor = GridBagConstraints.WEST;
        JTextField ort = new JTextField(15);
        p.add( ort, constraints);
        
         //telefon label
        buildConstraints(constraints, 0, 11, 1, 1, 19, 40);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.EAST;
        JLabel telefonLabel = new JLabel("Telefon: ", JLabel.LEFT);
        p.add( telefonLabel, constraints);
        
          //telefon Textfeld
        buildConstraints(constraints, 1, 11, 1, 1, 40, 40);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.WEST;
        JTextField telefon = new JTextField(15);
        p.add( telefon, constraints);
        
     //firma label
        buildConstraints(constraints, 0, 12, 1, 1, 19, 40);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.EAST;
        JLabel firmaLabel = new JLabel("Firma: ", JLabel.LEFT);
        p.add( firmaLabel, constraints);
        
          //firma Textfeld
        buildConstraints(constraints, 1, 12, 1, 1, 40, 40);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.WEST;
        JTextField firma = new JTextField(15);
        p.add( firma, constraints);
    
     //telefax label
        buildConstraints(constraints, 2, 11, 1, 1, 19, 40);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.EAST;
        JLabel telefaxLabel = new JLabel("Telefax: ", JLabel.LEFT);
        p.add( telefaxLabel, constraints);
        
          //telefax Textfeld
        buildConstraints(constraints, 3, 11, 1, 1, 40, 40);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.WEST;
        JTextField telefax = new JTextField(15);
       p.add( telefax, constraints);
        
     
    //email label
        buildConstraints(constraints, 0, 13, 1, 1, 19, 40);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.EAST;
        JLabel emailLabel = new JLabel("E-Mail: ", JLabel.LEFT);
        p.add( emailLabel, constraints);
        
          //email Textfeld
        buildConstraints(constraints, 1, 13, 1, 1, 90, 0);
        constraints.fill = GridBagConstraints.NONE;
        constraints.anchor = GridBagConstraints.WEST;
        JTextField email = new JTextField(15);
        p.add( email, constraints);
    
     setContentPane(p);
     
     
     } 
     
     public static void main (String [] args)
     {
      Gridbag head = new Gridbag(); 
      head.setVisible(true); 
      head.pack(); 
     }

Über Hinweise und Tipps im Umgang mit dem Layoutmanager wäre ich dankbar.

VG
 
Zurück