Three Java event handling methods

Source: Internet
Author: User

/*************************************** **************************************** ***********
* This class implements different methods for event listening to swing components *
* 1 implement the corresponding interface by the class, and rewrite the methods in the interface in the class to process the event *
* 2. Use an anonymous class to process events (used when multiple event sources exist )*
* 3 process events through internal Classes *
**
* If multiple event sources exist, you can use two methods to obtain the event source *
* The second method is recommended for getactioncommand () getsource *
**
* If the corresponding listener interface has multiple methods, you can use the corresponding adapter to listen to events *

**************************************** **************************************** **********/
Import javax. Swing. jframe;
Import javax. Swing. jpanel;
Import javax. Swing. jlabel;
Import javax. Swing. jtextfield;
Import javax. Swing. jbutton;
Import javax. Swing. Border. titledborder;
Import java. AWT. event .*;

// Public class chenji extends jframe implements actionlistener
Public class chenji extends jframe
{
Private jpanel;
Private jpanel jpanel1;
Private jtextfield nametext;
Private jtextfield sextext;
Private jtextfield addtext;
Private jtextfield chinatext;
Private jtextfield mathtext;
Private jtextfield englishtext;
Private jtextfield zhengzhitext;
Private jtextfield test;
Private jlabel title;
Private jlabel name;
Private jlabel sex;
Private jlabel add;
Private jlabel China;
Private jlabel math;
Private jlabel English;
Private jlabel zhengzhi;
Private jbutton jbutton1;
Private jbutton jbutton2;
Private jbutton jbutton3;
 
Public chenji ()
{
Setsize (400,500 );
Settitle ("score Registration Form ");
Setdefaclocloseoperation (exit_on_close );
Fram1 ();
}
Public void fram1 ()
{
Jpanel = (jpanel) getcontentpane ();
Jpanel. setlayout (null );
Title = new jlabel ("score Registration Form ");
Title. setbounds );
Jpanel. Add (title );
Name = new jlabel ("name ");
Name. setbounds (10, 40, 50, 20 );
Jpanel. Add (name );
Nametext = new jtextfield ("");
Nametext. setbounds (80, 40, 50, 20 );
Jpanel. Add (nametext );
Sex = new jlabel ("gender ");
Sex. setbounds (170,40, 50,20 );
Jpanel. Add (sex );
Sextext = new jtextfield ("");
Sextext. setbounds );
Jpanel. Add (sextext );
Add = new jlabel ("Hometown ");
Add. setbounds (10, 90, 50, 20 );
Jpanel. Add (ADD );
Addtext = new jtextfield ("");
Addtext. setbounds );
Jpanel. Add (addtext );
Jpanel1 = new jpanel ();
Jpanel1.setlayout (null );
Jpanel1.setborder (New titledborder ("score "));
Jpanel1.setbounds (10,120,370,150 );
China = new jlabel (" ");
China. setbounds (10, 20, 50, 20 );
Jpanel1.add (China );
Chinatext = new jtextfield ("");
Chinatext. setbounds (90,20, 50,20 );
Jpanel1.add (chinatext );
Math = new jlabel ("Mathematics ");
Math. setbounds (170,20, 50,20 );
Jpanel1.add (math );
Mathtext = new jtextfield ("");
Mathtext. setbounds (, 20, 50, 20 );
Jpanel1.add (mathtext );
Zhengzhi = new jlabel ("Politics ");
Zhengzhi. setbounds (10, 60, 50, 20 );
Jpanel1.add (zhengzhi );
Zhengzhitext = new jtextfield ("");
Zhengzhitext. setbounds (90,60, 50,20 );
Jpanel1.add (zhengzhitext );
Jpanel. Add (jpanel1 );
Jbutton1 = new jbutton ("total score ");
Jbutton1.addactionlistener (New actionlistener () // implement event processing through anonymous internal classes
{
Public void actionreceivmed (actionevent e) // use getactioncommand () to obtain the event Source
{
String Ss = E. getactioncommand ();
Try {
If (ss. Equals ("total score "))
Zongchengji ();
Else
Pingjun ();
} Catch (exception ex)
{
Ex. printstacktrace ();
System. Out. println ("your input score is incorrect ");
}

/* Jbutton JB = (jbutton) E. getsource (); // getsource () to obtain the event Source
Try {
If (JB = jbutton1)
Zongchengji ();
Else
Pingjun ();
} Catch (exception ex)
{
// Ex. printstacktrace ();
System. Out. println ("your input score is incorrect ");
}
*/
}
});
Jbutton1.setbounds (100,380, 50, 20 );
Jpanel. Add (jbutton1 );
Jbutton2 = new jbutton ("average score ");
Jbutton2.addactionlistener (New eventtest (); // implement event processing through internal classes
Jbutton2.setbounds (100,280, 50, 20 );
Jpanel. Add (jbutton2 );
Jbutton3 = new jbutton ("clear text box ");
// Jbutton3.addactionlistener (this); // use the class itself to implement the corresponding listening interface for event processing
Jbutton3.setbounds (180,380, 50, 20 );
Jpanel. Add (jbutton3 );
Test = new jtextfield ();
Test. setbounds (180,280, 80, 20 );
Jpanel. Add (test );
}
Public void actionreceivmed (actionevent E)
{
String Ss = E. getactioncommand ();
Try {
If (ss. Equals ("total score "))
Zongchengji ();
Else if (ss. Equals ("clear text box "))
Test. settext ("");
Else
Pingjun ();

} Catch (exception ex)
{
Ex. printstacktrace ();
System. Out. println ("your input score is incorrect ");
}

/* Jbutton JB = (jbutton) E. getsource ();
Try {
If (JB = jbutton1)
Zongchengji ();
Else
Pingjun ();
} Catch (exception ex)
{
// Ex. printstacktrace ();
System. Out. println ("your input score is incorrect ");
}
*/
}
Public void zongchengji ()
{
Float China = float. parsefloat (chinatext. gettext ());
Float math = float. parsefloat (mathtext. gettext ());
Float zhengzhi = float. parsefloat (zhengzhitext. gettext ());
String Zong = new float (China + math + zhengzhi). tostring ();
Test. settext (zong );
}
Public void pingjun ()
{
Float China = float. parsefloat (chinatext. gettext ());
Float math = float. parsefloat (mathtext. gettext ());
Float zhengzhi = float. parsefloat (zhengzhitext. gettext ());
String pingjun = new float (China + math + zhengzhi)/3). tostring ();
Test. settext (pingjun );
}
Public static void main (string [] ARGs)
{
Chenji = new chenji ();

Chenji. setvisible (true );

}

Class eventtest implements actionlistener
{
Public void actionreceivmed (actionevent E)
{
String Ss = E. getactioncommand ();
Try {
If (ss. Equals ("total score "))
Zongchengji ();
Else
Pingjun ();

} Catch (exception ex)
{
Ex. printstacktrace ();
System. Out. println ("your input score is incorrect ");
}

/* Jbutton JB = (jbutton) E. getsource ();
Try {
If (JB = jbutton1)
CJ. zongchengji ();
Else
CJ. pingjun ();
} Catch (exception ex)
{
// Ex. printstacktrace ();
System. Out. println ("your input score is incorrect ");
}
*/
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.