简体   繁体   中英

How do i switch between screens in swing

This has been asked before but I would like clarification , I'm new to java coding (sort of, started coding last month) and would like to know simply how can I switch between UIs in one JFrame , Picture this, a settings menu, How do I make it in one JFrame window instead of just make a new window with all the settings, If you don't get it, ask for clarification.

You can implement a frame (JFrame) and, for example, two panels (JPanel). Initially you embed panel A inside frame, when you want to show panel B then call the method showPanelB()

public class MyFrame extends JFrame {
    PanelA panelA = new PanelA();
    PanelB panelB = new PanelB();
    
    
    public static void main(String args[]) {        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MyFrame().setVisible(true);
            }
        });
    }
    
    public MyFrame() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        
        getContentPane().setLayout(new BorderLayout());
        showPanelA();
        
    }
    
    public void showPanelA() {
        getContentPane().add(panelA, BorderLayout.CENTER);
    }
    
    public void showPanelB() {
        getContentPane().add(panelB, BorderLayout.CENTER);
    }

}

class PanelA extends JPanel {
    // Panel implementation
}

class PanelB extends JPanel {
   // Panel implementation
}

This has been asked before but I would like clarification , I'm new to java coding (sort of, started coding last month) and would like to know simply how can I switch between UIs in one JFrame , Picture this, a settings menu, How do I make it in one JFrame window instead of just make a new window with all the settings, If you don't get it, ask for clarification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM