Ch 2 link is at the bottom of the page
UNIT - 1 JAVA SWING
1. LAYOUT MANAGEMENT
Commonly Used Layout Managers in Java
Java provides several layout managers, each with unique rules for arranging components.
1. FlowLayout
- Description: Arranges components in a row, wrapping to the next row if the space runs out.
- Default Layout for:
JPanel
- Alignment Options: Left, center (default), or right.
- Code Example:
- Use Cases: Simple layouts with a small number of components.
2. BorderLayout
- Description: Divides the container into five regions: North, South, East, West, and Center.
- Default Layout for:
JFrame
- Code Example:
- Use Cases: Organizing components around a central area.
3. GridLayout
- Description: Arranges components in a grid with equal-sized cells.
- Constructor Parameters: Rows, Columns (or just Rows with 0 for dynamic columns).
- Code Example:
- Use Cases: When you need a tabular layout.
4. GridBagLayout
- Description: A flexible and powerful layout manager allowing components to span multiple rows or columns.
- Key Feature: Uses a
GridBagConstraints
object to define each component's size, position, and alignment. - Code Example:
- Use Cases: Complex layouts with precise control over positioning.
5. CardLayout
- Description: Stacks components like cards, showing one at a time.
- Code Example:
- Use Cases: Implementing tabbed views or wizards.
6. BoxLayout
- Description: Arranges components either vertically (
BoxLayout.Y_AXIS
) or horizontally (BoxLayout.X_AXIS
). - Code Example:
- Use Cases: Stacking components in a single axis.
Custom Layouts
If none of the built-in layout managers meet your needs, you can create a custom layout by implementing the LayoutManager
or LayoutManager2
interfaces.
Key Methods in Layout Managers
All layout managers use specific methods to arrange components:
addLayoutComponent(String name, Component comp)
: Adds a component with constraints.removeLayoutComponent(Component comp)
: Removes a component.preferredLayoutSize(Container parent)
: Returns the preferred size for the container.minimumLayoutSize(Container parent)
: Returns the minimum size for the container.layoutContainer(Container parent)
: Arranges components within the container.
Choosing the Right Layout Manager
- FlowLayout: Simple layouts with a linear flow.
- BorderLayout: Good for dividing a UI into major areas.
- GridLayout: Ideal for grids and tables.
- GridBagLayout: When precise control is required.
- CardLayout: For swapping views or pages.
- BoxLayout: For vertical or horizontal stacking.
Best Practices for Java Layout Management
- Combine Layout Managers: Nest panels with different layouts for complex designs.
- Use Insets and Padding: Adjust spacing for better aesthetics.
- Minimize GridBagLayout Use: Use it only for truly complex needs due to its verbosity.
- Test Responsiveness: Resize the container to ensure components adapt properly.
Example: Combining Layouts
This example shows how combining layouts can handle complex UI requirements.
Click here for more IMP AND EXAMPLES 👇
2. EVENT HANDLING
Commonly Used Listener Interfaces
1. ActionListener
- Used For: Button clicks, menu item selections.
- Key Method:
actionPerformed(ActionEvent e)
2. MouseListener
- Used For: Mouse events like clicks, movement, entering/exiting a component.
- Key Methods:
mouseClicked(MouseEvent e)
mousePressed(MouseEvent e)
mouseReleased(MouseEvent e)
mouseEntered(MouseEvent e)
mouseExited(MouseEvent e)
3. KeyListener
- Used For: Keyboard events like key presses.
- Key Methods:
keyPressed(KeyEvent e)
keyReleased(KeyEvent e)
keyTyped(KeyEvent e)
4. WindowListener
- Used For: Window events like opening, closing, minimizing.
- Key Methods:
windowClosing(WindowEvent e)
windowOpened(WindowEvent e)
windowClosed(WindowEvent e)
windowActivated(WindowEvent e)
windowDeactivated(WindowEvent e)
Event Adapters
- Purpose: Simplify event handling by providing default (empty) implementations of listener interfaces.
- Common Adapters:
MouseAdapter
KeyAdapter
WindowAdapter
Example Using MouseAdapter
:
إرسال تعليق