AWT
AWT
awt PACKAGE
The Abstract Window Toolkit (AWT) contains
numerous classes and methods that allow you to
create and manage windows
CONSTRUCTORS:
• Frame( )
• Frame(String title) // title of the window
METHODS:
• void setSize(int Width, int Height)
• void setVisible(boolean visibleFlag)
• void setTitle (String newTitle)
FRAMES
When working with Frame objects, there are basically three
steps involved to get a Frame window to appear on the
screen:
Component add(Component c)
• Constructors:
Label( )
Label(String str)
Label(String str, int how)
String getText()
int getAlignment()
import java.awt.*; LABELS
class LabelDemo extends Frame
{
LabelDemo()
{
super("LabelDemo");
setLayout(new FlowLayout());
setSize(200,200);
Label one = new Label("One");
Label two = new Label("Two");
Label three = new Label("Three");
add(one);
add(two);
add(three);
setVisible(true);
}
}
class Labl LABELS
{
CONSTRUCTORS
Button( )
Button(String str)
METHODS
void setLabel(String str)
String getLabel( )
BUTTONS
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ButtonDemo" width=250 height=150>
</applet>
*/
public class ButtonDemo extends Applet implements ActionListener {
String msg = "";
Button yes, no, maybe;
public void init() {
yes = new Button("Yes");
no = new Button("No");
maybe = new Button("Undecided");
add(yes);
add(no);
add(maybe);
BUTTONS
yes.addActionListener(this);
no.addActionListener(this);
maybe.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if(str.equals("Yes")) {
msg = "You pressed Yes.";
}
else if(str.equals("No")) {
msg = "You pressed No.";
}
else {
msg = "You pressed Undecided.";
}
repaint();
}
BUTTONS
public void paint(Graphics g)
{
g.drawString(msg, 6, 100);
}
}
CHECK BOXES
• A check box is a control that is used to turn an option on or
off.
• CONSTRUCTORS
Checkbox( )
Checkbox(String str)
Checkbox(String str, boolean on)
Checkbox(String str, boolean on, CheckboxGroup cbGroup)
CHECK BOXES
METHODS
boolean getState( )
void setState(boolean on)
String getLabel( )
void setLabel(String str)
CHECK BOXES:EXAMPLE
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="CheckBoxDemo" width=250 height=300>
</applet>
*/
public class CheckBoxDemo extends Applet implements ItemListener
{
String msg = "";
Checkbox Win98, winNT, Win7;
public void init() {
Win98 = new Checkbox("Windows 98/XP", null, true);
winNT = new Checkbox("Windows NT/2000");
Win7 = new Checkbox("Windows 7");
add(Win98);
add(winNT);
add(Win7);
CHECK BOXES:EXAMPLE
Win98.addItemListener(this);
winNT.addItemListener(this);
Win7.addItemListener(this);
} //end of init()
public void itemStateChanged(ItemEvent ie) {
repaint();
}
public void paint(Graphics g) {
msg = "Current state: ";
g.drawString(msg, 6, 80);
msg = " Windows 98/XP: " + Win98.getState();
g.drawString(msg, 6, 100);
msg = " Windows NT/2000: " + winNT.getState();
g.drawString(msg, 6, 120);
msg = " Windows 7: " + Win7.getState();
g.drawString(msg, 6, 140);
} //end of paint()
}
CHECK BOXES
CHECK BOX GROUP
• It is possible to create a set of mutually exclusive check boxes in
which one and only one check box in the group can be checked at
any one time.
CONSTRUCTOR
CheckboxGroup()
METHODS
Checkbox getSelectedCheckbox( )
void setSelectedCheckbox(Checkbox which)
CHECK BOX GROUP: EXAMPLE
public class CBGroup extends Applet implements ItemListener
{
String msg = "";
Checkbox Win98, winNT, Win7;
CheckboxGroup cbg;
public void init() {
cbg = new CheckboxGroup();
Win98 = new Checkbox("Windows 98/XP", cbg, true);
winNT = new Checkbox("Windows NT/2000",cbg,false);
Win7 = new Checkbox("Windows 7",cbg,true);
add(Win98);
add(winNT);
add(Win7);
Win98.addItemListener(this);
winNT.addItemListener(this);
Win7.addItemListener(this);
} //end of init()
CHECK BOX GROUP: EXAMPLE
public void itemStateChanged(ItemEvent ie) {
repaint();
}
public void paint(Graphics g) {
msg = "Current selection: ";
msg += cbg.getSelectedCheckbox().getLabel();
g.drawString(msg, 6, 100);
}
}
CHECK BOX GROUP: EXAMPLE
LIST
• The List class provides a compact, multiple-choice,
scrolling selection list.
CONSTRUCTORS
• String getSelectedItem( )
• int getSelectedIndex( )
• int getItemCount( )
• void select(int index)
• String getItem(int index)
CONSTRUCTORS
TextField( )
TextField(int numChars) //(width)
TextField(String str)
TextField(String str, int numChars)
TEXTFIELD
METHODS
String getText( )
void setText(String str)
String getSelectedText( )
void select(int startIndex, int endIndex)
boolean isEditable( )
void setEditable(boolean canEdit)
CONSTRUCTORS
TextArea( )
TextArea(int numLines, int numChars)
TextArea(String str)
TextArea(String str, int numLines, int numChars)
TextArea(String str, int numLines, int numChars, int sBars)
SBAR VALUES:
• SCROLLBARS_BOTH
• SCROLLBARS_NONE
• SCROLLBARS_HORIZONTAL_ONLY
• SCROLLBARS_VERTICAL_ONLY
TEXTAREA
METHODS
EXAMPLE
import java.awt.*;
import java.applet.*;
/*
<applet code="TextAreaDemo" width=500 height=350>
</applet>
*/
TEXTAREA
public class TextAreaDemo extends Applet {
public void init()
{
String val = "There are two ways of constructing " +
"a software design.\n" +
"One way is to make it so simple\n" +
"that there are obviously no deficiencies.\n" +
"And the other way is to make it so complicated\n" +
"that there are no obvious deficiencies.\n\n" +
" -C.A.R. Hoare\n\n" +
"There's an old story about the person who wished\n" +
"his computer were as easy to use as his telephone.\n" +
"That wish has come true,\n" +
"since I no longer know how to use my telephone.\n\n" +
" -Bjarne Stroustrup, AT&T, (inventor of C++)";
TextArea text = new TextArea(val, 10,
25,TextArea.SCROLLBARS_BOTH);
add(text);
}
}
TEXTAREA
Menu Bars and Menu
• Menu is a common feature in many programs which allows the
program to fit many commands in a hierarchical structure
without taking much space on the screen
• MenuBar( )
CONSTRUCTORS- MENU
Menu( )
Menu(String optionName) // name of menu
Menu(String optionName, boolean removable)
CONSTRUCTORS:
• CheckboxMenuItem( )
• CheckboxMenuItem(String itemName)
• CheckboxMenuItem(String itemName, boolean on)
METHODS:
boolean getState( )
void setState(boolean checked)
EVENT HANDLING WITH MENUS
Menus only generate events when an item of type MenuItem
or CheckboxMenuItem is selected.
AdapterMouseDemo adapterDemo;
• BorderLayout
• GridLayout
• CardLayout
FLOWLAYOUT
• FlowLayout is the default layout manager.
CONSTRUCTORS
FlowLayout( )
FlowLayout(int how )
FlowLayout(int how , int horz , int vert )
• The four sides are referred to as north, south, east, and west.
The middle area is called the center.
CONSTRCUTORS
BorderLayout( )
BorderLayout(int horz , int vert )
BORDERLAYOUT
When a component is added, you pass in an int to the add()
method that denotes the region of the container in which the
component is to be added
CONSTRUCTORS
GridLayout( )
GridLayout(int numRows, int numColumns )
GridLayout(int numRows, int numColumns , int
horz , int vert )
GridLayout
Only one component can be added to each region of the
grid.
Each region of the grid will have the same size. When
the container is resized, each region of the grid will be
resized accordingly.
CONSTRUCTORS
CardLayout( )
CardLayout(int horz , int vert )
CardLayout
• The cards are typically held in an object of type Panel.
• The cards that form the deck are also typically objects of
type Panel.
• Thus, you must create a panel that contains the deck and
a panel for each card in the deck.
CardLayout
• Next, you add to the appropriate panel the components
that form each card.
if (e.getSource() == second)
ourLayout.show(cardPanel, "Second");
if (e.getSource() == third)
ourLayout.show(cardPanel, "Third");
}
}
CardLayout
CardLayout
CardLayout