title: Dynamically adding panel to JFrame
body: <p>I am working on form. I have created following to frames in two different files. 
  I want to add panel to follwoing class's frame. But I got following exception:
      " Exception in thread "AWT-EventQueue-0" java.lang.Error: 
        Unresolved compilation problem: 
          addForm1 cannot be resolved to a variable "</p>
  
  <p>Please help. thanks in advance. </p>
  
  <pre><code>File: frontPage.java
  
   import java.awt.Color;
      import java.util.Calendar;
      import javax.swing.*;
  
      import java.awt.event.*;
  
      public class frontPage extends JFrame implements ActionListener
      {
  
      //----------*******Variable Declarations*******-------------------  
          JPanel panelheaderImage, panelSideBar, panelTittle, panelTittleBar;
  
          JLabel labelheaderImage, labelDate, labelTime, labelTittle,
                 labelBottomImage;
  
          JButton buttonAddRecord, buttonSearch;
  
          JTextField nameSearch;
  
          Calendar currentDate;
  
          String year, month, day;
          Object addform1=null;
  
      //--------******FRAME CONSTRUCTOR*********------------------------  
          frontPage( )
          {
  
          //------------frame initialization--------
          setBackground( Color.BLUE );
          setSize( 750, 850 );
          setVisible( true );
          setLayout( null );
  
      //------------frame initialization ENDS-----------------------------        
  
      //------------Panel Initialization Starts---------------------------
  
          framBottomImage frameimg = new framBottomImage();       
          frameBottom framebottom = new frameBottom();
          frameAddForm addform = new frameAddForm();
          addform1= addform;
  
          add( frameimg.panelBottomImage );
          add( framebottom.panelBottomText );
  
  
          panelheaderImage = new JPanel( );
          panelheaderImage.setBounds( 0, 0, 600, 80 );
          add( panelheaderImage );
  
          panelSideBar = new JPanel( );
          panelSideBar.setBounds( 520, 90, 200, 40);
          panelSideBar.setLayout(null);
          add( panelSideBar );
  
          panelTittle = new JPanel();
          panelTittle.setLayout(null);
          panelTittle.setBounds(0, 85, 550, 30);
          add ( panelTittle );
  
          panelTittleBar = new JPanel( );
          panelTittleBar.setBounds( 0, 150, 450, 40);
          panelTittleBar.setLayout(null);
          add( panelTittleBar );
  
  
  
      //------------Panel Initialization Ends---------------------------- 
  
          labelheaderImage = new JLabel( new ImageIcon
                                         ( "C:\\Users\\SUN\\Desktop\\img.jpg" ) );
          panelheaderImage.add( labelheaderImage );
  
  
          currentDate = Calendar.getInstance();
          year = "" + currentDate.get( Calendar.YEAR );
          month = "" + currentDate.get( Calendar.MONTH );
          day = "" + currentDate.get( Calendar.DAY_OF_MONTH );
  
          labelDate = new JLabel( "Date:" + day + "/" + month + "/" + year );
          labelDate.setBounds(0, 0, 90, 20);
          panelSideBar.add( labelDate );
  
          labelTime = new JLabel( "Time:" );
          labelTime.setBounds(0, 20, 50, 20);
          panelSideBar.add( labelTime );
  
          labelTittle = new JLabel( "Patient's Case Paper" );
          labelTittle.setBounds( 150, 0, 250, 25);
          panelTittle.add( labelTittle );
  
          buttonAddRecord = new JButton( "ADD RECORD" );
          buttonAddRecord.setBounds( 25, 0, 110, 25 );
          buttonAddRecord.addActionListener(this);
          panelTittleBar.add( buttonAddRecord );
  
          nameSearch = new JTextField( "" );
          nameSearch.setBounds( 170, 0, 110, 25 );
          panelTittleBar.add( nameSearch );
  
          buttonSearch = new JButton( "Search" );
          buttonSearch.setBounds( 320, 0, 100, 25 );
          panelTittleBar.add( buttonSearch );
          }
  
          public void actionPerformed( ActionEvent ae )
          {
              if(ae.getSource( ) == buttonAddRecord )
              {
                  add( addForm1 );
              }
          }
  
      }
  
  Another File:
  frameAddForm.java
  
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  import javax.swing.*;
  
  public class frameAddForm 
  {
  
      JPanel panelAddForm;
  
      JLabel labelName, labelFirstName, labelLastName, labelMiddleName;
      JTextField textFieldFirstName, textFieldMiddleName, textFieldLastName;
  
      frameAddForm()
      {
  
      panelAddForm = new JPanel();
      panelAddForm.setLayout(null);
      panelAddForm.setBounds(0, 220, 500, 600);
  
      labelName = new JLabel( "Name" );
      labelName.setBounds( 20, 30, 50, 20);
      panelAddForm.add(labelName);
  //----------------------FNAME---------------------------------
  
  
  
      labelFirstName = new JLabel("First Name");
      labelFirstName.setBounds(100, 50, 80, 10);
      panelAddForm.add(labelFirstName);
  
      textFieldFirstName = new JTextField(  );
      textFieldFirstName.setBounds( 100, 30, 80, 20 );
  
      textFieldFirstName.addKeyListener( new KeyAdapter(  ) {
      public void keyTyped( KeyEvent e ) {
      String input = textFieldFirstName.getText(  );
      Pattern p = Pattern.compile( "[ 0-9,&amp;%$#@!()*^,\t\n\f\r]" );
      Matcher m = p.matcher( input );
      if ( m.find(  ) ) {
          JOptionPane.showMessageDialog( null, "Please Enter Valid Name",
                          "Sorry", JOptionPane.ERROR_MESSAGE );
                  }
  
              }
      } );
  
      panelAddForm.add( textFieldFirstName );
  
  //-------------------MNAME---------------------------------------------------
  
          labelMiddleName= new JLabel("Middle Name");
          labelMiddleName.setBounds( 190, 50, 80,  10);
          panelAddForm.add(labelMiddleName);
  
  
          textFieldMiddleName = new JTextField(  );
          textFieldMiddleName.setBounds( 190, 30, 80, 20 );
  
          textFieldMiddleName.addKeyListener( new KeyAdapter(  ) {
              public void keyTyped( KeyEvent e ) {
              String input = textFieldMiddleName.getText(  );
              Pattern p = Pattern.compile( "[ 0-9,&amp;%$#@!()*^]" );
              Matcher m = p.matcher( input );
              if ( m.find(  ) ) {
              JOptionPane.showMessageDialog( null, "Please Enter Valid Name",
                          "Sorry", JOptionPane.ERROR_MESSAGE );
                  }
  
              }
          } );
  
      panelAddForm.add( textFieldMiddleName );
  
  //--------------------------labelName--------------------------------------
  
      labelLastName = new JLabel("Last Name");
      labelLastName.setBounds(280, 50, 80, 10);
      panelAddForm.add(labelLastName);
  
      textFieldLastName = new JTextField(  );
      textFieldLastName.setBounds( 280, 30, 80, 20 );
  
      textFieldLastName.addKeyListener( new KeyAdapter(  ) {
      public void keyTyped( KeyEvent e ) {
      String input = textFieldLastName.getText(  );
      Pattern p = Pattern.compile( "[ 0-9,&amp;%$#@!()*^]" );
      Matcher m = p.matcher( input );
      if ( m.find(  ) ) {
              JOptionPane.showMessageDialog( null, "Please Enter Valid Name",
                          "Sorry", JOptionPane.ERROR_MESSAGE );
                  }
              }
      } );
  
      panelAddForm.add( textFieldLastName );
  
  
      }
  }
  </code></pre>