GridBagLayout immer Zentriert?

oh-yeah

Grünschnabel
Hi Leute ich bin echt am verzweifeln. Diese Java-Swing-Layouts die treiben mich in den Wahnsinn. Die machen nie was ich will :D.

Ich hoffe jemand von euch kann mir helfen.

Folgendes Problem:
Ich habe einen Frame im BorderLayout.
- NORTH -> JPanel
- CENTER -> JPanel

Das Panel im Center wird ständig verändert und neu befühlt. In einem Bereich soll dort eine Formular erstellt werden, mit TextFields/-Areas.
Hab mir gedacht nimmste das GridBagLayout.
Wenn ich aber die Sachen Positioniere werden aber alle Elmente in der Mitte vom Panel Angezeigt, obwohl das Panel das Layout GridBag hat.
Ich will aber das er oben links anfängt.

Der Code der Methode, die mir das Panel im Center füllt:

PHP:
		JLabel lblPrioritaet = new JLabel("Priorit.:");
		Object[] meldearten ={"Sehr Hoch", "Hoch", "Mittel", "Niedrig", "Sehr Niedrig" };
		cbxPrioritaet = new JComboBox(meldearten);

		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		jPanelContent.setLayout(gbl);
		
		jPanelContent.setAlignmentX((float)0.1);
		jPanelContent.setAlignmentY((float)0.1);
	
		
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.anchor = GridBagConstraints.WEST;
		

		gbc.insets = new Insets(2, 2, 2, 2);

		gbc.gridx = 0;
		gbc.gridy = 1;		
		gbc.gridheight = 1; 
		gbl.setConstraints(lblPrioritaet, gbc);
		jPanelContent.add(lblPrioritaet);

		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.gridwidth = 3;
		gbl.setConstraints(cbxPrioritaet, gbc);
		jPanelContent.add(cbxPrioritaet);
		
		refreshContentScreen();

Die Methode "refreshContentScreen()" macht ein revalidate und repaint.

Hoffe mir kann einer Helfen.

Vielen Dank
W.
 
Zuletzt bearbeitet:
hi oh-yeah,
Ja das Grib-Bag-Layout bereitet so seine spezielle Freude :D
Allerdings verwende ich es recht oft, da ich mich daran gewöhnt habe.

Um etwas weiter links oder weiter rechts zu befördern verwende ich meistens Platzhalter-Labels.

Hier mal ein Beispiel aus meinem Fundus (sorry dass es sich jetzt nicht konkret auf Dein Panel bezieht)
Java:
package de.cmk.risiko.gui.game;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import de.cmk.risiko.data.CountryAssigning;
import de.cmk.util.MExceptionHandler;


public class GameConfigPanel extends JPanel implements CountryMapPanelHolding, ImageIconHolding
{
	private GridBagLayout       gridBagLayout1      = new GridBagLayout();
	
	private JLabel              m_mapL              = new JLabel();
	private JLabel              m_playerCountL      = new JLabel();
	private JLabel              m_playerNameL       = new JLabel();
	
	private JButton             m_mapChooserBut     = new JButton();
	private JSpinner            m_playerCountSpin   = new JSpinner();
	private JTextField          m_playerNameTF      = new JTextField();
	
	private CountryAssignPanel  m_countryAssignPane = new CountryAssignPanel();
	
	private CountryMapPanel     m_mapPanel          = null;
	
	private JButton             m_imageBut          = new JButton();
	private JLabel              m_imageL            = new JLabel();
	
	private ImageIcon           m_imageIcon         = null;
	
	public GameConfigPanel()
	{
        try
        {
            jbInit();
        }
        catch(Exception ex)
        {
            MExceptionHandler.handleException(ex);
        }
	}
	
	private void jbInit() throws Exception
	{
		setLayout(gridBagLayout1);
		SpinnerModel spinModel = new SpinnerNumberModel(1,1,20,1); //TODO vielleicht später aus Kartendaten ermitteln
		m_playerCountSpin.setModel(spinModel); 
		m_mapL.setText("hier stünde die Karte");
		m_playerCountL.setText("Spieleranzahl:");
		m_playerNameL.setText("Spielername:");
		m_mapL.setBackground(Color.red);
		
		m_mapL.setOpaque(true);
		
		add(new JLabel(" "), new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
		         ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
		add(m_mapChooserBut, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0
	         ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		add(new JLabel(" "), new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0
		         ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
		add(m_mapL, new GridBagConstraints(1, 0, 2, 3, 4.0, 3.0
		         ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
		
		add(m_playerCountL,   new GridBagConstraints(0, 4, 1, 1, 1.0, 1.0
		         ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		add(m_playerCountSpin,   new GridBagConstraints(1, 4, 1, 1, 1.0, 1.0
		         ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));		
		add(new JLabel(" "), new GridBagConstraints(2, 4, 1, 1, 1.0, 1.0
		         ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
		
		add(m_playerNameL,   new GridBagConstraints(0, 5, 1, 1, 1.0, 1.0
		         ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		add(m_playerNameTF,   new GridBagConstraints(1, 5, 2, 1, 1.0, 1.0
		         ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		
		add(m_imageBut,   new GridBagConstraints(0, 7, 1, 1, 1.0, 1.0
		         ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		add(m_imageL,   new GridBagConstraints(1, 6, 3, 3, 4.0, 7.0
		         ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
		
		add(m_countryAssignPane,   new GridBagConstraints(0, 10, 3, 4, 1.0, 1.0
		         ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
		
		m_mapChooserBut.setAction(new MapLoadAction("Karte laden...",this));
		
		PlayerImageLoadAction pImgLoadAction = new PlayerImageLoadAction();
		m_imageBut.setAction(pImgLoadAction);
		pImgLoadAction.setImagePathHolder(this);
		m_imageL.setBackground(Color.GREEN);
		m_imageL.setOpaque(true);
	}
	
	public CountryAssigning createCountryAssigning()
	{
		return m_countryAssignPane.createCountryAssigning();
	}
	
	public CountryMapPanel getCountryMapPanel()
	{
		return m_mapPanel;
	}
	
	public void setCountryMapPanel(CountryMapPanel cmp)
	{
		m_mapPanel = cmp;
		
		double scaleFactor = (double)m_mapL.getHeight()/(double)m_mapPanel.getWorldHeight();
		m_mapPanel.setScaling(scaleFactor);
		
		remove(m_mapL);
		
		add(m_mapPanel, new GridBagConstraints(1, 0, 2, 3, 4.0, 3.0
		         ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
		validate();
	}
	
	public int getPlayerCount()
	{
		return (Integer)m_playerCountSpin.getValue();
	}
	
	public String getPlayerName()
	{
		return m_playerNameTF.getText();
	}

	public ImageIcon getImageIcon()
	{
		return m_imageIcon;
	}

	public void setImageIcon(ImageIcon imageIcon)
	{
		m_imageIcon = imageIcon;
		double scaleFactor = 100/(double)Math.min(imageIcon.getIconWidth(),imageIcon.getIconHeight());
		
		int x = new Double((double)imageIcon.getIconWidth()  * scaleFactor).intValue();
		int y = new Double((double)imageIcon.getIconHeight() * scaleFactor).intValue();
		
		
		String tip = "<html><body><pre>"+m_playerNameTF.getText();		
		tip += "</pre>"+
				"<img src=\"file:" + imageIcon.getDescription()+ 
				"\" width="+x+" height="+y+" alt=\"Bild1\">";
		tip += "</body></html>";
		
		System.out.println(tip);
		System.out.println("ScalceFactor="+scaleFactor);
		m_imageBut.setText(tip);		
	}
}
interessant ist für Dich hier eigetnlich nur die jbInit-Routine
ich gebe im Anhang da noch ein bildchen mit damit Du eine Vorstellung hast wie es in meinem Beispiel aussähe.
 

Anhänge

  • gridbag-beispiel.jpg
    gridbag-beispiel.jpg
    49,4 KB · Aufrufe: 288
Ah ok vielen Dank.

Aber eine Frage habe ich noch. Kann man ein Feld so einstellen, damit es sich bei einem Resize nicht verändert.
Ich will ein Label und daneben ein Textfield haben. Das Feld mit dem Label sollte nicht mitwachsen. Kann man das einstellen?
 
Ah ok vielen Dank.

Aber eine Frage habe ich noch. Kann man ein Feld so einstellen, damit es sich bei einem Resize nicht verändert.
Ich will ein Label und daneben ein Textfield haben. Das Feld mit dem Label sollte nicht mitwachsen. Kann man das einstellen?
Ja das müsste IMHO auch gehen. Dazu müsste man Fülllabels z.B. links und rechts verwenden und dem eigenen Label eine feste width und ggf. height mitgeben.
Du musst da ein wenig rumexperimentieren, es könnte sein dass auch die preferedWidth / height zu setzen ist.

Tip: Entwerfe den JPanel im Center mit dem GuiEditor von NetBeans 6.5,
das geht schnell und intuitiv.
Das ist sicher auch eine Möglichkeit, jedoch muss man mit NetBeans auch erstmal umgehen lernen. Ich selbst verwende lieber JBuilder für solche Sachen, da er aus meiner Sicht einen klareren Code erzeugt und außerdem keinen Code sperrt, der, wenn man ihn doch verändert, nicht mehr interpretiert werden könnte wie bei GUI-Editoren wie Net-Beans.
Obgleich diese Möglichkeit von JBuilder schon herzlich alt ist, hat er IMHO an dieser Stelle immer noch die Nase vorn.
 
Zuletzt bearbeitet:
Zurück