动态添加面板到JFrame

时间:2013-04-22 04:50:33

标签: java swing jframe jpanel

我正在撰写表格。我在两个不同的文件中创建了以下框架。 我想在下面的类框架中添加面板。但我得到以下例外:     “线程中的异常”AWT-EventQueue-0“java.lang.Error:       未解决的编译问题:         addForm1无法解析为变量“

请帮忙。提前致谢。

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,&%$#@!()*^,\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,&%$#@!()*^]" );
            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,&%$#@!()*^]" );
    Matcher m = p.matcher( input );
    if ( m.find(  ) ) {
            JOptionPane.showMessageDialog( null, "Please Enter Valid Name",
                        "Sorry", JOptionPane.ERROR_MESSAGE );
                }
            }
    } );

    panelAddForm.add( textFieldLastName );


    }
}

1 个答案:

答案 0 :(得分:1)

您将addForm1定义为Object。您无法将对象添加到JFrame。您必须将addForm1定义为Swing组件,如JPanel。

Java类名称以大写字母开头。