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