Wie verstecke ich das aktuelle JPanel und zeige ein neues mit einer Schaltfläche in Java an?

Ich muss in diesem Programm leider mehrere Fenster verwenden und ich glaube nicht, dass CardLayout funktionieren wird, da zwischen den verschiedenen Layouts keine Schaltflächen konstant sein können. Ich versuche also, eine Schaltfläche zu codieren, um das aktuelle JPanel (thePanel) auszublenden und ein neues (thePlacebo) anzuzeigen.

Ich versuche, das Panel in einem ActionListener wie folgt auszublenden:

frame.getContentPane().remove(thePanel);

Ich dachte, das würde funktionieren, aber es friert mein Programm nur ein, sobald ich den Knopf drücke.

Hier ist ein Teil des Codes für den Kontext:

public class Reflexology1 extends JFrame{
JButton button1, button2;
JButton movingButton;
JTextArea textArea1;
int buttonAClicked, buttonDClicked;
private long _openTime = 0;
private long _closeTime = 0;
JPanel thePanel = new JPanel();
JPanel thePlacebo = new JPanel();
final JFrame frame = new JFrame("Reflexology");

public static void main(String[] args){
    new Reflexology1();
}

public Reflexology1(){


    frame.setSize(600, 475);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Reflexology 1.0");
    frame.setResizable(false);


    button1 = new JButton("Accept");
    button2 = new JButton("Decline");
    movingButton = new JButton("Click Me");

    ListenForAcceptButton lForAButton = new ListenForAcceptButton();
    ListenForDeclineButton lForDButton = new ListenForDeclineButton();
    button1.addActionListener(lForAButton);
    button2.addActionListener(lForDButton);
    //movingButton.addActionListener(lForMButton);

    JTextArea textArea1 = new JTextArea(24, 50);

    textArea1.setText("Tracking Events\n");
    textArea1.setLineWrap(true);
    textArea1.setWrapStyleWord(true);
    textArea1.setSize(15, 50);

    FileReader reader = null;
    try {
        reader = new FileReader("EULA.txt");
        textArea1.read(reader, "EULA.txt");
    } catch (IOException exception) {
        System.err.println("Problem loading file");
        exception.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException exception) {
                System.err.println("Error closing reader");
                exception.printStackTrace();
            }
        }
    }

    JScrollPane scrollBar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    AdjustmentListener listener = new MyAdjustmentListener();

    thePanel.add(scrollBar1);
    thePanel.add(button1);
    thePanel.add(button2);
    thePlacebo.add(movingButton);

    frame.add(thePanel);

    ListenForWindow lForWindow = new ListenForWindow();
    frame.addWindowListener(lForWindow);
    frame.setVisible(true);

}
// Implement listeners

private class ListenForAcceptButton implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if (e.getSource() == button1){
            Calendar ClCDateTime = Calendar.getInstance();
            System.out.println(ClCDateTime.getTimeInMillis() - _openTime);
            _closeTime = ClCDateTime.getTimeInMillis() - _openTime;
            frame.getContentPane().remove(thePanel);
        }
    }
}

Weiß jemand, was ich falsch machen könnte?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage