Solve the following exercise: 1. Use the following Java code to answer the following: to. Correct the errors that it has b. Add required statements, routines, or modules c. Explain the logic of the program and how it works
Solve the following exercise:
1. Use the following Java code to answer the following:
to. Correct the errors that it has
b. Add required statements, routines, or modules
c. Explain the logic of the program and how it works
// Demonstration of the JTextField class.
import java.awt. *;
import java.awt.event. *;
import javax.swing. *;
public class TestTextField extends JFrame
{
private JTextField textfield1, textfield2, textfield3;
private JPasswordField password field;
// configure GUI
public TestTextField ()
{
super ("Text Entry Test and Password Sample");
Container container = getContentPane ();
container.setLayout (new FlowLayout ());
// create text field with default size
textfield1 = new JTextField (10);
container.add (textfield1);
// create text field with default text
TextField2 = new JTextField (" ");
container.add (textfield2);
// create text field with default text,
// 20 visible elements and no event handler
Text3 field = new JTextField ("Non-editable text field", 20);
Field Text3.setEditable (false);
Text field3.setToolTipText ("This cannot be edited");
container.add (fieldText3);
// create password field with default text
password field = new JPasswordField ("Hidden text");
container.add (password field);
// register event handlers
HandlerTextField handle = new HandlerTextField ();
Field Text1.addActionListener (handler);
Field Text2.addActionListener (handler);
Text field3.addActionListener (handler);
Password field.addActionListener (handler);
setSize (325, 100);
setVisible (true);
} // end of TextFieldTest constructor and start of main method
public main (String args [])
{
TestTextField application = new TestTextField ();
application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
// private inner class for event handling
private class TextField Handler implements ActionListener
{
// process text field events
public void action Performed (ActionEvent event)
{
String string = "";
// user pressed Enter on object JTextField textfield1
if (event.getSource () == textfield1)
string = "fieldText1:" + event.getActionCommand ();
// user pressed Enter on object JTextField textfield2
else if (event.getSource () == textfield2)
string = "fieldText2:" + event.getActionCommand ();
// user pressed Enter on JTextField objectTextField3
else if (event.getSource () == textField3)
string = "fieldText3:" + event.getActionCommand ();
// user pressed Enter on object JTextField password field
else if (event.getSource () == password field) {
string = "Password field:" +
new String (password field.getPassword ());
}
JOptionPane.showMessageDialog (null,
string, "Results", JOptionPane.CANCEL_OPTION);
} // end of actionPerformed method
} // end of private inner classTextFieldHandler
} // end of the TestTextField class
Step by step
Solved in 3 steps with 6 images