Prepared By: Uday Shah
Ruparel Education Pvt. Ltd – (HOD-IT)
Asst. Prof.,
Faculty of Computer Applications,
Nobile University
Unit 5 – Introduction to Java
1. Explain the architecture of
Swing.
Swing is a part of Java Foundation Classes
(JFC) used to create graphical user interface (GUI) applications. It provides
ready-made components like buttons, labels, text fields, tables, menus, etc.
Swing is built on top of AWT (Abstract Window Toolkit), so it uses AWT’s basic
window features but gives more advanced controls.
The architecture of Swing follows a Model-View-Controller
(MVC) design pattern. In this pattern, the Model stores data, the View
displays data on the screen, and the Controller handles user actions
such as clicking buttons or typing text.
For example, in a JTextField, the text
entered by the user is stored in the model, the textbox shown on screen is the
view, and keyboard actions are controlled by the controller.
Swing components are lightweight because they
are written in Java and do not depend fully on the operating system. This
allows Swing applications to look similar on Windows, Linux, and Mac.
Basic Swing hierarchy:
Object
└── Component
└── Container
└── JComponent
├── JButton
├── JLabel
├── JTextField
└── JTable
Example:
import javax.swing.*;
class Demo {
public static void main(String args[]) {
JFrame f = new JFrame("Swing
Example");
JButton b = new
JButton("Click");
b.setBounds(100,100,100,30);
f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
2. Differentiate between AWT
and Swing.
AWT stands for Abstract Window Toolkit. It is
Java’s first GUI library. Swing is a newer and improved GUI library developed
as part of JFC. Swing is built on top of AWT.
AWT components are heavyweight,
meaning they use native operating system components. Swing components are lightweight,
meaning they are mostly written in Java and do not depend on OS components.
Swing provides more controls such as JTable,
JTree, JTabbedPane, JColorChooser, etc. AWT has limited controls like Button,
Label, TextField, Checkbox.
Swing supports pluggable look and feel,
so the design can be changed. AWT uses fixed OS look and feel.
|
Feature |
AWT |
Swing |
|
Full Form |
Abstract Window Toolkit |
Part of Java Foundation Classes |
|
Components |
Heavyweight |
Lightweight |
|
Look |
Native OS Look |
Customizable |
|
Controls |
Limited |
Rich Controls |
|
Speed |
Faster sometimes |
Slightly slower |
Example:
// AWT
Button b = new
Button("OK");
// Swing
JButton b = new
JButton("OK");
3. Explain the Swing
lightweight component model.
Swing components
are called lightweight components because they do not use separate native
screen resources from the operating system. They are drawn by Java itself.
For example, when
a JButton is displayed, Java paints it using code instead of asking Windows or
Linux to create a real OS button. Because of this, Swing components look
similar on every platform.
Lightweight
components reduce dependency on operating system design. This gives flexibility
in appearance and behavior. Java can easily change colors, themes, fonts,
borders, and styles.
All Swing
components except top-level containers like JFrame, JDialog, and JWindow are
lightweight. These top-level containers are heavyweight because they need OS
windows.
Advantages of
lightweight model:
- Same look on all platforms
- Rich GUI design
- Custom painting possible
- Easy theme support
- Better portability
Example:
JButton btn = new
JButton("Save");
JLabel lbl = new
JLabel("Name");
These are
lightweight Swing components.
4. Describe the role of layout
managers in Java.
A layout manager is used to arrange GUI
components automatically inside a container such as a frame, panel, or JFRAME. It
controls the position and size of buttons, labels, text fields, etc.
Instead of manually giving coordinates,
layout managers organise components properly even when the window size changes.
This makes the GUI flexible and professional.
Java provides many layout managers, such as the following:
- FlowLayout
- BorderLayout
- GridLayout
- CardLayout
- GridBagLayout
For example, if you resize a window, the layout
manager automatically adjusts components according to available space.
Without a layout manager:
setLayout(null);
button.setBounds(10, 20, 100, 30);
With layout manager:
setLayout(new FlowLayout());
add(button);
Advantages:
- Easy
GUI design
- Automatic
resizing
- Platform
independent
- Less
coding
- Better
alignment
5. Explain FlowLayout with a
suitable example.
FlowLayout is the default layout manager for
Panel and JPanel. It places components in a row from left to right, similar to
words in a paragraph.
When one row becomes full, components
automatically move to the next row. It is simple and useful for small forms.
FlowLayout supports alignment:
- LEFT
- CENTER
- RIGHT
Syntax:
setLayout(new FlowLayout());
Example:
import javax.swing.*;
import java.awt.*;
class FlowDemo {
public static void main(String args[]) {
JFrame f = new JFrame();
f.setLayout(new FlowLayout());
f.add(new JButton("One"));
f.add(new JButton("Two"));
f.add(new JButton("Three"));
f.setSize(300,200);
f.setVisible(true);
}
}
Output:
[One] [Two] [Three]
6. Explain BorderLayout with
its regions.
BorderLayout divides a container into five
regions. It is the default layout for the JFrame content pane.
The five regions are:
- North
- South
- East
- West
- Center
Diagram:
|
NORTH |
||
|
WEST |
CENTER |
EAST |
|
SOUTH |
||
Only one component can be added to
each region. If no region is given, the component goes to the center.
Example:
setLayout(new BorderLayout());
add(new JButton("Top"),
BorderLayout.NORTH);
add(new
JButton("Bottom"), BorderLayout.SOUTH);
add(new JButton("Left"),
BorderLayout.WEST);
add(new
JButton("Right"), BorderLayout.EAST);
add(new
JButton("Center"), BorderLayout.CENTER);
It is useful for the main window
design.
7. Explain GridLayout with a diagram.
GridLayout arranges components in rows and columns like a
table. Every cell has an equal size.
It is useful when many buttons need equal arrangement, such as a calculator keypad or a login form.
Syntax:
setLayout(new GridLayout(rows, columns));
Diagram for GridLayout(2,3):
|
C1 |
C2 |
C3 |
|
C4 |
C5 |
C6 |
Example:
setLayout(new GridLayout(2, 2));
add(new
JButton("1"));
add(new
JButton("2"));
add(new
JButton("3"));
add(new
JButton("4"));
Advantages:
- Equal-sized components
- Neat arrangement
- Easy to use
8. Write a
short note on JMenuBar, JMenu, and JMenuItem.
These classes are
used to create menus in Swing applications.
JMenuBar is the
top menu bar placed in a frame. It contains multiple menus like File, Edit, and Help.
JMenu represents
one menu inside the menu bar. Example: File menu.
A JMenuItem represents items inside a menu, such as Open, Save, and Exit.
Hierarchy:
JMenuBar
├──
JMenu (File)
│
├── JMenuItem(Open)
│
└── JMenuItem(Save)
Example:
JMenuBar mb = new
JMenuBar();
JMenu file = new
JMenu("File");
JMenuItem open =
new JMenuItem("Open");
file.add(open);
mb.add(file);
frame.setJMenuBar(mb);
9. Explain the Graphics class in Java.
The graphics class is
used for drawing shapes, text, and images in Java GUI applications.
It provides
methods like the following:
- drawLine()
- drawRect()
- drawOval()
- drawString()
- fillRect()
A graphics object
is automatically passed to paint() or paintComponent() methods.
Example:
public void
paint(Graphics g) {
g.drawLine(50,50,150,50);
g.drawRect(50,70,100,50);
g.drawString("Hello", 80, 150);
}
Uses:
- Drawing diagrams
- Custom design
- Charts
- Animation basics
13. Explain MouseEvent with an example.
A mouse event occurs when the user uses the mouse in a GUI
application.
Common mouse events:
- mouseClicked()
- mousePressed()
- mouseReleased()
- mouseEntered()
- mouseExited()
The MouseEvent object gives information like:
- X
and Y position
- Click
count
- Which
button pressed
Example:
import javax.swing.*;
import java.awt.event.*;
class Demo extends JFrame implements
MouseListener {
Demo() {
addMouseListener(this);
setSize(300,300);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
System.out.println("X = " + e.getX());
System.out.println("Y = " + e.getY());
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public static void main(String args[]) {
new Demo();
}
}
When user clicks inside frame, mouse position
is displayed.
::Best of Luck ::