0% found this document useful (0 votes)
4 views5 pages

Java AWT vs Swing: Event Listeners & GUI

The document provides a concise overview of Java GUI programming, highlighting the differences between AWT and Swing, the concept of Event Listeners, and three Swing components: JLabel, JTextField, and JButton. It includes two example Java programs demonstrating user input handling and a simple welcome message display using Swing. The document concludes by offering assistance with saving or running the provided code.

Uploaded by

jahilikzr4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Java AWT vs Swing: Event Listeners & GUI

The document provides a concise overview of Java GUI programming, highlighting the differences between AWT and Swing, the concept of Event Listeners, and three Swing components: JLabel, JTextField, and JButton. It includes two example Java programs demonstrating user input handling and a simple welcome message display using Swing. The document concludes by offering assistance with saving or running the provided code.

Uploaded by

jahilikzr4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 [Link].*;

import [Link].*;

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
[Link](20, 20, 80, 25);

[Link](100, 20, 200, 25);

[Link](20, 60, 80, 25);

[Link](100, 60, 200, 25);

[Link](100, 100, 100, 30);

// Add Action

[Link](new ActionListener() {

public void actionPerformed(ActionEvent e) {

String info = "Name: " + [Link]() + "\nEmail: " + [Link]();

[Link](frame, info);

});

// Add components

[Link](nameLabel); [Link](nameField);

[Link](emailLabel); [Link](emailField);

[Link](submitBtn);

[Link](350, 200);

[Link](null);

[Link](true);

}
---

5. Simple Swing Program: Button Click Shows Welcome Message

import [Link].*;

import [Link].*;

public class WelcomeApp {

public static void main(String[] args) {

JFrame frame = new JFrame("Welcome App");

JButton button = new JButton("Click Me");

// Set button position

[Link](100, 100, 120, 40);

// Add action listener

[Link](new ActionListener() {

public void actionPerformed(ActionEvent e) {

[Link](frame, "Welcome to NDI class");

});

[Link](button);

[Link](300, 300);

[Link](null);
[Link](true);

---

Let me know if you want these saved as files or need help running them.

You might also like