GUI simple addition calculator analog Listener event Get property value of other domain window shutdown Listener Event

Source: Internet
Author: User
Tags gettext

Idle to do nothing, learn to learn the development of graphical interface, maybe when you write something, there will be very practical value.

Never wondered how to get other property values, such as text boxes, when the button was clicked.

Today, I saw the horse soldier Teacher's video, through a façade mode method, add the required classes to their own member variables, such as the text box belongs to the class injected into the button listener class properties, you can easily get all the public properties of the class.


Package Com.gui._02actionlistener;
Import Java.awt.Button;
Import Java.awt.FlowLayout;
Import Java.awt.Frame;
Import Java.awt.Label;
Import Java.awt.TextField;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.WindowAdapter;

Import java.awt.event.WindowEvent; /** * Addition Calculator Simple implementation * 1. Gets the value of the other domain * 2. Window Close Event * @author Administrator * * */public class Textfield_numadd {public static
	void Main (string[] args) {new Tfframe_2num (). Buildframe (); }}/** * Create window * @author Administrator * */class Tfframe_2num extends frame{//define 3 member variables TextField num1,num2,resul
	T
		public void Buildframe () {num1 = new TextField (10);
		num2 = new TextField (10);
		
		result = new TextField (15);
		Label Labeladd = new label ("+");
		Button Btneq = New button ("=");
		
		Add Click event Btneq.addactionlistener (New addActionListener (this));
		Settitle ("nums add");
		
		SetLayout (New FlowLayout ()); Window Close Event This.addwindowlistener (new Windowadapter () {@Override public void windowclosing (WindowEvent e) {system.exit (0);
		
		}
			
		});
		xx + xx = xx Add (NUM1);
		Add (Labeladd);
		Add (num2);
		Add (BTNEQ);
		Add (result);
		Pack ();
		SetVisible (TRUE);
	Setlocationrelativeto (NULL);
	}} class addActionListener implements actionlistener{//define the frame member variable, in order to get the property value of frame Tfframe_2num TF;
	Public addActionListener (Tfframe_2num tf) {this.tf = TF;
		} @Override public void actionperformed (ActionEvent e) {int n1 = Integer.parseint (Tf.num1.getText ());
		int n2 = Integer.parseint (Tf.num2.getText ());
	Tf.result.setText ((N1+N2) + ""); }
	
}



Window Close event, if you implement the frame class, rewrite the frame's Addwindowlistener () method, the parameter is new a new Windowadapter (), and then implement the new Windowadapter () Windowclosing method, add system.exit (0);

<span>		</span>//window Close Event
<span>		</span>frame f = new Frame ();
		F.addwindowlistener (New Windowadapter () {
			@Override public
			void windowclosing (WindowEvent e) {
				System.exit (0);
			}
			
		);


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.