package zuoye;
import java.awt *.;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ti5 extends Frame implements ActionListener {
static Ti5 frm = new Ti5 ();
static TextField tx1, tx2, tx3;
static Button bu1, bu2, bu3, bu4;
static Label la1, la2;
public static void main (String [] args)
{
frm.setTitle (" title 5 " ) ;
frm.setSize (400, 400);
frm.setLayout (new FlowLayout ());
tx1 = new TextField (10);
tx2 = new TextField (10);
tx3 = new TextField (20);
tx3.setEditable (false);
bu1 = new Button (" plus" ) ;
bu2 = new Button (" minus" ) ;
bu3 = new Button (" multiply " ) ;
bu4 = new Button (" except" ) ;
bu1.addActionListener (frm);
bu2.addActionListener (frm);
bu3.addActionListener (frm);
bu4.addActionListener (frm);
la1 = new Label (" The first number " ) ;
la2 = new Label (" second number " ) ;
frm.add (bu1);
frm.add (bu2);
frm.add (bu3);
frm.add (bu4);
frm.add (la1);
frm.add (tx1);
frm.add (la2);
frm.add (tx2);
frm.add (tx3);
frm.setVisible (true);
}
@ Override
public void actionPerformed (ActionEvent e)
{
/ / TODO Auto -generated method stub
}
}
I want to achieve in this program tx1 and tx2 text box enter a number after each click on add, subtract , multiply, divide after four buttons are displayed in the corresponding tx3 calculation result. .
But public void actionPerformed (ActionEvent e)
{
/ / TODO Auto -generated method stub
}
methods do not know if you write code. . Seeking guidance, I'm just learning java reference materials do not ask for advice but still some
------ Solution ------------------- -------------------------
public void actionPerformed (ActionEvent e) {}
In this method , the parameter e to get to the event source button control, and then get to the button to display the name , which is "plus" , " minus" , " multiply " , and then determine which button is clicked deal " subtraction multiplication and division "
------ Solution -------------------------------------- ------
The above is what I wrote to you a button listener addition , similar to the rest of the methods.
------ For reference only -------------------------------------- -
landlord hello , give some idea of their own code to try it.
1.actionperformed method will be called when you add a control to trigger the listener .
2. attempt to distinguish between types of keys, do the appropriate action.
3. obtain an instance of each txtfield in action and then read and do the appropriate action on the line.
can try.
没有评论:
发表评论