Académique Documents
Professionnel Documents
Culture Documents
Introduction
Developed by Sun Microsystems.
Java – an island in Indonesia where a lot
of coffee is grown.
Designed with syntax similar to C++
syntax to help programmers in C++
migrate fast to Java.
Points about Java
Automatic conversion:
byte->short->int->long->float->double
char
char and boolean are not convertible to each
other via casting.
Casting required when destination class is
smaller than the source class.
Example int k=(double)3.14;
Class and Object
Java is an object oriented language.
What is an object ?
What is a class? Class
Ball
Example:
greenBall instanceof Ball : returns true/false
object
class
String
int numbers[10];
int numbers[]={1,2,3,4};
switch(expression){
[case value1:
statement
1 to n times
break;]
[deafult: default statement;]
}
Loops
while (condition) { statements }
for(initialization;condition;iteration){}
break
continue
Constructors and methods
new Student(“John”,”123”)
Constructors
Specialization Generalization
Class
football
objects objects
Subclass Class
Subclass Class Subclass Class
Java inheritance
In java a class can inherit only from one class.
keyword: extends
All the methods available in super class is
available to subclass. A subclass can redefine
them if required. When a subclass redefines a
method inherited from super class, it is said to
have overridden that method.
Subclass cannot redefine a method to have
lesser access privilege than what was
originally defined. That is, if a method is
defined as public in super class, it cannot be
defined as protected or anything lower than
that in subclass.
Casting: A subclass can be converted to its
parent class without explicit casting. But for a
super class to be converted to subclass
explicit casting is required.
Polymorphism – dynamic binding
Window Panel
java.awt package
Creating windows for a stand-
alone application
Frame class is used to create window for a
stand-alone application.
Constructors and methods:
Frame()
Frame(String title)
void setSize(int width, int height)
void setVisible(boolean flag)
void setTitle(String title)
void setMenuBar(MenuBar mb)
void removeAll()
void remove(Component c)
Component add(Component comp)
public void setResizable(boolean resizable)
Frame layout
public void add(Component comp, Object constraints)
constraints are : BorderLayout.NORTH,
BorderLayout.SOUTH, BorderLayout.EAST,
BorderLayout.WEST, and
BorderLayout.CENTER.
We shall deal with BorderLayout class later.
public void validate()
AWT uses validate to cause a container to lay
out its subcomponents again after the
components it contains have been added to
or modified.
public void setBackground(Color c)
Example: setBackground(Color.RED);
Color
static Color black , static Color BLACK
static Color blue , static Color BLUE
static Color cyan , static Color CYAN
static Color DARK_GRAY ,static Color darkGray
static Color gray , static Color GRAY
static Color green ,static Color GREEN
static Color LIGHT_GRAY, static Color lightGray
static Color magenta ,static Color MAGENTA
static Color orange ,static Color ORANGE
static Color pink , static Color PINK
static Color red , static Color RED
static Color white ,static Color WHITE
static Color yellow ,static Color YELLOW
Alignment:BOTTOM_ALIGNMENT, CENTER_ALIGNMENT,
LEFT_ALIGNMENT, RIGHT_ALIGNMENT,
TOP_ALIGNMENT
Example
import java.awt.*;
public class MyFrame extends Frame{
public MyFrame(){
super("New Title");
setSize(300,300);
setVisible(true);
Panel p= new Panel();
p.add(new Label("Name"));
p.add(new Label("ID"));
add(p,BorderLayout.CENTER);
validate(); }
public static void main(String str[]){
new MyFrame();
}}
TextField
A TextField object is a text component
that allows for the editing of a single line
of text.
Constructors :
TextField()
TextField(int columns)
TextField(String text)
TextField(String text, int columns)
TextFields methods
void isEditable(boolean edit)
boolean isEditable()
void setEchoChar(char ch)
char getEchoChar()
String getText()
void setText(String t)
import java.awt.*;
public class MyFrame extends Frame{
public MyFrame(){
super("New Title");
setSize(200,100);
setResizable(false);
setVisible(true);
Panel p= new Panel();
p.add(new Label("Login"));
p.add(new TextField(10));
p.add(new Label("Password"));
TextField password= new TextField(10);
password.setEchoChar('*');
p.add(password);
add(p,BorderLayout.CENTER);
validate();}
public static void main(String str[]){
new MyFrame();}}
TextArea
A TextArea object is a multi-line region that
displays text. It can be set to allow editing or to
be read-only.
Constructors:
TextArea()
TextArea(int rows, int columns)
TextArea(String text)
TextArea(String text, int rows, int columns)
TextArea(String text, int rows, int columns,
int scrollbars)
TextArea scrollbars values
SCROLLBARS_BOTH,
SCROLLBARS_VERTICAL_ONLY,
SCROLLBARS_HORIZONTAL_ONLY,
SCROLLBARS_NONE.
Methods:
void isEditable(boolean edit)
boolean isEditable()
public void append(String str)
String getText()
void setText(String t)
public void setColumns(int columns)
public void setRows(int rows)
import java.awt.*;
public class MyFrame extends Frame{
public MyFrame(){
super("Text Area Demo");
setSize(500,200);
setResizable(false);
setVisible(true);
Panel p= new Panel();
String str="An EJB module is used to assemble one or more
enterprise beans into a single deployable unit.\n An EJB
module is stored in a standard Java archive (JAR) file.\n
An EJB module contains the following:\n One or more
deployable enterprise beans.\n deployment descriptors ";
TextArea ta= new TextArea(5,50);
ta.setText(str);
p.add(ta);
add(p,BorderLayout.CENTER);
validate();}
public static void main(String str[]){
new MyFrame();}
}
Check Boxes
Constructor & Methods
Checkbox()
Checkbox(String label)
Checkbox(String label, boolean state)
public void setState(boolean state)
public boolean getState()
public void setCheckboxGroup(CheckboxGroup g)
import java.awt.*;
public class MyFrame extends Frame{
public MyFrame(){
super("Checkbox Area Demo");
setSize(200,100);
setResizable(false);
setVisible(true);
Panel p= new Panel();
p.add(new Checkbox("Red"));
p.add(new Checkbox("Blue"));
p.add(new Checkbox("Green"));
add(p,BorderLayout.CENTER);
validate();}
public static void main(String str[]){
new MyFrame();}
}
Radio buttons
The constructors of Checkbox class with
CheckboxGroup class as argument are
used to achieve the radio button
controls.
Checkbox(String label, boolean state,
CheckboxGroup group)
Checkbox(String label, CheckboxGroup group,
boolean state)
CheckboxGroup
The CheckboxGroup class is used to
group together a set of Checkbox buttons.
Constructor:
public CheckboxGroup()
import java.awt.*;
public class MyFrame extends Frame{
public MyFrame(){
super("Checkbox Area Demo");
setSize(200,100);
setResizable(false);
setVisible(true);
Panel p= new Panel();
List()
List(int rows)
List(int rows,boolean multipleMode)
Methods:
public void add(String item)
public String getItem(int index)
public int getItemCount()
public int[] getSelectedIndexes()
public String[] getSelectedItems()
public void remove(int position)
public void remove(String item)
public void removeAll()
public void select(int pos)
import java.awt.*;
public class MyFrame extends Frame{
public MyFrame(){
super("List Area Demo");
setSize(200,100);
setResizable(false);
setVisible(true);
Panel p= new Panel();
List lst = new List(4, true);
lst.add(“Sun");
lst.add(“Moon");
lst.add("Earth");
lst.add(“Star");
p.add(lst);
add(p,BorderLayout.CENTER);
validate();}
public static void main(String str[]){
new MyFrame();}}
Button
Constructors and methods
Button()
Button(String label)
public void setActionCommand(String command)
Sets the command name for the action event
fired by this button. By default this action
command is set to match the label of the
button.
mb.add(edit);
}
public static void main(String str[]){
new MyFrame();}}
Event Handling
The Delegation Event Model
Generates events
Event
object
Listener (Window)
Sends event
object