# Rahmen um Layout ziehen



## joern1 (19. Aug 2007)

Ich denke, dass ich hier ein ziemlich einfaches Problem habe, bei dem ich nicht weiterkomme. Leider habe ich
hier im Forum auch nichts Passendes gefunden.

Ich habe ein GridbagLayout und würde gern um dieses Layout einen Rahmen setzen. Dazu habe ich dann
das Layout in eine Methode 

private void initiate() ausgelagert. Diese Methode soll dann im Konstruktor so übergeben werden, dass ein
Rahmen drum herum erscheint.

Mein Code:


```
import java.awt.Component;
    import java.awt.GridBagConstraints;
	import java.awt.GridBagLayout;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.CompoundBorder;
    import javax.swing.border.TitledBorder;


	public class View extends JPanel { 

		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 View() { 
			   
			  
			     
		       
			      JPanel textAreaWithBorder = new JPanel(); 
			      textAreaWithBorder.setBorder(new CompoundBorder(new TitledBorder(null, 
			                "Kundendaten: ", TitledBorder.CENTER, TitledBorder.TOP), null)); 
			      textAreaWithBorder.add(initiate());  <----   Böser FEHLER
			      //textAreaWithBorder.add(); 
			      add(textAreaWithBorder); 
			   
			   
				
		  	
		  	
		   } // Konstruktor
		   
		   private void initiate(){
			   
//			 Layout im Kopfbereich 
			   GridBagConstraints constraints = new GridBagConstraints();
			    GridBagLayout grid = new GridBagLayout();
			    setLayout(grid);
			    
			    String[] str = { "Herr", "Frau", "Frau Dr.", "Herr Dr.", "Herr Prof." };

		        
		        //petList.addActionListener(this);
			    
			    	//leer label
			      buildConstraints(constraints, 0, 6, 1, 1, 20, 20);
			      constraints.fill = GridBagConstraints.NONE;
			      constraints.anchor = GridBagConstraints.EAST;
			      JLabel leerLabel = new JLabel("  ", JLabel.LEFT);
			      add( leerLabel, constraints);
			    
			    	//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);
			      add( anredeLabel, constraints);
			      
			      //anrede Textfeld
			      buildConstraints(constraints, 1, 7, 1, 1, 20, 20);
			      constraints.fill = GridBagConstraints.NONE;
			      constraints.anchor = GridBagConstraints.WEST;
			      JComboBox pet = new JComboBox(str);
			        pet.setSelectedIndex(4);
			      add( pet, constraints);
		      
		      //vorname label
		      buildConstraints(constraints, 0, 8, 1, 1, 20, 20);
		      constraints.fill = GridBagConstraints.NONE;
		      constraints.anchor = GridBagConstraints.EAST;
		      JLabel vornameLabel = new JLabel("  Vorame: ", JLabel.LEFT);
		      add( vornameLabel, constraints);
		      
		      //vorname Textfeld
		      buildConstraints(constraints, 1, 8, 2, 1, 20, 20);
		      constraints.fill = GridBagConstraints.NONE;
		      constraints.anchor = GridBagConstraints.WEST;
		      JTextField vorname = new JTextField(15);
		      add( vorname, constraints);
		      
	         //name label
		      buildConstraints(constraints, 2, 8, 1, 1, 80, 80);
		      constraints.fill = GridBagConstraints.NONE;
		      constraints.anchor = GridBagConstraints.EAST;
		      JLabel nameLabel = new JLabel("        Name: ", JLabel.LEFT);
		      add( nameLabel, constraints);
		      
	         //name Textfeld
		      buildConstraints(constraints, 3, 8, 1, 1, 20, 20);
		      constraints.fill = GridBagConstraints.NONE;
		      constraints.anchor = GridBagConstraints.WEST;
		      JTextField name = new JTextField(20);
		      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);
		      add( strasseLabel, constraints);
		      
	         //strasse Textfeld
		      buildConstraints(constraints, 1, 9, 1, 1, 30, 20);
		      constraints.fill = GridBagConstraints.HORIZONTAL;
		      JTextField strasse = new JTextField(15 );
		      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);
		      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);
		      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);
		      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);
		      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);
		      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);
		      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);
		      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);
		      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);
		      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);
		      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);
		      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);
		      add( email, constraints);
			   
		   }
		   
		 
		   
		} //  End of class
```

Über Hinweise wäre ich dankbar.  Es ist wahrscheinlich auch gar nicht nötig, einen neuen Panel zu erzeugen oder ?


Viele Grüße
Jörn


----------



## Marco13 (19. Aug 2007)

Warum machst du nicht im Konstruktor einfach
this.setBorder(bla...);
!?


----------



## joern1 (20. Aug 2007)

ja, scheint tatsächlich zu funktionieren.



```
this.setBorder(new CompoundBorder(new TitledBorder(null, 
			                "Kundendaten: ", TitledBorder.CENTER, TitledBorder.TOP), null)); 
			      initiate();
```

setzt den Rahmen um alle Elemente, die initiate() erzeugt.

 :applaus: Perfekt.


----------

