Dear All,
I would like to change the content pane of frame after pressing
a button by the method: frame.setContentFrame(newContentFrame);
However, after press the button, the Frame is frozen~~
How can I make it work? THANKS A LOT~~

import javax.swing.*;
import java.awt.event.*;
public class Test{
        JFrame main;
        JPanel panel1;
        JPanel panel2;

        public Test(){
            main = new JFrame();
            panel1 = new JPanel();
            panel2 = new JPanel();
            JLabel label1 = new JLabel("hihihihiihhihi");
            panel1.add(label1);

            JLabel label2 = new JLabel("eeeeee");
            panel2.add(label2);

            JButton butt1 = new JButton("Change");
            butt1.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent evt) {
                        main.setContentPane(panel2);
                    }
                });
                panel1.add(butt1);

           main.setContentPane(panel1);
           main.setBounds(100,100,100,100);
           main.setVisible(true);
        }
}

i JUST CALLED: new Test(); in main method~

Call validate() on the JFrame after you reset the panel. You also might want to try creating the GUI like the java tutorials recommend i.e. what they do here. (Although I don't think it is causing your problem, can't hurt to do it correctly)

This article has been dead for over six months. Start a new discussion instead.