Respuesta :
Answer:
Action Listener class has to present in a program only once, actionPerformed() method is overriding method also present once.
When we have multiple source of event like here button and textfield, single actionPerformed() method will provide implementation
We can detect the source from when event is fired, and accordingly write separte code for each source or same code (like here).
Hence we have to add common addActionListener to both button and textfield.
Same reading mechanism will work inside that common actionPerformed method.
Both source will fired event only if they have both bounded to addActionListener
AS our case Button will fire event and TextField will also fire event.
So both of them registered with addActionListener method.
So from question of choices
a (add an actionListener to btnRead (Among other things)
d (add an actionListener to txtNumber (Among other things)
are correct
TO VERIFY THE CONCEPT A SAMPLE PROGRAM WITH OUT PUT HAS GIVEN BELOW
*/
/* PROGRAM TO SET EVENT HANDELER TO BOTH BUTTON AND TEXTFIELD, SO BOTH oF THEM CAN FIRED EVENT TO READ TEXT VALUE*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class TestInput {
JButton btnRead;
JTextField txtNumber,txtDispaly;
JFrame f;
public TestInput() //// Constructor
{
f=new JFrame();
f.setLayout(new FlowLayout()); //// SETTING FRAME TO FLOW LAYOUT
txtNumber=new JTextField("0.0",15); //// CREATED A TEXTFIELD WHEN ENTER PRESSED, IT SHOULD READ OWN VALUE
f.add(txtNumber);
btnRead=new JButton(" Read The Number "); // CREATED A BUTTON WHEN PRESSED TO READ FROM NUMBER TEXTFIELD
f.add(btnRead); // ADD BUTTON TO FRAME
txtDispaly=new JTextField("0.0",15); // VALUE TO BE DISPLAY HERE AFTER READ , EVENT FIRED EITHER BUTTON OT TEXTFIELD
f.add(txtDispaly); // ADD TEXTFIELD TO FRAME
f.setSize(300,300); // FRAME SIZE TO DISPLAY
f.setVisible(true);
ActionListener myHandler = new EventHandler(); // INSTANCE OF CUSTOME EVENT HANDELER
btnRead.addActionListener(myHandler); /// Binding the custom event handler TO BUTTON
txtNumber.addActionListener(myHandler); /// Binding event handler to TextField, so on enter key press it will also read
}
/// Define inner class as listener.
private class EventHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
// EVENT IS TRIGGERED EITHER BUTTON IS CLICKED OT TEXTFIELD IS ENTER KEY PRESSED
if(e.getSource() == btnRead || e.getSource() == txtNumber )
{
txtDispaly.setText(txtNumber.getText()); // VALUE DISPLAYED IN DISPLAY TEXTFIELD AFTRE READ FROM NUMBER TEXTFIELD
}
}
}
public static void main(String[] args)
{
new TestInput();
}
}
Explanation:
For Detailed and better Explanation the screenshot of code and output is given in the attached files. Please have a look on them.

