Applet GUI
Applet GUI
Advanced GUI
The Applet Class
The <applet> HTML Tag
Passing Parameters to Applets
Conversions Between Applications and Applets
Running a Program as an Applet and as an Application
Handling the Mouse Event
Handling the Keyboard Event
Model dynamic behavior using sequence diagrams and
statecharts diagrams
Advanced Layout (CardLayout and GridBagLayout and
using No Layout Manager) (Optional)
The Applet Class
public class MyApplet extends JApplet {
public void init() {
... }
public void start() {
... }
public void stop() {
... }
public void destroy() {
... }
//your other methods
}
Browser Calling Applet Methods
The init() Method
Invoked when the applet is first loaded and again
if the applet is reloaded.
DisplayMessageApp
Keys:
Home VK_HOME
End VK_End
Page Up VK_PGUP
Page Down VK_PGDN
etc...
Example 12.6
Keyboard Events Demo
Objective: Display
a user-input
character. The user
can also move the
character up,
down, left, and
right using the
arrow keys.
Activation
anotherMethod()
Method Invocation
Sequence diagrams, cont.
setXCoordinate
repaint
Statechart diagrams
Statechart diagrams describe flow of control of
the object.
Indicate
Initial State
State1
Transition
State2
Statechart diagrams, cont.
init()
Initialized
start() stop()
stop()
Started Stopped
start()
destroy()
Destroyed
Example 12.7
The TicTacToe Game
JApplet JPanel MouseListener
-char token -char token -char token
+getToken 1 9
+getToken +getToken
TicTacToe Cell
+setToken +setToken +setToken
+paintComponet +paintComponet +paintComponet
-whoseTurn: char -token: char
+mouseClicked +mouseClicked +mouseClicked
-cell Cell[][] = new Cell[3][3]
-JLabel jlblStatus: JLabel +getToken():char
+setToken(token: char): void
+init(): void +paintComponet(g: Graphics): void
+isFull(): boolean +mouseClicked(e: MouseEvent): void
+isWon(): boolean
Example 12.7, cont.
CardLayout
GridBagLayout
Using No Layout Manager
Event Adapters (Optional)
Event Adapters (Optional)
Standard adapters
Anonymous adapters
CardLayout (Optional from here on)
ShowCardLayout Run
GridBagLayout
The GridBagLayout manager is the most
flexible and the most complex. It is similar to the
GridLayout manager in the sense that both
layout managers arrange components in a grid.
The components can vary in size, however, and
can be added in any order in GridBagLayout.
Example 12.11: Using
GridBagLayout Manager
ShowGridBagLayout Run
Using No Layout Manager
You can place components in a container
without using any layout manager. In this
case, the component must be placed using
the component’s instance method
setBounds().
Example 12.12: Using No
Layout Manager
This example
shows a program
that places the same
components in the
same layout as in
the preceding
example, but
without using a
layout manager.
ShowNoLayout Run