# mehrere JPanels untereinander ?



## G.Leucht (22. Mai 2008)

Hallo,

ich möchte mehrere JPanel unteinander positionieren:







mein mislungener Code  :bahnhof: 


```
panel1.setLayout(new GridBagLayout());     
	       
	       addJComponentToJPanel( panel1,smallFileLB   ,0 , 0 , 3 , 1 , 1 , 1 , GridBagConstraints.NORTH,GridBagConstraints.BOTH);
	       addJComponentToJPanel( panel1,smallPathTF   ,1 , 0 , 1 , 1 , 1 , 1 , GridBagConstraints.NORTH,GridBagConstraints.BOTH);
	       addJComponentToJPanel( panel1,smallDirBT    ,2 , 0 , 1 , 1 , 1 , 1 , GridBagConstraints.NORTH,GridBagConstraints.BOTH);
	       
	       addPanelToJFrame( panel1,0,0,1,1,1,1,GridBagConstraints.NORTH,GridBagConstraints.BOTH);
		
	       
           panel2.setLayout(new GridBagLayout());     
	       
	       addJComponentToJPanel( panel2,bigFileLB   ,0 , 0 , 3 , 1 , 1 , 1 , GridBagConstraints.NORTH,GridBagConstraints.BOTH);
	       addJComponentToJPanel( panel2,bigPathTF   ,1 , 0 , 1 , 1 , 1 , 1 , GridBagConstraints.NORTH,GridBagConstraints.BOTH);
	       addJComponentToJPanel( panel2,bigDirBT    ,2 , 0 , 1 , 1 , 1 , 1 , GridBagConstraints.NORTH,GridBagConstraints.BOTH);
	       
	       addPanelToJFrame( panel2,0,1,1,1,1,1,GridBagConstraints.NORTH,GridBagConstraints.BOTH);
```

*Methode dazu:*


```
public void addPanelToJFrame(JComponent c, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill)
	{
    	add(c, new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, new Insets(0, 0, 0, 0), 0, 0));
	}
    
    void addJComponentToJPanel(JPanel panel, JComponent c, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill ) 
    { 
    	panel.add(c, new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, new Insets(0, 0, 0, 0), 0, 0));        
    }
```


----------



## SlaterB (22. Mai 2008)

funktioniert:

```
public class TestGUI
    extends JFrame
{

    public TestGUI()
    {
        setLayout(new GridBagLayout());
        JPanel panel1 = new JPanel();
        panel1.add(new JButton("panel1"));

        JPanel panel2 = new JPanel();
        panel2.add(new JButton("panel2"));
        addPanelToJFrame(panel1, 0, 0, 1, 1, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.BOTH);
        addPanelToJFrame(panel2, 0, 1, 1, 1, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.BOTH);
        setSize(200, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void addPanelToJFrame(JComponent c, int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty,
                                 int anchor, int fill)
    {
        add(c, new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, new Insets(0, 0, 0, 0),
                                      0, 0));
    }

    public static void main(String[] args)
    {
        new TestGUI();
    }

}
```

vielleicht hast du gar kein GridBagLayout im JFrame,
ohne Code kann man dazu nix sagen


----------



## G.Leucht (22. Mai 2008)

> vielleicht hast du gar kein GridBagLayout im JFrame,
> ohne Code kann man dazu nix sagen



das hab ich vergessen :lol:


----------

