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~

Recommended Answers

All 2 Replies

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.