0

我正在努力。在此框架中,单击“添加记录”按钮,我向 Jframe 添加了一个面板,该面板是通过扩展 JPanel 类定义的。现在我希望在面板中创建按钮单击“保存”,我想从面板中定义的文本字段中获取文本,该文本字段将存储在变量中。在按钮单击保存时没有任何反应。帮我。提前致谢。

import java.awt.Color;
import java.sql.Savepoint;
import java.util.Calendar;
import javax.swing.*;
import java.awt.event.*;
import addPanel.panelAddForm;


public class frontPage extends JFrame implements ActionListener
{

//----------*******Variable Declarations*******-------------------  
    JPanel  panelheaderImage, panelSideBar, panelTittle, panelTittleBar;

            //panelFrontPage;           

    JLabel  labelheaderImage, labelDate, labelTime, labelTittle, labelBottomImage;

    JButton buttonAddRecord, buttonSearch, buttonadd;

    JTextField nameSearch;

    Calendar currentDate;

    String year, month, day;

//------------------------------------------------------------------


//--------******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();


        add( frameimg.panelBottomImage );
        add( framebottom.panelBottomText );
        //add( addform.panelAddForm );

        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 );

        /*panelFrontPage = new JPanel();
        panelFrontPage.setLayout(null);
        panelFrontPage.setBounds(0, 200, 500, 650);
        panelFrontPage.setEnabled(true);
        panelFrontPage.setVisible(true);
        add(panelFrontPage);*/

//------------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 );

        buttonadd = new JButton( "add" );
        buttonadd.setBounds( 170, 410, 80, 25 );
        buttonadd.addActionListener(this);
        panelTittleBar.add( buttonadd );

    }

    public void actionPerformed( ActionEvent ae )
    {
        panelAddForm addform = new panelAddForm();
        addform.save.addActionListener(this);   

        if( ae.getSource( ) == buttonAddRecord )
        {
            addform.revalidate();
            addform.repaint();
            add( addform );
            System.out.println("at add record button end");
        } 

        if( ae.getSource() == addform.save )
        {
            System.out.println("Save Clicked");
        }

    }

}

类 panelAddForm.java:

 package addPanel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.security.PublicKey;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.*;

public class panelAddForm extends JPanel 
{

    JLabel  labelName, labelFirstName, labelLastName, labelMiddleName,
            labelGender, labelAge, labelCity, labelContact, labelFees,
            labelNote, registrationId;

    public JTextField   textFieldFirstName, textFieldMiddleName, textFieldLastName,
                textFieldAge, textFieldCity, textFieldContact, textFieldFees,
                textFieldId;

    JTextArea textAreaNote;

    JRadioButton male, female;
    ButtonGroup selection;
    public JButton save, cancel;

    public panelAddForm()
    {
       this.setLayout(null);
       setVisible(true);
       setBounds(0, 200, 500, 450);

        registrationId= new JLabel("Registration Id");
        registrationId.setBounds(20, 0, 120, 20);
        add(registrationId);

        textFieldId = new JTextField();
        textFieldId.setBounds(130, 0, 50, 20);
        add(textFieldId);


        labelName = new JLabel( "Name" );
        labelName.setBounds( 20, 30, 50, 20);
        add(labelName);   

        labelFirstName = new JLabel("First Name");
        labelFirstName.setBounds(100, 50, 80, 10);
        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 );
            }

        }
    } );

    add( textFieldFirstName );

//----------------------------------------MNAME---------------------------------------------------

    labelMiddleName= new JLabel("Middle Name");
    labelMiddleName.setBounds( 190, 50, 80,  10);
    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 );
            }

        }
    } );

    add( textFieldMiddleName );

//-----------------------------------labelName-----------------------------------------------------------

    labelLastName = new JLabel("Last Name");
    labelLastName.setBounds(280, 50, 80, 10);
    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 );
            }
        }
    } );

    add( textFieldLastName );

    labelGender = new JLabel( "Gender" );
    labelGender.setBounds( 20, 70, 70, 20 );
    add(labelGender);

    selection = new ButtonGroup();

    male = new JRadioButton( "Male" );
    male.setBounds( 90, 70, 70, 20 );
    selection.add( male );
    add( male );

    female = new JRadioButton( "Female" );
    female.setBounds( 170, 70, 70, 20 );
    selection.add( male );
    add( female );

//---------------------AGE---------------------------------------   
    labelAge = new JLabel( "Age" );
    labelAge.setBounds( 20, 110, 50, 20 );
    add( labelAge );

    textFieldAge = new JTextField(  );
    textFieldAge.setBounds( 100, 110, 80, 20 );

    textFieldAge.addKeyListener( new KeyAdapter(  ) {
        public void keyTyped( KeyEvent e ) {
            String input = textFieldAge.getText(  );
            Pattern p = Pattern.compile( "[ A-Z,a-z,&%$#@!()*^]" );
            Matcher m = p.matcher( input );
            if ( m.find(  ) ) {
                JOptionPane.showMessageDialog( null, "Numbers only",
                        "Sorry", JOptionPane.ERROR_MESSAGE );
            }

        }
    } );
    add( textFieldAge );

    //---------------------CITY---------------------------------------
    labelCity = new JLabel( "City" );
    labelCity.setBounds( 20, 160, 80, 20 );
    add( labelCity );

    textFieldCity = new JTextField(  );
    textFieldCity.setBounds( 120, 160, 80, 20 );
    add(textFieldCity);

    textFieldCity.addKeyListener( new KeyAdapter(  ) {
        public void keyTyped( KeyEvent e ) {
            String input = textFieldCity.getText(  );
            Pattern p = Pattern.compile( "[ 0-9,&%$#@!()*^]" );
            Matcher m = p.matcher( input );
            if ( m.find(  ) ) {
                JOptionPane.showMessageDialog( null, "Characters only",
                        "Sorry", JOptionPane.ERROR_MESSAGE );
            }

        }
    } );

    //-----------------CONTACT---------------------------------------
    labelContact = new JLabel( "Phone Number" );
    labelContact.setBounds( 20, 210, 100, 20 );
    add( labelContact );

    textFieldContact = new JTextField( 10 );
    textFieldContact.setBounds( 120, 210, 80, 20 );
    add( textFieldContact );

    textFieldContact.addKeyListener( new KeyAdapter(  ) {
        public void keyTyped( KeyEvent e ) {
            String input = textFieldContact.getText(  );
            Pattern p = Pattern.compile( "^[ 1-9][ 0-9]{12}$" );
            Matcher m = p.matcher( input );
            if ( m.find(  ) ) {
                JOptionPane.showMessageDialog( null, "numbers only",
                        "Sorry", JOptionPane.ERROR_MESSAGE );
            }
        }
    } );

    add( textFieldContact );

    //-----------------NOTE---------------------------------------

    labelNote = new JLabel( "Note" );
    labelNote.setBounds( 20, 260, 140, 30 );
    add( labelNote );


    textAreaNote = new JTextArea();
    textAreaNote.setBounds(100, 260, 200, 50);
    add(textAreaNote);

    //----------------FEES--------------------------------------
    labelFees = new JLabel( "Consultation Fees" );
    labelFees.setBounds( 20, 330, 120, 20 );
    add( labelFees );

    textFieldFees = new JTextField(  );
    textFieldFees.setBounds( 140, 330, 80, 20 );
    add( textFieldFees );

    textFieldFees.addKeyListener( new KeyAdapter(  ) {
        public void keyTyped( KeyEvent e ) {
            String input = textFieldFees.getText(  );
            Pattern p = Pattern.compile( "[ A-Z,a-z,&%$#@!(    )*^]" );
            Matcher m = p.matcher( input );
            if ( m.find(  ) ) {
                JOptionPane.showMessageDialog( null, "Numbers only",
                        "Sorry", JOptionPane.ERROR_MESSAGE );
            }

        }
    } );

// ----------------------------------------------------

    save = new JButton( "SAVE" );
    save.setBounds( 25, 380, 70, 20 );
    //save.addActionListener( this );
    add( save );

    cancel = new JButton( "CANCEL" );
    cancel.setBounds( 150, 380, 80, 20 );
    //cancel.addActionListener( this );
    add( cancel );
}

}
4

1 回答 1

1

由于您将表单添加到对来自它的事件做出反应的函数中,因此永远不会有任何东西可以产生这些事件。尝试将这两行移出actionPerformed和移入frontPage或另一个初始化函数。

public void actionPerformed( ActionEvent ae )
{
    panelAddForm addform = new panelAddForm();
    addform.save.addActionListener(this);   
    ...
} 
于 2013-04-22T12:19:32.617 回答