Académique Documents
Professionnel Documents
Culture Documents
java.lang.Class class
The java.lang.Class class performs
mainly two tasks:provides methods
to get the metadata of a class at
runtime.
provides methods to examine and
change the runtime behaviour of a
class.
Method
Description
3) public Object
creates new instance.
newInstance()throws
InstantiationException,IllegalAcce
ssException
4) public boolean isInterface()
checks if it is interface.
checks if it is array.
checks if it is primitive.
8) public Field[]
getDeclaredFields()throws
SecurityException
9) public Method[]
getDeclaredMethods()throws
SecurityException
public Constructor[]
getDeclaredConstructors()throws
SecurityException
public Method[]
getDeclaredMethods()throws
SecurityException
10
11
newInstance() method
ThenewInstance()method
ofClassclass andConstructorclass is
used to create a new instance of the
class.
The newInstance() method of Class
class can invoke zero-argument
constructor whereas newInstance()
method of Constructor class can invoke
any number of arguments. So
Constructor class is preferred over
Class class.
Applet,AWT and JavaSwing Tutorial
12
13
What is an applet?
An applet is a small Java program
that is embedded and ran in some
other Java interpreter program such
as
a Java technology-enabled browser
Suns applet viewer program called
appletviewer
14
15
16
Introduction
Applet runs in the browser and its
lifecycle method are called by JVM when
it is loaded and destroyed. Here are the
lifecycle methods of an Applet:
init(): This method is called to
initialized an applet
start(): This method is called after
the initialization of the applet.
stop(): This method can be called
multiple times in the life cycle of an
Applet.
Applet,AWT and JavaSwing
Tutorial
17
18
19
20
21
22
Introduction
Applet isjava programthat can be
embedded intoHTML pages. Java
applets runs on the java enables web
browsers such as mozila and internet
explorer. Applet is designed to run
remotely on the client browser, so
there are some restrictions on it.
Applet can't access system resources
on the local computer. Applets are
used to make the web site more
dynamic and entertaining.
Applet,AWT and JavaSwing
Tutorial
23
Advantages of Applet:
Applets are cross platform and can run on
Windows, Mac OS and Linux platform
Applets can work allthe version of Java Plugin
Appletsruns in a sandbox, so the user does
not need to trust the code, so it can work
without security approval
Applets are supported by most web browsers
Applets are cached in most web browsers, so
will be quick to load when returning to a web
page
User can also have full access to the machine
if user allows
Applet,AWT and JavaSwing
Tutorial
24
25
Applet Vs Application
Applets as previously described,
are the small programs while
applications
are larger programs. Applets
don't have the main method
while in an application execution
starts with the main method.
Applets canrun in our browser's
window or in an appletviewer
Applet,AWT and JavaSwing
Tutorial
26
27
28
29
30
31
One more thing to point here is that
applets are unable to use the native
methods,run any program on the
user system or load shared libraries.
The majorsecurityconcern here is
that the local shared libraries and the
native methods may results in the
loophole in the java security model.
32
33
34
35
36
37
Attribute
Explaination
Example
Code
Code=applet0.class
Width
Width of applet
Width=300
Height
Height of Applet
Height=300
Code base
Applets Directory
Codebase=/applet
Alt
Name
Name=AppletExam
Align=right
38
History of an Applet
Edit java source code & html
notepad Hello.java
notepad Hello.html
Compile source to ByteCodes
javacHello.java
produces Hello.class
View applet (Java Virtual Machine)
appletviewerHello.html
browser Hello.html
Applet,AWT and JavaSwing
Tutorial
39
<html>
<body>
<applet code=Hello.class
<width=300height=300>
</applet>
</body>
</html>
Save as Hello.html
40
import java.applet.Applet;
import java.awt.*;
public class Hello extends Applet{
public void init() {
repaint();
}
public void paint(Graphics g) {
g.drawString(Hello World!,30,30);
}
}
Save as Hello.java
Applet,AWT and JavaSwing
Tutorial
41
History of an Applet
Edit java source code & html
notepad Hello.java
notepad Hello.html
Compile source to ByteCodes
javacHello.java
produces Hello.class
View applet (Java Virtual Machine)
appletviewerHello.html
browser Hello.html
Applet,AWT and JavaSwing
Tutorial
42
Applet Security
For security reasons, applets that are
loaded over the network have
severalrestrictions.
an appletcannotordinarily reador
writefiles on the computer that
it's executing on.
an applet cannotmakenetwork
connectionsexcept to the
hostthat it came from.
Applet,AWT and JavaSwing
Tutorial
43
44
45
46
47
Event Handling
Examining of Event Classes and
Interfaces
Events are Supported by
java.awt.event package
Event
Source
Listener
Applet,AWT and JavaSwing
Tutorial
48
Event Classes
MouseWheelevent
ActionEvent TextEvent
AdjustmentEvent WindowEvent
ComponentEvent
ContainerEvent
FocusEvent
InputEvent
ItemEvent
KeyEvent
MouseEvent
Applet,AWT and JavaSwing
Tutorial
49
Source of Events
Button
CheckBox
Choice
List
MenuItem
ScrollBar
TextComponents
Window
Applet,AWT and JavaSwing
Tutorial
50
EventListener Interface
ActionListener
AdjustmentListener
ComponentListener
ContainerListener
FocusListener
ItemListener
KeyListener
MouseListener
MouseMotionListener
Applet,AWT and JavaSwing
Tutorial
51
MouseWheelListener
TextListener
WindowListener
ActionListener Interface
void actionPerformed(ActionEvent ae)
AdjustmentListener interface
Void
adjustmentValueChanged(AdjustmentEv
ent ae)
Applet,AWT and JavaSwing
Tutorial
52
ComponentListener Interface
Void componentResized(ComponentEvent ce)
Void componentMoved(ComponentEvent ce)
Void componentShown(ComponentEvent ce)
Void componentHidden(ComponentEvent ce)
ContainerListener Interface
Void componentAdded(ContainerEvent ce)
Void componentRemoved(ContainerEvent ce)
53
FocusListenerEvent Interface
Void FicusGained(FocusEvent fe)
Void focusLost(FocusEvent fe)
ItemListener Interface
Void ItemStateChanged (ItemEvent ie)
KeyListener Interface
Applet,AWT and JavaSwing
Tutorial
54
55
MouseMotionListener Interface
Void mouseDragged(MouseEvent me)
Void mouseMoved(MouseEvent me)
TextListenerInterface
Void textChanged(TextEvent te)
WindowListener Interface
Void windowActivated(WindoEvent we)
Void windowClosed(WindoEvent we)
Applet,AWT and JavaSwing
Tutorial
56
57
58
59
60
61
Font Matrices
Methods
Int charWidth(int c)
Int cahrWidth(char c)
Int getAscent()
Int getDescent()
Font getFont()
Int getHeight()
Int stringWidth(String str)
Applet,AWT and JavaSwing
Tutorial
62
63
Layout Managers
FlowLayout()
BorderLayout()
GridLayout()
CardLayout()
Menu()
MenuItem()
64
Java Swing
Japplet
Jbutton
JCheckbox
Jcombobox
Jlabel
Jradiobutton
Jscrollpane
Jtable
JTextField
JTree
Applet,AWT and JavaSwing
Tutorial
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127