MCA SEM2
UNIT 5
Presented By : Dr. Abhishek Roy
COURSE CODE :CSE21949
COURSE : Object Oriented Programming with
Java
AWT Class Hierarchy (Basic AWT Example)
import java.awt.*;
import java.awt.event.*;
public class AWTExample {
public static void main(String[] args) {
Frame f = new Frame("AWT Example");
Button b = new Button("Click Me");
b.setBounds(100, 100, 80, 30);
f.add(b);
f.setSize(300, 200);
f.setLayout(null);
f.setVisible(true);
}
}
Explanation: This program creates a basic AWT window with a button using Frame and
Button components.
Introduction to Swing (Simple JFrame
Example)
import javax.swing.*;
public class SwingExample {
public static void main(String[] args) {
JFrame f = new JFrame("Swing Example");
JButton b = new JButton("Click");
b.setBounds(100, 100, 100, 40);
f.add(b);
f.setSize(300, 200);
f.setLayout(null);
f.setVisible(true);
}
}
Explanation: This program creates a Swing window using JFrame and adds a JButton to
it.
Swing vs AWT
// Using AWT
Button awtButton = new Button("AWT Button");
// Using Swing
JButton swingButton = new JButton("Swing Button");
Explanation: AWT is heavyweight (uses native system resources), whereas Swing is
lightweight and more flexible with richer components.
Swing Component Hierarchy Example
import javax.swing.*;
public class SwingHierarchy {
public static void main(String[] args) {
JFrame frame = new JFrame("Hierarchy Demo");
JLabel label = new JLabel("Hello Swing");
frame.add(label);
frame.setSize(200, 100);
frame.setVisible(true);
}
}
Explanation: Swing components like JLabel are added to containers like JFrame,
forming a hierarchy.
Swing Containers – JFrame, JPanel
import javax.swing.*;
public class JPanelExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Panel Example");
JPanel panel = new JPanel();
JButton button = new JButton("OK");
panel.add(button);
frame.add(panel);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
Explanation: JPanel is a generic container to group components; added inside JFrame.
JDialog Example
import javax.swing.*;
public class DialogExample {
public static void main(String[] args) {
JFrame f = new JFrame();
JDialog d = new JDialog(f, "Dialog Example", true);
d.setSize(200, 100);
d.setVisible(true);
}
}
Explanation: JDialog is a pop-up window typically used for short-term tasks like alerts
or input.
JTextField and JTextArea
import javax.swing.*;
public class TextExample {
public static void main(String[] args) {
JFrame f = new JFrame("Text Example");
JTextField tf = new JTextField("Type here");
JTextArea ta = new JTextArea("This is a text area");
tf.setBounds(50, 50, 150, 20);
ta.setBounds(50, 80, 200, 100);
f.add(tf); f.add(ta);
f.setSize(300, 250);
f.setLayout(null);
f.setVisible(true);
}
}
Explanation: JTextField is for single-line input, while JTextArea handles multi-line input.
Layout Management – BorderLayout
Example
import javax.swing.*;
import java.awt.*;
public class BorderLayoutExample {
public static void main(String[] args) {
JFrame f = new JFrame("BorderLayout");
f.setLayout(new BorderLayout());
f.add(new JButton("North"), BorderLayout.NORTH);
f.add(new JButton("South"), BorderLayout.SOUTH);
f.add(new JButton("East"), BorderLayout.EAST);
f.add(new JButton("West"), BorderLayout.WEST);
f.add(new JButton("Center"), BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}
Explanation: BorderLayout places components in five regions: North, South, East,
West, and Center.
Layout Management – GridLayout Example
import javax.swing.*;
import java.awt.*;
public class GridLayoutExample {
public static void main(String[] args) {
JFrame f = new JFrame("GridLayout");
f.setLayout(new GridLayout(2, 2));
f.add(new JButton("1"));
f.add(new JButton("2"));
f.add(new JButton("3"));
f.add(new JButton("4"));
f.setSize(200, 200);
f.setVisible(true);
}
}
Explanation: GridLayout arranges components in a grid format with equal-sized cells.
Layout Management – FlowLayout Example
import javax.swing.*;
import java.awt.*;
public class FlowLayoutExample {
public static void main(String[] args) {
JFrame f = new JFrame("FlowLayout");
f.setLayout(new FlowLayout());
f.add(new JButton("A"));
f.add(new JButton("B"));
f.add(new JButton("C"));
f.setSize(200, 100);
f.setVisible(true);
}
}
Explanation: FlowLayout arranges components in a left-to-right flow, wrapping to the
next line as needed.
Event Handling – ActionListener Example
import javax.swing.*;
import java.awt.event.*;
public class ButtonEvent {
public static void main(String[] args) {
JFrame f = new JFrame("Event Example");
JButton b = new JButton("Click");
b.setBounds(100, 100, 80, 30);
b.addActionListener(e -> System.out.println("Button Clicked!"));
f.add(b);
f.setSize(300, 200);
f.setLayout(null);
f.setVisible(true);
}
}
Explanation: ActionListener listens for actions like button clicks and handles them via
actionPerformed.
Handling Mouse Events
import javax.swing.*;
import java.awt.event.*;
public class MouseEventDemo {
public static void main(String[] args) {
JFrame f = new JFrame("Mouse Example");
JLabel l = new JLabel("No Event");
l.setBounds(50, 50, 150, 20);
f.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
});
f.add(l);
f.setSize(300, 200);
f.setLayout(null);
f.setVisible(true);
}
}
Explanation: Mouse events like clicks can be handled using MouseListener or
MouseAdapter.
Using Adapter Classes
import javax.swing.*;
import java.awt.event.*;
public class WindowAdapterExample {
public static void main(String[] args) {
JFrame f = new JFrame("Adapter Demo");
f.setSize(300, 200);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.out.println("Window Closed");
System.exit(0);
}
});
f.setVisible(true);
}
}
Explanation: Adapter classes provide empty implementations of listener interfaces so
only needed methods are overridden.
Applet Lifecycle – Simple Applet Example
import java.applet.Applet;
import java.awt.Graphics;
public class HelloApplet extends Applet {
public void init() {
System.out.println("Applet Initialized");
}
public void paint(Graphics g) {
g.drawString("Hello Applet", 50, 50);
}
}
Explanation: Applets have a lifecycle: init(), start(), stop(), and destroy()—paint() is
used to draw UI.
Passing Parameters to Applets
import java.applet.Applet;
import java.awt.Graphics;
public class ParamApplet extends Applet {
public void paint(Graphics g) {
String name = getParameter("user");
g.drawString("Welcome " + name, 50, 50);
}
}
Explanation: getParameter() fetches values passed via <param> tag in HTML used to
embed the applet.
THE END