Implementieren von CardLayout in einem JFrame und Wechseln von Karten basierend auf bestimmten Tastendrücken

Ich habe meinen Code unten gepostet. Ich habe die einfache Aufgabe, eine navigierbare Benutzeroberfläche zu erstellen. Ich habe in den letzten Stunden nachgeforscht, wie dies erreicht werden kann, und dies ist der Code, den ich zusammengestellt habe.

Ursprünglich wollte ich die Navigation ohne Layouts oder irgendetwas durchführen. Ich muss das Startfenster anzeigen, nachdem der Benutzer im Begrüßungsfenster auf die Schaltfläche "Anmelden" geklickt hat.

It zeigt die Willkommenskarte gut an, aber wenn ich zur validateLogin-Methode komme (die aktiviert wird, wenn der Login-Button gedrückt wird und nach erfolgreicher Anmeldung das Home-Panel innerhalb der Karten angezeigt wird), bleibt sie einfach im Willkommens-Panel, obwohl Ich habe bestätigt, dass mein Programm die Schleife zum Kartenwechsel über system.out.Println () erreicht.

Bitte um Hilfe. Ich habe meinen ganzen Samstag damit verbracht, dieses eine Problem durch Versuche und Nachforschungen zu lösen, aber ohne Erfolg. Dies ist ein letzter Ausweg für mich. Wenn mir jemand meine Fehler zeigen kann, dann bin ich glücklich auf dem Weg und repariere sie. Wenden Sie dieses Update dann auf die vielen anderen Karten an, die für mein Programm erforderlich sind.

    enter code here
    public class mainGUI implements ActionListener{
JFrame main;
JPanel cards = new JPanel(new CardLayout());
CardLayout cl = (CardLayout)(cards.getLayout());

//Items for the welcome panel
JPanel welcome = welcomePanel();
JButton login;
JButton register;
JTextField username;
JTextField password;

//home panel
JPanel home = homePanel();


//WelcomePanel welcome = new WelcomePanel();

ArrayList<Student> students = new ArrayList<Student>();
Student workingStudent;


/**
 * calls load() at start and save() on exit
 * 
 */
public mainGUI(){
    load();

    main = new JFrame();
    main.setSize(900, 600);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setTitle("MyCourses 2k16");
    main.setContentPane(welcomePanel());

    //fill out the cards
    cards.add(welcome, "Welcome");
    cards.add(home, "Home");
            //display welcome card
    cl.show(cards, "welcome");

    main.setVisible(true);

    saveState();
}

private JPanel welcomePanel() {
    JPanel welcome = new JPanel();
    welcome.setLayout(null);
    welcome.setBackground(Color.DARK_GRAY);

    JLabel hi = new JLabel("Welcome to MyCourses 2K16");
    hi.setSize(800, 100);
    hi.setLocation(50,50);
    hi.setFont(new Font("Serif", Font.BOLD, 48));
    hi.setForeground(Color.WHITE);

    JLabel select = new JLabel("Fill in the information, then click login or register to proceed, no special characters allowed");
    select.setSize(700,100);
    select.setLocation(75,100);
    select.setFont(new Font("Serif", Font.PLAIN, 18));
    select.setForeground(Color.WHITE);

    login = new JButton( "login");
    login.setSize(100, 50);
    login.setLocation(50, 200);
    login.addActionListener(this);

    register = new JButton( "register");
    register.setSize(100,50);
    register.setLocation(200, 200);
    register.addActionListener(this);

    JLabel un = new JLabel("username");
    un.setSize(100, 30);
    un.setLocation(50, 270);
    un.setForeground(Color.WHITE);

    username = new JTextField();
    username.setSize(200, 30);
    username.setLocation(50,300);

    JLabel pw = new JLabel("password");
    pw.setSize(100, 30);
    pw.setLocation(50, 350);
    pw.setForeground(Color.WHITE);

    password = new JTextField();
    password.setSize(200, 30);
    password.setLocation(50,380);

    welcome.add(hi);
    welcome.add(select);
    welcome.add(login);
    welcome.add(register);
    welcome.add(un);
    welcome.add(username);
    welcome.add(pw);
    welcome.add(password);

    return welcome;
}

private JPanel homePanel() {
    JPanel home = new JPanel();
    home.setLayout(null);
    home.setBackground(Color.DARK_GRAY);

    JLabel hi = new JLabel("HOME");
    hi.setSize(800, 100);
    hi.setLocation(50,50);
    hi.setFont(new Font("Serif", Font.BOLD, 48));
    hi.setForeground(Color.WHITE);

    return home;

}

public void load(){

}

private void saveState(){
    Iterator<Student> it = students.iterator();
    while(it.hasNext()){
        it.next().saveStudent();
    }
}



public static void main(String[] args) {

    new mainGUI();

}


@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource()==login){
        System.out.println("Logging in...");
        validateLogin(students);
    }
    else if (e.getSource()==register){

    }
}

private void validateLogin(ArrayList<Student> students){
    boolean valid = false;

    for(int i = 0; i < students.size(); i++){

        if(username.getText().equals(students.get(i).getUsername())
                && password.getText().equals(students.get(i).getPassword()))
        {   
            valid = true;
            workingStudent=(students.get(i));
            System.out.println("Successful Login!");
            cl.show(cards, "home");
        }
    }
    if(valid == false){
        System.out.println("Invalid Login, try again");
    }

}

}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage