JPanel mittig auf JPanel

vrcat

Mitglied
Hallo zusammen ich habe mal wieder einHallo zusammen ich habe mal wieder eine Frage. Diesmal handelt es sich um ein Layout Problem. Ich möchte gerne, dass auf einem JPanel ein zweites JPanel Mittig positioniert wird.
Mit dem BorderLayout dachte ich sollte das kein Problem sein. Jedoch wird das JPanel dort aber in der Grösse des anderen JPanels auf dem es dargestellt werden soll angezeigt.

Wie bekomme ich es nun hin das mein geaddetes JPanel ne eigene Grösse hat und mittig auf dem darunter liegendem JPanel Positioniert ist.

DANKE FÜR EURE HILFE
e Frage. Diesma
 
Beispiel mit GridBagLayout (geht allerdings "kaputt", wenn das Fenster zu klein wird, aber ich vermute, das ändert sich, wenn das innere Panel auch Inhalt hat):
Code:
package panelMittig;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 
 * @author Hans Peter von Welt
 */
public class PanelMittigInPanelFrame extends JFrame {

    public PanelMittigInPanelFrame() {
        super("Panel mittig in Panel");
        this.setSize(600, 600);
        
        GridBagLayout gBL = new GridBagLayout();
        GridBagConstraints gBC = new GridBagConstraints();

        //Äußeres Panel
        JPanel p = new JPanel();
        p.setLayout(gBL);

        //Inneres Panel
        JPanel mitte = new JPanel();
        mitte.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        mitte.setPreferredSize(new Dimension(400, 400));

        this.getContentPane().setLayout(new BorderLayout());

        //Panel für ringsherum
        JPanel spacer;
        
        //Oberen Platz schaffen (entspricht BorderLayout.NORTH)
        spacer = new JPanel();

        gBC.fill = GridBagConstraints.VERTICAL;
        gBC.weightx = 0.0;
        gBC.weighty = 0.0;
        gBC.gridwidth = GridBagConstraints.REMAINDER;

        gBL.setConstraints(spacer, gBC);
        p.add(spacer);
        
        //Linken Platz schaffen (entspricht BorderLayout.WEST)
        spacer = new JPanel();

        gBC.fill = GridBagConstraints.BOTH;
        gBC.weightx = 0.0;
        gBC.weighty = 0.0;
        gBC.gridwidth = 3;

        gBL.setConstraints(spacer, gBC);
        p.add(spacer);
        

        //Mittiges Panel einfügen (entspricht BorderLayout.CENTER)
        gBC.fill = GridBagConstraints.NONE;
        gBC.weightx = 1.0;
        gBC.weighty = 1.0;

        gBL.setConstraints(mitte, gBC);
        p.add(mitte);
                
        
        //Rechten Platz schaffen (entspricht BorderLayout.EAST)
        spacer = new JPanel();

        gBC.fill = GridBagConstraints.BOTH;
        gBC.weightx = 0.0;
        gBC.weighty = 0.0;
        gBC.gridwidth = GridBagConstraints.REMAINDER;

        gBL.setConstraints(spacer, gBC);
        p.add(spacer);
        

        //Unteren Platz schaffen (entspricht BorderLayout.SOUTH)
        spacer = new JPanel();

        gBC.fill = GridBagConstraints.VERTICAL;
        gBC.weightx = 0.0;
        gBC.weighty = 0.0;
        gBC.gridwidth = GridBagConstraints.REMAINDER;

        gBL.setConstraints(spacer, gBC);
        p.add(spacer);
        
        //Äußeres Panel dem JFrame hinzufügen 
        this.getContentPane().add(p);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.show();
    }

    public static void main(String[] args) {
        new PanelMittigInPanelFrame();
    }
}
Gruß hpvw
 
Hallo!

Schau mal hier:
Code:
/**
 * 
 */
package de.tutorials;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @author Tom
 * 
 */
public class JPanelinPanelExample extends JFrame {

	public JPanelinPanelExample() {
		super("JPanelinPanelExample");
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		JPanel jPanel1 = new JPanel();
		JPanel jPanel2 = new JPanel();

		jPanel1.setLayout(new GridBagLayout());

		jPanel1.setBackground(new Color(51, 153, 255));
		jPanel1.setPreferredSize(new Dimension(320, 240));
		jPanel2.setBackground(new Color(255, 153, 0));
		jPanel2.setPreferredSize(new Dimension(160, 120));
		jPanel1.add(jPanel2, new GridBagConstraints());

		add(jPanel1, BorderLayout.CENTER);

		pack();
		setVisible(true);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new JPanelinPanelExample();
	}

}

Gruß Tom
 
Super danke habe schon was mit add("Center",panel) gefunden geht aber leider nur in X-Richtung. Aber mit eurem Quelltext hauts ordentlich hin danke nochmal.
 
Ups, da war ich wohl etwas zu umständlich.

@Tom:
JFrame mit add? Ich dachte, man soll da getContentPane().add nehmen?
Habe gerade bei mir nochmal getestet, nur mit add gibts einen java.lang.Error. (Eclipse 3.irgendwas, J2 SDK 1.4.2_03)

Gruß hpvw
 
Zurück