Problem mit CardLayoit

Technoblade

Erfahrenes Mitglied
Hi,
ich hab mir ein kleines Testprogramm geschrieben, um für eine Anwendung mit dem CardLayout umgehen zu können. Allerdings funktioniert das layout leider nicht so wie es meiner Meinung nach sollte, und mir sind mittlerweile die Ideen ausgegangen wp der Fehler im kurzen Quelltext sein könnte. Hab mir schon viele Beispiele zum CardLayout angeguckt, finde aber nirgends den entscheidenden Unterschied. Ich hoffe hier findet sich jemand dem der Fehler möglichst schnell auffällt.

Nunja, hier der kurze unkommentierte Quelltext.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test
{

    public static void main(String[] args)
    {
        JFrame fenster = new JFrame("test");
        Container c = fenster.getContentPane();
        c.setLayout(new FlowLayout());
        
        final JPanel spez = new JPanel();
        final CardLayout cl = new CardLayout(); 
        spez.setLayout(cl);
        
        String[] test01 = {"LOL", "lol", "Lol"};
        String[] test02 = {"OMG", "omg", "Omg"};
        String[] test03 = {"WTF", "wtf", "Wtf"};
        
        JComboBox auswahlLol = new JComboBox(test01);
        JComboBox auswahlOmg = new JComboBox(test02);
        JComboBox auswahlWtf = new JComboBox(test03);
        
        cl.addLayoutComponent(auswahlLol, "lol");
        cl.addLayoutComponent(auswahlOmg, "omg");
        cl.addLayoutComponent(auswahlWtf, "wtf");
        cl.show(spez, "lol");
        
        String[] test04 = {"lol", "omg", "wtf"};
        JComboBox auswahl = new JComboBox(test04);
        auswahl.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent e)
                                    {
                                        JComboBox j = (JComboBox) e.getSource();
                                        cl.show(spez, (String) j.getSelectedItem());
                                    }
                                });
                                
        c.add(auswahl);
        c.add(spez);
        fenster.setVisible(true);
        fenster.pack();
    }
}
 
Zurück