java - 在另一个类中定义并在 JFrame 中使用的 JButtton 监听器

标签 java swing jpanel actionlistener

我正在努力。在此框架中,单击“添加记录”按钮,我向 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 );
}

}

最佳答案

由于您将表单添加到对其中的事件使用react的函数中,因此永远不会有任何东西可以产生这些事件。尝试将这两行从 actionPerformed 移出并移入 frontPage 或另一个初始化函数。

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

关于java - 在另一个类中定义并在 JFrame 中使用的 JButtton 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16146782/

相关文章:

java - Thread.sleep停止整个功能的android

java - Android 方 block 游戏

java - 如何在 Play Framework 2.5 模板中注入(inject)应用程序

java - JPanel 图像多次运行

java - JFrame 启动时如何禁用 JPanel

java - 整数除法 : Why is the result of 1/3 == 0?

java - 如何在java中键盘输入后更改图像?

java - 单元格编辑器如何工作?

java - 当第二个框架退出时,如何使我的主登录框架保持 Activity 状态?

java - 在 jPanel 上显示鼠标坐标