Thanks!
Here’s a short and precise version of answers and commented Java programs based on the
handwritten questions:
---
1. Difference between AWT and Swing in Java:
AWT is heavyweight; depends on the native system GUI.
Swing is lightweight; written entirely in Java and more flexible.
---
2. What is Event Listener in Java?
An Event Listener is an interface in Java used to handle events like button clicks. It contains methods
that get called when a specific event occurs.
---
3. Three Swing Components and Their Properties:
JLabel – displays text.
JTextField – takes input from the user.
JButton – triggers actions when clicked.
---
4. GUI Program to Input Name & Email and Display in Dialog:
import javax.swing.*;
import java.awt.event.*;
public class UserForm {
public static void main(String[] args) {
JFrame frame = new JFrame("User Form");
JLabel nameLabel = new JLabel("Name:");
JLabel emailLabel = new JLabel("Email:");
JTextField nameField = new JTextField();
JTextField emailField = new JTextField();
JButton submitBtn = new JButton("Submit");
// Set bounds
nameLabel.setBounds(20, 20, 80, 25);
nameField.setBounds(100, 20, 200, 25);
emailLabel.setBounds(20, 60, 80, 25);
emailField.setBounds(100, 60, 200, 25);
submitBtn.setBounds(100, 100, 100, 30);
// Add Action
submitBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String info = "Name: " + nameField.getText() + "\nEmail: " + emailField.getText();
JOptionPane.showMessageDialog(frame, info);
});
// Add components
frame.add(nameLabel); frame.add(nameField);
frame.add(emailLabel); frame.add(emailField);
frame.add(submitBtn);
frame.setSize(350, 200);
frame.setLayout(null);
frame.setVisible(true);
}
---
5. Simple Swing Program: Button Click Shows Welcome Message
import javax.swing.*;
import java.awt.event.*;
public class WelcomeApp {
public static void main(String[] args) {
JFrame frame = new JFrame("Welcome App");
JButton button = new JButton("Click Me");
// Set button position
button.setBounds(100, 100, 120, 40);
// Add action listener
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Welcome to NDI class");
});
frame.add(button);
frame.setSize(300, 300);
frame.setLayout(null);
frame.setVisible(true);
---
Let me know if you want these saved as files or need help running them.