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

Java

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

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
You are on page 1/ 5

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy