简体   繁体   中英

How to add external JPanel in JFrame?

在此输入图像描述

I'm working on large scale program. As you can see I have one main JFrame and about 20 menu items on that. Each menu item must pop up a new window. At the beginning I have created a JLayeredPanel and then I assigned each menu item to one JPanel which is inside JFrame.Then I put 25 panel in JLayeredPanel... Default all the panels are set to invisible like:

panel1.setVisible(false);
panel2.setVisible(false);

so on

When user click on one menu item, its JPanel will be visible and rest are invisible. It looks messy and I have 5000 lines code. I used InternalFrame and TabbedPane but I'm not happy with them. I want to split my code in different JPanel classes and assign them to the main JFrame. I mean when user clicked on each menu item it will call the external JPanel and render it on the JPanel on the main JFrame. I am using design mode in netbeans and it does everything for me but the simpled structure is like this and it is not working:

 public class NewJPanel extends JPanel{
    //I have added buttons and etc on this panel
    ......

    }

public class  frame extends JFrame(){

        JPanel panel = new JPanel();
        .....
        Public frame(){
             frame.add(panel);
        }
        ......
        //When use click on the any button on the panel
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            //this is not working

            NewJPanel fi = new NewJPanel ();
            panel1.add(fi);

            //or I tested this way separately but it did not work

           panel1.remove();
           panel1 = new NewJPanel();
           add(panel);
           invalidate();
        }       


}

please give me any suggestion how I can control this program in splited classes in professional way.

  1. remove JPanel from JFrame.getContentPane.remove(myPanel)

  2. add a new JPanel with constants, everyhing depends of used LayoutManager and its methods implemented in API

  3. call JFrame.(re)validate() and JFrame.repaint() as last code lines, if everything is done, these notifiers correctly repaint available area

  4. again to use CardLayout, there isn't signoficant performance or memory issue

Please give me any suggestion how I can control this program in splited classes in proressional way.

Ok.

You should put all of your JPanels in a JTabbedPane. The JTabbedPane would be added to the JFrame.

The JFrame, JTabbedPane, and each JPanel would be constructed in a separate class.

You use Swing components, rather than extending them. The only reason you extend a Swing component is if you override one of the component methods.

You should also create model classes for each of the JPanels, as well as a model class for the application.

Read this article to see how to put a Swing GUI together.

make's code better

public class NewJPanel extends JPanel{
//I have added buttons and etc on this panel
......

}

 public class  frame extends JFrame(){

    JPanel panel = new JPanel();
    .....
    Public frame(){
         //frame.add(panel); you dont need call frame because extends JFrame in frame class
         add(panel);

    ......
    //When use click on the any button on the panel
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        //this is not working

        NewJPanel fi = new NewJPanel();
        add(fi);

        //or I tested this way separately but it did not work

       /*panel1.remove();
       panel1 = new NewJPanel();
       add(panel);
       invalidate();you must define panel1 before use it,like  :JPanel panel1 = new JPanel();*/
    }       

 } 

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