0% found this document useful (0 votes)
3 views8 pages

Unit 5 Java

Swing is a part of the Java Foundation Classes used for creating GUI applications, offering lightweight components and a customizable look and feel. It has advantages over AWT, including a richer set of components, adherence to the MVC architecture, and greater flexibility. The document also discusses layout managers in Java, the applet life cycle, and provides example Java programs demonstrating Swing and applet functionalities.

Uploaded by

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

Unit 5 Java

Swing is a part of the Java Foundation Classes used for creating GUI applications, offering lightweight components and a customizable look and feel. It has advantages over AWT, including a richer set of components, adherence to the MVC architecture, and greater flexibility. The document also discusses layout managers in Java, the applet life cycle, and provides example Java programs demonstrating Swing and applet functionalities.

Uploaded by

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

1)Explain Swing and its advantages over AWT

Introduction
Swing is a part of the Java Foundation Classes (JFC) used to create Graphical User
Interface (GUI) applications. It provides a rich set of lightweight components and is more
flexible than AWT.

Features of Swing
●​ Platform independent.
●​ Lightweight components.
●​ Supports pluggable look and feel.
●​ Provides advanced GUI components like JButton, JLabel, JTable, etc.

Advantages of Swing over AWT


●​ Swing components are lightweight, while AWT components are heavyweight.
●​ Swing provides a richer set of GUI components.
●​ Swing supports customizable look and feel.
●​ Swing follows the MVC architecture, making applications easier to maintain.
●​ Swing is more flexible and suitable for modern GUI development.

Java Program
import [Link].*;

class SwingExample {
public static void main(String args[]) {
JFrame f = new JFrame("Swing");
JButton b = new JButton("Click");

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


[Link](b);

[Link](300, 200);
[Link](null);
[Link](true);
}
}
Output
A window titled "Swing" is displayed with a "Click" button.

Explanation
●​ JFrame creates the main window.
●​ JButton creates a button and adds it to the frame.
●​ Swing components provide a user-friendly and attractive interface.
Unit–5: Question 2

Demonstrate a simple Swing application


(JNTUH R22 Semester Exam Answer)

Introduction
Swing is a part of the Java Foundation Classes (JFC) used to develop Graphical User
Interface (GUI) applications. It provides a rich collection of lightweight components such as
JFrame, JButton, JLabel, and JTextField for creating attractive and user-friendly
interfaces. Swing is platform independent and supports a customizable look and feel, making
it more powerful than AWT.

Features of Swing
●​ Lightweight components.
●​ Platform independent.
●​ Supports pluggable look and feel.
●​ Provides rich GUI components.
●​ Easy to develop interactive applications.

Java Program
import [Link].*;

class SimpleSwing {
public static void main(String args[]) {

JFrame f = new JFrame("Simple Swing");


JLabel l = new JLabel("Welcome to Swing");

[Link](70, 80, 150, 30);


[Link](l);

[Link](300, 200);
[Link](null);
[Link](true);
}
}
Output
A window titled "Simple Swing" is displayed with the message
"Welcome to Swing".

Explanation
●​ JFrame creates the main application window.
●​ JLabel is used to display the text.
●​ setBounds() specifies the position and size of the label.
●​ add() adds the label to the frame.
●​ setVisible(true) makes the frame visible to the user.

Advantages
●​ Easy to create graphical applications.
●​ Provides reusable and lightweight components.
●​ Supports platform-independent execution.
●​ Offers better customization and flexibility than AWT.
●​ Improves the user experience with rich GUI elements.
3)What are the various layout managers used in
Java?

Introduction
A Layout Manager in Java is an object that controls the arrangement and positioning of
components in a container such as a Frame or Panel. It automatically organizes GUI
components according to predefined rules, making the interface flexible and platform
independent.

Types of Layout Managers


1.​ FlowLayout
○​ Arranges components from left to right in a row.
○​ It is the default layout for Panel.
2.​ BorderLayout
○​ Divides the container into five regions:​
North, South, East, West, and Center.
○​ It is the default layout for Frame.
3.​ GridLayout
○​ Arranges components in a grid of rows and columns.
○​ All components have equal size.
4.​ CardLayout
○​ Displays one component at a time like a stack of cards.
○​ Used in applications with multiple screens.
5.​ GridBagLayout
○​ A flexible layout that arranges components in a grid with different sizes and
positions.

Java Program
import [Link].*;

class FlowDemo {
public static void main(String args[]) {
Frame f = new Frame();
[Link](new FlowLayout());
[Link](new Button("One"));
[Link](new Button("Two"));
[Link](new Button("Three"));
[Link](300, 200);
[Link](true);
}
}
Output
A window is displayed with three buttons arranged from left to right.

Explanation
●​ FlowLayout arranges components in a sequential manner.
●​ setLayout() is used to specify the layout manager.
●​ Buttons are automatically positioned according to the selected layout.

Advantages
●​ Automatically arranges components.
●​ Makes GUI design simple and flexible.
●​ Supports platform-independent interfaces.
●​ Improves the appearance and maintainability of applications.
4)Explain the life cycle of an applet

Introduction
An Applet is a small Java program that runs inside a web browser or an applet viewer. The
execution of an applet is controlled by the Java Virtual Machine (JVM). During its execution,
an applet passes through different stages called the Applet Life Cycle.

Applet Life Cycle Methods


1.​ init()
○​ Called only once when the applet is loaded.
○​ Used to initialize variables and objects.
2.​ start()
○​ Called after init() and whenever the applet becomes active.
○​ Used to start or resume execution.
3.​ paint(Graphics g)
○​ Called whenever the applet needs to display or redraw its output.
○​ Used for drawing text, shapes, and graphics.
4.​ stop()
○​ Called when the user leaves the applet page.
○​ Used to suspend execution.
5.​ destroy()
○​ Called only once before the applet is removed from memory.
○​ Used to release allocated resources.

Applet Life Cycle Diagram


init()

start()

paint()

stop()

destroy()
Java Program
import [Link];
import [Link];
public class LifeCycleDemo extends Applet {
public void init() {
[Link]("Applet Initialized");
}
public void start() {
[Link]("Applet Started");
}
public void paint(Graphics g) {
[Link]("Hello Applet", 50, 50);
}
public void stop() {
[Link]("Applet Stopped");
}
public void destroy() {
[Link]("Applet Destroyed");
}
}

Output
Applet Initialized
Applet Started
Hello Applet
Applet Stopped
Applet Destroyed

Explanation
●​ init() initializes the applet.
●​ start() begins execution.
●​ paint() displays the output.
●​ stop() pauses the applet when it becomes inactive.
●​ destroy() releases resources before the applet is removed.

Advantages
●​ Simple to develop and execute.
●​ Platform independent.
●​ Supports graphical user interfaces.
●​ Can be embedded in web pages.

You might also like