Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向JFrame动态添加面板_Java_Swing_Jframe_Jpanel - Fatal编程技术网

Java 向JFrame动态添加面板

Java 向JFrame动态添加面板,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,我正在写表格。我在两个不同的文件中创建了以下框架。 我想将面板添加到后续类的框架中。但我有以下例外: 线程“AWT-EventQueue-0”java.lang中出现异常。错误: 未解决的编译问题: 无法将addForm1解析为变量“ 请帮忙。提前谢谢 File: frontPage.java import java.awt.Color; import java.util.Calendar; import javax.swing.*; import java.awt

我正在写表格。我在两个不同的文件中创建了以下框架。 我想将面板添加到后续类的框架中。但我有以下例外: 线程“AWT-EventQueue-0”java.lang中出现异常。错误: 未解决的编译问题: 无法将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 );


    }
}

您将addForm1定义为一个对象。不能将对象添加到JFrame。必须将addForm1定义为Swing组件,如JPanel

Java类名以大写字母开头