JLayeredPane funktioniert nicht innerhalb des GridBagLayouts

swalbking

Mitglied
Hallo,
ich würde gern in ein Panel mit GridBagLayout Komponenten einfügen. Jede dieser Komponenten besteht aus einem Hintergrundbild und einem daraufliegendem Button. Bei Klick auf diesen Button soll an dessen Stelle ein kleines Bild angezeigt werden.
Allerdings zeigt der folgende Code leider nur ein leeres Fenser an:
Code:
public class JLayeredPaneTest  extends JFrame  {
	    JPanel map;
	    JLabel     bildHinten, bildVorne;
	    int x = 45;
            int cx = x/3;
	    JLayeredPane jlp = new JLayeredPane();

	    JLayeredPaneTest()
	    {
	    	map = new JPanel();
		    map.setBackground(Color.WHITE);
		    map.setLayout(new GridBagLayout());
		    this.add(map, BorderLayout.CENTER);   
		    
	        bildHinten = new JLabel();
	        bildHinten.setIcon(new ImageIcon("gross.png"));
	        bildHinten.setBounds(0,0,x,x);
	        bildHinten.setVisible(true);
	
	        bildVorne = new JLabel();
	        ImageIcon ii = new ImageIcon("klein.png");	
			bildVorne.setIcon(ii);
	        
	        final JButton button = new JButton( );

	        button.setBounds(cx*7/6,0,cx*2/3,cx*2/3);
	        button.addActionListener( new ActionListener() {
		    	public void actionPerformed(ActionEvent e){	
		    			jlp.removeAll();
		    			jlp.add(bildHinten,1);
		    			bildVorne.setBounds(button.getX()-1/6*cx,0,cx,cx);
		    			jlp.add(bildVorne,0);
		    	}
		    });
	        
	        jlp.add(bildHinten, 1);
	        jlp.add(button, 0);

	        GridBagConstraints constr = new GridBagConstraints();
		    constr.gridx = 0;	
		    constr.gridy = 0;	
		    constr.anchor = GridBagConstraints.CENTER;	
			   
	        map.add(jlp, constr);	        
	    }

	    public static void main(String[] args) {
	    	JLayeredPaneTest test= new JLayeredPaneTest();
	        test.setVisible(true);
	        test.setSize(600, 400);
	    }
}

Wenn ich das Layout von "map" auf GridLayout ändere, funktioniert alles wie gewünscht. Allerdings bin ich auf das GridBagLayout angewiesen :-(

Kann mir jemand helfen?

Gruß,
Swalbking
 
Nach unzähligen Versuchen, hier die Lösung:

Die Zeile
Code:
jlp.setPreferredSize(new Dimension(x,x));
muss hinzugefügt werden und alles funktioniert wie gewünscht :)
 

Neue Beiträge

Zurück