0% found this document useful (0 votes)
7 views24 pages

Unit2 AWTpptx 2024 09 05 08 04 44

Uploaded by

mayankahir81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views24 pages

Unit2 AWTpptx 2024 09 05 08 04 44

Uploaded by

mayankahir81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Unit 2

AWT
Computer
Engineering
Diploma

(Abstract Window Unit - 2


AWT (Abstract

Toolkit)
Window Toolkit)
09CE1504
AWT & Swing
 Abstract Window Toolkit is known as AWT.

 It is used to develop GUI or window based Application.

 AWT Components are Heavy Weight because it is platform


AWT dependent.
 View of components are as per view of Operating System.

 Java.awt package contains graphics classes.

 Java.awt.event package contains event classes.


AWT Class
Hierarchy
 A component is an object having a graphical representation.

 Component is an abstract class.

 Components can be displayed on the screen.


Compone  Components allow the user to interact with the program.
nt
 Swing is a small java program that runs on a web browser.

 Lightweight

Swing: An  Much bigger set of built-in controls. Trees, image buttons,


Overview tabbed panes, sliders, toolbars, color choosers, tables, text
areas to display HTML.

 More customizable. Can change border, text alignment, or add


image to almost any control. Can customize how minor
features are drawn.
 Components are named JName. e.g. JFrame, JPanel, JApplet,
JDialog, JButton, etc.

 There is an almost-equivalent Swing component for most


Swing AWT components.

 Instead of adding components directly to frames or


applets, use the content pane
Frame

Swing -
Demo

Content Pane
 JComponent
 JFrame
 JComboBox, JLabel
 JList, JTextComponent
Swing -  JMenuBar

Compone  JPanel, JFrame


 JProgressBar, JScrollBar
nts  JSlider
 JTable, JTree
 Components go in the "content pane", not directly in the
frame.

Swing  Changing other properties (layout manager, background


color, etc.) also apply to the content pane.
Compone
nts -  JFrames close automatically when you click on the close
button.
JFrame
 You get Java (Metal) look by default, so you have to explicitly
switch if you want native look.
import javax.swing.*;
import java.awt.*;

Swing public class frame


{
Compone
public static void main(String args[])
nts – {
JFrame JFrame frm = new JFrame("Welcome to
Marwadi University");
Example frm.setSize(300,300);
frm.setVisible(true);
}
}
 Java: GUI component = class

GUI  Properties

Componen  Methods
JButton

t API 
 Events
(1) Create it
 Instantiate object: b = new JButton(“press me”);

Using a (2) Configure it


 Properties: b.text = “Submit”;
GUI  Methods: b.setText(“Submit”);
Componen (3) Add it
 panel.add(b);
t JButton
(4) Listen to it
 Events: Listeners
GUI Internal structure

JFrame
JPanel
Anatomy JFrame

of an JButton
containers

Applicatio JPanel

n GUI
JLabel
JButton JLabel
(1) Create: Listener
 Frame
 Panel
JLabel JButton
 Components
Build from  Listeners
bottom up JPanel
(2) Add: (bottom up)
 listeners into components
 components into panel
 panel into frame
JFrame
JFrame f = new JFrame(“title”);
JPanel p = new JPanel( );
JButton b = new JButton(“Submit”);
Submit
f.setSize(400,400);
f.setVisible(true);

Example p.add(b); //add button to panel


f.add(p); //add panel to frame
import javax.swing.*;
class hello
{ Submit
public static void main(String[] args)
{
JFrame f = new JFrame(“Frame1”);
JPanel p = new JPanel();
Applicatio JButton b = new JButton(“Submit”);
n Code p.add(b); // add button to panel
f.add(p);
f.setSize(500,500);
f.setVisible(true);

}
}
 The process of determining the size and position of
components.

 Layout management can be done using absolute


Layout positioning.
 Size and position of every component within the
Managem container must be specified.
ent  Does not adjust well when the top-level container is
resized.

 Does not adjust well to differences between users and


systems, such as font size.
 The Java platform supplies four commonly used layout
managers:

1) BorderLayout
2) BoxLayout
Layout 3) FlowLayout
Managem 4) GridLayout

ent (Cont)
 Has five areas available to hold components
 north, south, east, west and center
Layout
 All extra space is placed in the center area
Manageme  Only the center area is affected when the container is resized.
nt (1)
 Default layout manager of content panes.
Border
Layout
 Places components in a single row (left to right) or
column (top to bottom).
 Respects component’s maximum size and alignment
Layout hints.

Manageme
nt (2) Box
Layout
 Places components from left to right, starting new
rows if necessary.
 Default LayoutManager of JPanel.
Layout
Manageme
nt (3) Flow
Layout
 Places components in a requested number of rows
and columns.
 Components are placed left-to-right and top-to-
bottom.
Layout
 Forces all components to be the same size
Manageme  as wide as the widest component's preferred width
nt (4) Grid  as high as the highest component’s preferred height

Layout
Thank you….

You might also like