Java Notes Unit 4
Java Notes Unit 4
UNIT 4
1) What are the key features of the Java Foundation Classes (JFC)?
The Java Foundation Classes (JFC) encompass a set of APIs and libraries that provide
developers with tools to create graphical user interfaces (GUIs) and handle multimedia and
networking tasks in Java applications. Some key features of the Java Foundation Classes (JFC)
include:
Overall, the Java Foundation Classes (JFC) offer a comprehensive set of tools and APIs for
developing feature-rich and cross-platform Java applications with advanced GUI, graphics,
multimedia, and networking capabilities.
The purpose of FlowLayout is to arrange components in a row, flowing from left to right, with
optional wrapping to the next row.
It is useful for creating simple layouts where components are added one after another
horizontally, like buttons in a toolbar or labels in a panel.
FlowLayout automatically resizes and positions components based on their preferred sizes and
the size of the container.
Constructor:
FlowLayout(): Creates a new FlowLayout with a left alignment and default horizontal and
vertical gaps of 5 pixels.
FlowLayout(int align): Creates a new FlowLayout with the specified alignment. The
alignment can be one of FlowLayout.LEFT, FlowLayout.CENTER, FlowLayout.RIGHT,
FlowLayout.LEADING, or FlowLayout.TRAILING.
Example:
import javax.swing.*;
import java.awt.*;
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
b. BorderLayout:
Purpose:
The purpose of BorderLayout is to arrange components in five regions: north, south, east,
west, and center.
It's suitable for creating layouts where components are positioned around the edges of a
container, with one component occupying the center.
BorderLayout automatically resizes and positions components based on their preferred sizes
and the size of the container.
Constructor:
BorderLayout has two constructors:
BorderLayout(): Creates a new BorderLayout with horizontal and vertical gaps of 0 pixels.
BorderLayout(int hgap, int vgap): Creates a new BorderLayout with the specified horizontal
and vertical gaps between components.
When adding components to a BorderLayout, you specify the region using corresponding constants:
BorderLayout.NORTH
BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.CENTER
Example:
import javax.swing.*;
import java.awt.*;
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
Purpose:
Constructor:
GridLayout(int rows, int cols): Creates a new GridLayout with the specified number of rows
and columns. Components are arranged in a grid with the specified number of rows and
columns.
GridLayout(int rows, int cols, int hgap, int vgap): Creates a new GridLayout with the specified
number of rows and columns, along with horizontal and vertical gaps between components.
import javax.swing.*;
import java.awt.*;
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
Event Source:
Think of the event source as something that can "trigger" events, like a button being clicked
or a key being pressed.
For example, if you click a button on a webpage, that button is the event source.
Event Class:
An event class is like a message that tells you what happened. It holds information about the
event, like which button was clicked or what key was pressed.
For instance, if you click a button, the event class might say, "Button 'Submit' was clicked."
Event Listener:
An event listener is like someone who's listening for a specific type of event to happen.
When that event occurs, the listener reacts to it.
For instance, if you have a listener for button clicks, it will perform a specific action when a
button is clicked, like submitting a form or opening a new window.
The event source (like a button) notices when something happens (like a click).
It creates an event class (like a message) that describes what happened (like "Button
'Submit' was clicked").
Then, it tells the event listener (like a person listening) that the event occurred.
The event listener reacts by performing a specific action, based on what happened.
This model helps keep code organized and makes it easy to create interactive programs, like
---------------x-------------------