user3590756
user3590756

Reputation: 1

switching Jpanels 1 Jframe

I've been searching and nothing has been simple enough for me. But for the thing I'm working on I want to make one JFrame in my main method and use different class files for each JPanel. (This is to keep the information separated and clean). Also what's the best way to switch JPanels if I do it in this method?

public class Main extends JFrame {
public Main(){
    JFrame intro = new JFrame("FormProgram");
    intro.setSize(800,600);
    intro.setVisible(true);
    intro.setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    intro.setLocationRelativeTo(null);

}

public static void main(String[] args) {

    new Main();

}

Then my basic second class looks like this.

public class Page1 extends JFrame implements ActionListener{


public JLabel test;

public void Page1(){

    Container cp = intro.getContentPane();
    cp.setLayout(null);
    this.test = new JLabel("welcome");
    this.test.setBounds(5,5,300,300);
    cp.add(test);


}

Upvotes: 0

Views: 40

Answers (1)

camickr
camickr

Reputation: 324098

what's the best way to switch JPanels

Use a CardLayout. Read the section from the Swing tutorial on How to Use a CardLayout for more information and a working example.

Upvotes: 1

Related Questions