AWT and SWING
1
Allows to interact with our programs through mouse movements button clicks, key
presses.
Three things that are necessary to develop GUI
The GUI component sets
Component arrangement
Response to user request
2
JFC (Java Foundation Class)
Encompass a group of features for constructing GUI
AWT (Abstract window tool kit): native GUI
Swing : makes the look and feel of GUI the same across platform
AWT vs Swing (Reading)
3
Graphic classes classification
1. Container classes: used to group component
2. Component classes : subclasses of JComponents
3. Helper classes: used to draw and place objects
4
Dim ensi on Classes in th jav a.awt pack age
Lay ou tMa na g er
He avy we ight
Font 1
FontMet rics
Object Color Panel Applet JApplet
Graphics
Component Container Wind ow Frame JF rame
*
Dial og JDi alog
JComponent Swi ng Co mp one nts
in th e javax .swin g p ackag e
Ligh tweigh t
5
D im en sio n C la s s e s in t h e ja v a .a w t
L a y o u tM a n a g e r package
H e a v y w e ig h t
Font 1
F o n tM e tr i c s
O b jec t C o lo r P an el A p p le t J A p p le t
G r a p h ic s
C o m p on ent C o n ta in e r W in d o w F ram e JF ram e
*
D ia lo g J D ia lo g
JC o m po nen t JP an el S w in g C o m p o n e n ts
in t h e ja v a x . s w in g p a c k a g e
L ig h t w e ig h t
6
D im en sion C lasses in the ja va.aw t
L a you tM an ag er pack age
H ea v yw e ig h t
Fon t 1
F on tM etrics
O b ject C olor P an el A p p let JA p p let
G ra ph ics
C o m p on ent C on tain er W in d ow Fram e JFram e
*
D ialog JD ialog
JC o m po nen t JPan el S w in g C o m p o ne nts
in the ja va x.sw ing p acka ge
L igh tw e ight
7
J C h e c k B o x M e n u Ite m
J M e n u I te m JM enu
A b s tr a c t B u tto n J B u tto n J R a d io B u tto n M e n u I te m
J T o g g le B u tto n JC heckB o x
J R a d io B u tto n
JC o m po nen t J E d ito r P a n e
J T e x tC o m p o n e n t J T e x t F ie ld J P a s s w o r d F ie ld
J T e x tA re a
JLabel J L is t JC o m bo B o x JPane l J O p tio n P a n e J S c r o llB a r J S lid e r
JT abbedP ane J S p litP a n e J L a y e re d P a n e J S e p a r a to r J S c r o llP a n e J R o o tP a n e
J T o o lB a r JM enuB ar JPopupM enu J F ile C h o o s e r J C o lo r C h o o s e r J T o o lT ip
J T re e J T a b le J T a b le H e a d e r J I n te r n a lF r a m e J P ro g re ss B a r J S p in n e r 8
Is a top level window with a title and a border
An independent window can be moved around the screen
9
Panels can be used as sub-containers to group GUI components to achieve the
desired layout.
Panel is a blank rectangular component that can contain other components.
Each panel uses a layout manager to determine the position and size of its child
components.
It is recommended that you place the user interface components in panels and place
the panels in a frame.
You can also place panels in a panel.
10
To add a component to JFrame, you actually add it to the content pane of JFrame.
To add a component to a panel, you add it directly to the panel using the add method
You can use new JPanel() to create a panel with a default FlowLayout manager or
new JPanel(LayoutManager) to create a panel with the specified layout manager.
Use the add(Component) method to add a component to the panel.
For example, JPanel p = new JPanel();
p.add(new JButton("OK"));
11
A pop up window that are not as flexible as a frame
You must create a frame in order to create a dialog
12
13
14
15
• JCheckBox is a widget that has two states.
On and Off. It is a box with a label.
• If the checkbox is checked, it is
represented by a tick in a box.
16
Constructors
cb = new JCheckBox(text); Creates check box, initially unchecked.
cb = new JCheckBox(text, state); Creates check box, checked or not
depending on state.
Methods
state = cb.isSelected(); Returns true if the check box is checked.
cb.setSelected(state); Checks (true) or unchecks check box.
cb.addActionListener(action-listener); Adds an action listener to the radio
button. The action listener will be called if
button is selected.
cb.addItemListener(item-listener); Add an item listener to a radio button.
The item listener will be called if the
button is selected or deselected.
17
• Radio buttons are groups of buttons
in which only one button at a time
can be selected.
18
• A text field can be used to enter or display a string.
19
• A password field can be used to enter a password.
Password field
20
• A JTextArea enables the user to enter multiple lines
of text
21
• A combo box, also known as a choice list or drop-down list, contains
a list of items from which the user can choose
22