Page 222 - CTS - CSA TP - Volume 2
P. 222
COMPUTER SOFTWARE APPLICATION - CITS
// Setting layout to FlowLayout
frame.setLayout(new FlowLayout());
// Adding components to the frame
frame.add(nameLabel);
frame.add(nameTextField);
frame.add(submitButton);
// Adding action listener to the button
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameTextField.getText();
System.out.println(“Name submitted: “ + name);
showConfirmationDialog(“Submission”, “Name submitted: “ + name);
}
});
// Adding event handler to the text field
nameTextField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameTextField.getText();
System.out.println(“Enter key pressed in the text field. Name: “ + name);
showConfirmationDialog(“Text Field Event”, “Enter key pressed. Name: “ + name);
}
});
// Setting frame visibility
frame.setVisible(true);
// Adding window close event handling
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.exit(0);
}
});
}
207
CITS : IT & ITES - Computer Software Application - Exercise 118