Java AWT Event Listener

Source: Internet
Author: User
Tags event listener gettext pack
Testactionevent.java


import java.awt.*;
Import java.awt.event.*;

public class testactionevent{public
	static void Main (string[] args) {
		frame f=new frame ("testactionevent");
		Button B=new button ("Press Me!");
		Monitor bh=new Monitor ();
		B.addactionlistener (BH);
		F.add (b,borderlayout.center);
		F.pack ();
Testtextfield.java

import java.awt.*;
Import java.awt.event.*;

Class testtextfield{public
	static void Main (string[] args) {
		new Tfframe ();
	}	
}

Class Tfframe extends frame{
	tfframe () {TextField tf=new TextField
		();
		Add (TF);
		Tf.addactionlistener (New Tfactionlistener ());
		Pack ();
		SetVisible (True);	
	}

Class Tfactionlistener implements actionlistener{public
		void actionperformed (ActionEvent e) {
			TextField tf= (TextField) E.getsource ();
			System.out.println (Tf.gettext ());
			Tf.settext ("");
		}
}

Testtextfield.java Change the Echo character

import java.awt.*;
Import java.awt.event.*;

Class testtextfield{public
	static void Main (string[] args) {
		new Tfframe ();
	}	
}

Class Tfframe extends frame{
	tfframe () {TextField tf=new TextField
		();
		Add (TF);
		Tf.addactionlistener (New Tfactionlistener ());
		Tf.setechochar (' * ');
		Pack ();
		SetVisible (True);	
	}

Class Tfactionlistener implements actionlistener{public
		void actionperformed (ActionEvent e) {
			TextField tf= (TextField) E.getsource ();
			System.out.println (Tf.gettext ());
			Tf.settext ("");
		}
}

Addition Calculator import java.awt.*;

Import java.awt.event.*;
	Class testtfmath{public static void Main (string[] args) {new Tfmath ();
	} class Tfmath extends frame{TextField a=null;
	TextField B=null;
	
	TextField C=null;
		Tfmath () {a=new TextField ();
		B=new TextField ();
		C=new TextField ();
		Button Plus=new button ("calculation");
		Label Lbplus=new label ("+");
		Label Lbis=new label ("=");
		Plus.addactionlistener (This) (new Actionlistener_plus);
		SetLayout (New FlowLayout ());
		Add (a);
		Add (Lbplus);
		Add (b);
		Add (Lbis);
		Add (c);
		Add (plus);
		Pack ();	
	SetVisible (TRUE);
		} class Actionlistener_plus implements actionlistener{/*textfield a,b,c;
			Actionlistener_plus (TextField A,textfield B,textfield c) {this.a=a;
			This.b=b;
		This.c=c;
		}*/Tfmath Tm=null;
		Actionlistener_plus (Tfmath tm) {This.tm=tm;
			public void actionperformed (ActionEvent e) {int i_a=integer.parseint (Tm.a.gettext ());
			int I_b=integer.parseint (Tm.b.gettext ()); Tm.c.settext ("" + (I_a+i_b)); }
}

Anonymous internal class event listener

import java.awt.*;
Import java.awt.event.*;

Class testtfmath{public
	static void Main (string[] args) {
		new Tfmath ();
	}	
}

Class Tfmath extends frame{
	Tfmath () {
		final TextField a=new ();
		Final TextField b=new TextField ();
		Final TextField c=new TextField ();
		Button Plus=new button ("calculation");
		Label Lbplus=new label ("+");
		Label Lbis=new label ("=");
		Plus.addactionlistener (new ActionListener () {public
				void actionperformed (ActionEvent e) {
					C.settext ("" + Integer.parseint (A.gettext ())
						+integer.parseint (B.gettext ()));
				})
		;
		SetLayout (New FlowLayout ());
		Add (a);
		Add (lbplus);
		Add (b);
		Add (lbis);
		Add (c);
		Add (plus);
		Pack ();
		SetVisible (True);	
	}	


Testactionevent2.java

import java.awt.*;
Import java.awt.event.*;

public class testactionevent2{public
	static void Main (string[] args) {
		frame f=new frame ("testactionevent");
		Button Btn_start=new button ("Start");
		Button Btn_stop=new button ("Stop");
		Monitor bh=new Monitor ();
		Btn_start.addactionlistener (BH);
		Btn_stop.addactionlistener (BH);
		F.add (btn_start,borderlayout.west);
		F.add (btn_stop,borderlayout.east);
		F.pack ();
		F.setvisible (True);
	}

Class Monitor implements actionlistener{public
		void actionperformed (ActionEvent e) {
			System.out.println (" Button "+ e.getactioncommand () +" pressed! ");
		}


Related Article

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.