Visual Programming Pt5
Visual Programming Pt5
1. AWT API was introduced in JDK 1.0. Most of the AWT components have
become obsolete and should be replaced by newer Swing components.
2.
Swing API, a much more comprehensive set of graphics libraries that
enhances the AWT, was introduced as part of Java Foundation Classes
(JFC) after the release of JDK 1.1. JFC consists of Swing, Java2D,
Accessibility, Internationalization, and Pluggable Look-and-Feel Support
APIs. JFC has been integrated into core Java since JDK 1.2.
3. The latest JavaFX, which was integrated into JDK 8, is meant to replace
Swing
Now... Let’s take a look at
the different between
Swing API and AWT API
Java API for GUI
• Downside of AWT:
• Worked well for simple applications but difficult to write high-
quality portable graphics
• Limited graphics programming to the lowest common
denominator.
• Different platforms had different bugs
Java API for GUI
2. java.awt.event
3. javax.swing
Java API for GUI
Main Packages
1. GUI components
• An object that defines a screen
element to display information or
allow the user to interact with
program in a certain way
2. Event
• Object that represents some occurrence in
which we may be interested
• Event correspond to user actions, such as
pressing a mouse button or typing a key on the
keyboard
• Most GUI components generate events to
indicate a user action related to that
component
• For example, a component representing a
button will generate an event to indicate that
it has been pushed
Elements of GUI applications
3. Listener
• An object that is “waiting” for an event to
occur and that can respond in some way
when it does
Java Containers
Sample codes:
import javax.swing.*; Importing all javax.swing
classes from the package
public class DemojFrame {
public static void main(String[] args) {
JFrame frame = new JFrame(“javax.swing.JFrame");
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
}
}
Creating GUI Label Text
field
Check
Box
Radio
Button
Objects Button
Components
Combo
Box
// Create a button with text OK
JButton jbtOK = new JButton("OK");
import javax.swing.*;
import javax.swing.*;
4. Setimport javax.swing.*;
frame close operation
javax.swing.JFrame
+JFrame() Creates a default frame with no title.
+JFrame(title: String) Creates a frame with the specified title.
+setSize(width: int, height: int): void Specifies the size of the frame.
+setLocation(x: int, y: int): void Specifies the upper-left corner location of the frame.
+setVisible(visible: boolean): void Sets true to display the frame.
+setDefaultCloseOperation(mode: int): void Specifies the operation when the frame is closed.
+setLocationRelativeTo(c: Component): Sets the location of the frame relative to the specified component.
void If the component is null, the frame is centered on the screen.
+pack(): void Automatically sets the frame size to hold the components in the
frame.
User interfaces using frames
Layout Manager
• Java’s layout managers provide a level of
abstraction to automatically map your user
interface on all window systems.
Example:
User interfaces using frames
Layout Manager
FlowLayout
These are methods available in FlowLayout class.
The get and set methods for these data fields are provided in
java.awt.FlowLayout the class, but omitted in the UML diagram for brevity.
Example:
User interfaces using frames
Layout Manager
GridLayout
These are methods available in GridLayout class.
The get and set methods for these data fields are provided in
java.awt.GridLayout the class, but omitted in the UML diagram for brevity.
-rows: int The number of rows in this layout manager (default: 1).
-columns: int The number of columns in this layout manager (default: 1).
-hgap: int The horizontal gap of this layout manager (default: 0).
-vgap: int The vertical gap of this layout manager (default: 0).
Example:
User interfaces using frames
Layout Manager
BorderLayout
These are methods available in BorderLayout class.
The get and set methods for these data fields are provided in
java.awt.BorderLayout the class, but omitted in the UML diagram for brevity.
-hgap: int The horizontal gap of this layout manager (default: 0).
-vgap: int The vertical gap of this layout manager (default: 0).
Syntax:
Font myFont = new Font(name, style, size);
Example:
Font myFont = new Font("SansSerif ", Font.BOLD, 16);
Font myFont = new Font("Serif", Font.BOLD+Font.ITALIC, 12);
User interfaces using frames
Using Panels as Sub-Containers
• Panels act as sub-containers for grouping user
interface components.
Example:
frame
A textfield
p2
A button 12
buttons p1
Common Features of Swing Components
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
java.awt.Component
-font: java.awt.Font The font of this component.
-background: java.awt.Color The background color of this component.
-foreground: java.awt.Color The foreground color of this component.
-preferredSize: Dimension The preferred size of this component.
-visible: boolean Indicates whether this component is visible.
+getWidth(): int Returns the width of this component.
+getHeight(): int Returns the height of this component.
+getX(): int getX() and getY() return the coordinate of the component’s
+getY(): int upper-left corner within its parent component.
java.awt.Container
+add(comp: Component): Component Adds a component to the container.
+add(comp: Component, index: int): Component Adds a component to the container with the specified index.
+remove(comp: Component): void Removes the component from the container.
+getLayout(): LayoutManager Returns the layout manager for this container.
+setLayout(l: LayoutManager): void Sets the layout manager for this container.
+paintComponents(g: Graphics): void Paints each of the components in this container.
The get and set methods for these data fields are provided in
the class, but omitted in the UML diagram for brevity.
javax.swing.JComponent
-toolTipText: String The tool tip text for this component. Tool tip text is displayed when
the mouse points on the component without clicking.
-border: javax.swing.border.Border The border for this component.
End of
Chapter 4 part 1