Unit 1
1.Introduction/Define LayoutManager.
LayoutManager is used to arrange components automatically inside a container.
2) Define Component:
A component is a small part of a screen like a button or label.
It is used to show information or take input from the user.
3) Define Container:
A container is used to hold components.
It helps to arrange and manage them properly.
4) Explain GridLayout with Example
GridLayout is a LayoutManager in Java that arranges components in a grid of rows and columns.
Each component is placed in a cell, and all cells are of equal size.
>Features of GridLayout
1. Equal Size Components
All components have the same width and height.
2. Row and Column Structure
Components are arranged in rows and columns like a table.
3. Automatic Placement
Components are added from left to right, top to bottom.
4. No Overlapping
Each cell contains only one component.
>Syntax
GridLayout(int rows, int columns)
>Example (Java AWT)
import java.awt.*;
public class GridLayoutExample {
public static void main(String[] args) {
Frame f = new Frame("GridLayout Example");
// Set GridLayout with 2 rows and 3 columns
f.setLayout(new GridLayout(2, 3));
f.add(new Button("1"));
f.add(new Button("2"));
f.add(new Button("3"));
f.add(new Button("4"));
f.add(new Button("5"));
f.add(new Button("6"));
f.setSize(300, 200);
f.setVisible(true);
}
}
>Explanation of Example
Layout is set to 2 rows and 3 columns
Total 6 buttons are added
Each button is placed in one cell
All buttons appear in equal size in grid form
>Advantages
Simple and easy to use
Makes UI neat and organized
Best for forms and calculator layouts
>Disadvantages
No flexibility in component size
Cannot merge cells
5) Explain BorderLayout with Example
BorderLayout is a LayoutManager in Java that divides the container into 5 regions:
North, South, East, West, and Center.
>Regions of BorderLayout
North → Top
South → Bottom
East → Right side
West → Left side
Center → Middle area
>Features
1. Container is divided into 5 parts
2. Each region can hold only one component
3. Center gets maximum space
4. Components are resized automatically
>Example (Java AWT)
import java.awt.*;
public class BorderLayoutExample {
public static void main(String[] args) {
Frame f = new Frame("BorderLayout Example");
f.setLayout(new BorderLayout());
f.add(new Button("North"), BorderLayout.NORTH);
f.add(new Button("South"), BorderLayout.SOUTH);
f.add(new Button("East"), BorderLayout.EAST);
f.add(new Button("West"), BorderLayout.WEST);
f.add(new Button("Center"), BorderLayout.CENTER);
f.setSize(300, 200);
f.setVisible(true);
}
}
>Explanation
5 buttons are added to 5 regions
Each button appears in its respective position
Center button takes the largest space
>Advantages
Easy to use
Good for main window layout
Automatically adjusts size
>Disadvantages
Only one component per region
Less flexible for complex layouts
6) Explain BoxLayout with Example
BoxLayout is a LayoutManager in Java Swing that arranges components in a single row or a single column.
👉 It works in one direction only:
X_AXIS → Horizontal (left to right)
Y_AXIS → Vertical (top to bottom)
>Features
1. Single Direction Layout
Components are arranged either in a row or a column.
2. Flexible Alignment
Components can be aligned properly.
3. Respects Component Size
Uses preferred size of components.
4. Used in Swing
Mainly used with JPanel.
>Syntax
setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
or
setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
>Example (Java Swing)
import javax.swing.*;
public class BoxLayoutExample {
public static void main(String[] args) {
JFrame f = new JFrame("BoxLayout Example");
JPanel p = new JPanel();
// Set BoxLayout (Vertical)
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
p.add(new JButton("Button 1"));
p.add(new JButton("Button 2"));
p.add(new JButton("Button 3"));
f.add(p);
f.setSize(300, 200);
f.setVisible(true);
}
}
>Explanation
Layout is set to vertical (Y_AXIS)
Buttons are arranged top to bottom
All components appear in a single column
>Advantages
Simple and flexible
Good for vertical/horizontal menus
Easy alignment control
>Disadvantages
Only one direction layout
Not suitable for complex grid designs
7) Explain JComboBox with its Methods and Example
JComboBox is a Swing component used to create a drop-down list, where the user can select one item from multiple options.
>Features
1. Shows a list of items in drop-down form
2. Allows single selection only
3. Can be editable or non-editable
4. Easy to use in forms
>Common Methods of JComboBox
1. addItem(Object item)
→ Adds an item to the list
2. removeItem(Object item)
→ Removes an item
3. getSelectedItem()
→ Returns selected item
4. setSelectedItem(Object item)
→ Sets selected item
5. getItemCount()
→ Returns total number of items
>Example (Java Swing)
import javax.swing.*;
import java.awt.*;
public class Demo {
public static void main(String[] args) {
JFrame f = new JFrame("JComboBox Example");
f.setLayout(new FlowLayout());
String[] items = {"Java", "Python", "C++"};
JComboBox cb = new JComboBox(items);
f.add(cb);
f.setSize(300, 200);
f.setVisible(true);
}
}
>Advantages
Saves space (compact UI)
Easy selection
User-friendly
>Disadvantages
Only one item selection
Limited visible options
8) Explain JCheckBox with its Methods and Example (Easy + Detailed)
JCheckBox is a Swing component used to create a checkbox.
It allows the user to select (tick) or unselect (untick) one or more options.
>Features
1. Supports multiple selection
2. Can be checked or unchecked
3. Commonly used in forms and settings
4. Simple and user-friendly
>Important Methods
1. setSelected(boolean b)
→ Used to check or uncheck the checkbox
2. isSelected()
→ Returns true if checkbox is selected
3. setText(String text)
→ Sets the text of checkbox
4. getText()
→ Gets the text of checkbox
>Example (Java Swing)
import javax.swing.*;
import java.awt.*;
public class Demo {
public static void main(String[] args) {
JFrame f = new JFrame("JCheckBox Example");
f.setLayout(new FlowLayout());
JCheckBox c1 = new JCheckBox("Java");
JCheckBox c2 = new JCheckBox("Python");
f.add(c1);
f.add(c2);
f.setSize(300, 200);
f.setVisible(true);
}
}
>Advantages
Allows multiple choices
Easy to use
Good for user input
9) Explain JRadioButton with its Methods and Example (Detailed & Easy)
JRadioButton is a Swing component used to create radio buttons.
It allows the user to select only one option from multiple choices.
👉 To ensure only one option is selected, radio buttons are added to a ButtonGroup.
Features
Single Selection
Only one option can be selected at a time.Used in Forms
Commonly used for choices like gender, payment mode, etc.ButtonGroup Support
ButtonGroup is used to group multiple radio buttons.User Friendly
Easy to use and understand.
Important Methods
setSelected(boolean b)
→ Used to select or unselect the radio buttonisSelected()
→ Returns true if the button is selectedsetText(String text)
→ Sets the text of the radio buttongetText()
→ Returns the text of the radio button
Example (Java Swing)
import javax.swing.*;
import java.awt.*;
public class Demo {
public static void main(String[] args) {
JFrame f = new JFrame("JRadioButton Example");
f.setLayout(new FlowLayout());
// Create radio buttons
JRadioButton r1 = new JRadioButton("Male");
JRadioButton r2 = new JRadioButton("Female");
// Create ButtonGroup
ButtonGroup bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
// Add to frame
f.add(r1);
f.add(r2);
f.setSize(300, 200);
f.setVisible(true);
}
}
Explanation
Two radio buttons (Male and Female) are created
Both are added to a ButtonGroup
ButtonGroup ensures only one can be selected at a time
Layout is set using FlowLayout
Advantages
Allows only one selection
Clean and organized UI
Easy to implement
Disadvantages
Cannot select multiple options
Requires ButtonGroup for proper working
Conclusion (Exam Line)
JRadioButton is used to select only one option from multiple choices using ButtonGroup.
10) Explain JLabel and JTextField with Methods and Example (More Detailed & Easy)
JLabel
JLabel is a Swing component used to display text, image, or both on the screen.
It is mainly used to give instructions or labels to the user and does not accept input.
Features of JLabel
Used to display information only
Cannot be edited by user
Can show text, image, or both
Supports text alignment and styling
Important Methods of JLabel
setText(String text)
→ Sets the text of labelgetText()
→ Returns the textsetForeground(Color c)
→ Changes text colorsetFont(Font f)
→ Changes font style and sizesetHorizontalAlignment(int align)
→ Sets alignment (LEFT, CENTER, RIGHT)
JTextField
JTextField is a Swing component used to accept single-line input from the user.
It allows the user to enter, edit, and read text.
Features of JTextField
Accepts single-line text input
Editable by user
Can set size and limit text
Used in forms (name, email, etc.)
Important Methods of JTextField
setText(String text)
→ Sets text in the fieldgetText()
→ Gets user-entered textsetEditable(boolean b)
→ Enables or disables editingsetColumns(int n)
→ Sets width of text fieldsetFont(Font f)
→ Changes font
Example (Java Swing)
import javax.swing.*;
import java.awt.*;
public class Demo {
public static void main(String[] args) {
JFrame f = new JFrame("JLabel & JTextField Example");
f.setLayout(new FlowLayout());
// Create JLabel
JLabel l = new JLabel("Enter Name:");
// Create JTextField
JTextField t = new JTextField(15);
// Add components
f.add(l);
f.add(t);
f.setSize(300, 200);
f.setVisible(true);
}
}
Explanation
JLabel displays the message "Enter Name:"
JTextField allows user to type input
User enters data, which can be accessed using getText()
Layout arranges both components properly
Advantages
JLabel
Simple to display text
Supports styling and alignment
JTextField
Easy input from user
Editable and flexible
Conclusion (Exam Line)
JLabel is used to display text or information, while JTextField is used to take single-line input from the user.
11) Write a Note on Event Delegation Model (Easy + Detailed)
The Event Delegation Model in Java is used to handle events (actions) generated by user interaction like button click, key press, mouse click, etc.
👉 In this model, an event source sends the event to an event listener for processing.
Main Parts of Event Delegation Model
Event Source
The component that generates the event
(Example: Button, TextField)Event Object
Contains information about the event
(like type of event, source, etc.)Event Listener
The object that receives and handles the event
Working of Event Delegation Model
User performs an action (click, type, etc.)
Event is generated by the source
Event object is created
Event is sent to the registered listener
Listener handles the event
Example (Button Click Event)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Demo {
public static void main(String[] args) {
JFrame f = new JFrame("Event Example");
JButton b = new JButton("Click Me");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
});
f.setLayout(new FlowLayout());
f.add(b);
f.setSize(300, 200);
f.setVisible(true);
}
}
Advantages
Clear separation between event source and handling
Improves code readability
Easy to manage multiple events
Conclusion (Exam Line)
Event Delegation Model handles events by sending them from source to listener for processing.
No comments:
Post a Comment