Chapter 1 Awt (Updated) (1) COMPLETE CHAPTER
Chapter 1 Awt (Updated) (1) COMPLETE CHAPTER
Prepared by
Prof.Suwarna Thakre
INTRODUCTION
• AWT is an API to develop GUI or window based
applications in Java
Buttons
Check boxes
Choice
Lists
Menu
cbGroup)
CheckboxGroup cbGroup)
/*<applet code=choice1.class
width=200 height=200>
</applet>*/
public class choice1 extends Applet
{
public void init()
{
Choice c1=new Choice();
c1.add("Win/xp");
c1.add("win/nt");
c1.add("win/2000");
add(c1);
}
}
LIST CLASS
PART-III
AWT Compontes---TextField,TextArea
Prepared by
Prof.Suwarna Thakre
TextField
• The TextField class implements a single-line text-entry
area, usually called an edit control.
• Text fields allow the user to enter strings and to edit the
text using the arrow keys, cut and paste keys, and mouse
selections.
• TextField is a subclass of TextComponent.
Constructors of TextField
• TextField( ):- creates a default text field
/*<applet code=textfield1.class
width=200 height=200>
</applet>*/
public class textfield1 extends Applet
{
public void init()
{setLayout(new FlowLayout());
TextField c1=new TextField();
TextField c2=new TextField(20);
TextField c3=new TextField("information");
TextField c4=new TextField(" info",30);
add(c1);add(c2);add(c3);add(c4);
}
}
Program Using TextField
import java.awt.*;
import java.applet.*;
/*<applet code=textfield2.class
width=200 height=200>
</applet>*/
public class textfield2 extends Applet
{
public void init()
{
Label l=new Label("enter name");
Label l1=new Label("enter password");
TextField c1=new TextField(20);
TextField c2=new TextField(20);
c2.setEchoChar('*');
add(l);add(c1);add(l1);add(c2);
}
}
TextArea
Sometimes a single line of text input is not enough for a
given task. To handle these situations, the AWT includes a
simple multiline editor called TextArea. Following are
the constructors for TextArea:
• TextArea( )
• TextArea(int numLines, int numChars)
• TextArea(String str)
• TextArea(String str, int numLines, int
numChars)
Constructor Of TextArea
• TextArea(String str, int numLines, int
numChars, int sBars)
• Here, numLines specifies the height, in lines, of the text
area, and numChars specifies its width, in characters.
Initial text can be specified by str.
• In the fifth form we can specify the scroll bars that we
want the control to have.
• TextArea is a subclass of TextComponent
Scroll Bar Constants
sBars must be one of these values:
• SCROLLBARS_BOTH
• SCROLLBARS_NONE
• SCROLLBARS_HORIZONTAL_ONLY
SCROLLBARS_VERTICAL_ONLY
Methods Of TextArea
/*<applet code=textarea.class
width=200 height=200>
</applet>*/
public class textarea extends Applet
{
public void init()
{
String str="The TextArea control in AWT provide us multiline editor area";
TextArea ta3=new TextArea(str);
ta3.setBounds(20,30,80,120);
ta3.append(" we can append data");
ta3.insert(" is powerful",13);
add(ta3);
}
}
Menu Bars and Menus