简体   繁体   中英

How to instance a JPanel into panel in Jframe

Sorry if I might mix terms up. So I have a basic application where I would press the button and Jpanel1 with a label in it, would then switch/replace to JPanel2 that'll have a picture in it, all within the panel.在此处输入图像描述

The JPanel inside the box would change from Jpanel1 to JPanel 2 after pressing the button. So is their a way to instance a JPanel in a panel or JFrame? I can't find the method on how to fill the panel with the JPanel.

frame.getContentPane().add(panel, /*layout.location*/);

You mean add

The exact way to do this depends on the LayoutManager you use. I suggest checking out the Visual Guide to LayoutManagers . For example, if you use a BorderLayout , you can add a panel to the center and then replace it with a different panel when the user clicks a button.

I ended using layeredPane with a card layout . Then I placed a panel using the center Border Layout , then instanced the JPanel as the panel from a different class.

Picture of Windowbuilder layout

    panelPPBrowser = new JPanel();
    layeredPane.add(panelPPBrowser, "name_216881785358769");
    panelPPBrowser.setLayout(new BorderLayout(0, 0));
    
    panelBrowser panelBrowserCon = new panelBrowser();
    panelPPBrowser.add(panelBrowserCon, BorderLayout.CENTER);

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