Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Java AWT
Java AWT
• Java AWT (Abstract Window Toolkit) is an API to develop Graphical
User Interface (GUI) or windows-based applications in Java.
• The java.awt package provides classes for AWT API such as Text
Field, Label, TextArea, RadioButton, CheckBox, Choice, List etc.
Java AWT Hierarchy
Components
• All the elements like the button, text fields, scroll bars, etc. are called
components.
• In Java AWT, there are classes for each component as shown in above
diagram.
• In order to place every component in a particular position on a screen,
we need to add them to a container.
Container
• The Container is a component in AWT that can contain another
components like buttons, textfields, labels etc.
• The classes that extends Container class are known as container such
as Frame, Dialog and Panel.
Types of containers
• There are four types of containers in Java AWT:
1. Window
2. Panel
3. Frame
4. Dialog
Window
• The window is the container that have no borders and menu bars.
• You must use frame, dialog for creating a window.
• We need to create an object of Window class to create this container.
Panel
• The Panel is the container that doesn't contain title bar, border or menu
bar.
• It can have other components like button, text field etc.
• An object of Panel class creates a container, in which we can add
components.
Frame
• The Frame is the container that contain title bar and border and can
have menu bars.
• It can have other components like button, text field, scrollbar etc.
Useful Methods of Component Class
Method Description
public void add(Component c) Inserts a component on this component.
public void setSize(int width,int height) Sets the size (width and height) of the
component.
public void setLayout(LayoutManager m) Defines the layout manager for the component.
public void setVisible(boolean status) Changes the visibility of the component, by
default false.
Java AWT Button
• The Button class is used to create a button.
Button Class Constructors
Sr. no. Constructor Description
1. Button( ) It constructs a new button
with an empty string i.e. it
has no label.
2. Button (String text) It constructs a new button
with given string as its label.
Button Class Methods
Sr. no. Method Description
1. void setText (String text) It sets the string message
on the button
2. String getText() It fetches the String
message on the button.
Example
import java.awt.*;
class ButtonExample
{
public static void main (String a[])
{
Frame f = new Frame("Button Example");
Button b = new Button("Click Here");
f.setLayout(null);
b.setBounds(50,100,80,30);
f.add(b);
f.setSize(400,400);
f.setVisible(true);
}
}
Java AWT Label
• The Label class is used to create a Label.
• It is used to display a single line of read only text.
Label class Constructors
Sr. no. Constructor Description
1. Label() It constructs an empty label.
2. Label(String text) It constructs a label with the
given string.
Label Class Methods
Sr. no. Method name Description
1. void setText(String text) It sets the texts for label
with the specified text.
2. String getText() It gets the text of the label
Example
import java.awt.*;
class LabelExample
{
public static void main (String a[])
{
Frame f = new Frame("Button Example");
Label l = new Label("User Name");
l.setBounds(50,100,80,30);
f.setLayout(null);
f.add(l);
f.setSize(400,400);
f.setVisible(true);
}
}
TextField
• TextField allows a user to enter a single line text and edit it.
• It inherits TextComponent class
Constructors of TextField Class
Sr. no. Constructor Description
1. TextField() It constructs a new text field
component.
2. TextField(String text) It constructs a new text field
initialized with the given
string text to be displayed.
Methods of TextField Class
1. void setText(String t): It sets the text presented to the specified text.
2. String getText(): It gets the text from the text field.
3. void setEchoChar(char c): It sets the echo character for text field.
Example
import java.awt.*;
class TextFieldExample
{
public static void main (String a[])
{
Frame f = new Frame("TextField Example");
TextField t1 = new TextField ();
TextField t2 = new TextField ("User name");
f.setLayout(null);
t1.setBounds(100, 150, 100, 50);
t2.setBounds(100, 250, 100, 50);
f.add(t1);
f.add(t2);
f.setSize(400,400);
f.setVisible(true);
}
}
TextArea
• TextArea allows a user to enter a multiple line text and edit it.
• It inherits TextComponent class
Constructors of TextArea Class
Sr. no. Constructor Description
1. TextArea() It constructs a new and
empty text area with no text
in it.
2. TextArea(String text) It constructs a new text area
and displays the specified
text in it.
Methods of TextArea Class
1. void setText(String t): It sets the text presented to the specified text.
2. String getText(): It gets the text from the text area.
Example
import java.awt.*;
class TextAreaExample
{
public static void main (String a[])
{
Frame f = new Frame("TextArea Example");
TextArea t1 = new TextArea();
TextArea t2 = new TextArea("User name");
f.setLayout(null);
t1.setBounds(100, 150, 100, 50);
t2.setBounds(100, 250, 100, 50);
f.add(t1);
f.add(t2);
f.setSize(400,400);
f.setVisible(true);
}
}
Java AWT Checkbox
• The Checkbox class is used to create a checkbox.
• It is used to turn an option on (true) or off (false).
Constructors
Sr. no. Constructor Description
1. Checkbox() It constructs a checkbox with
no string as the label.
2. Checkbox(String label) It constructs a checkbox with
the given label.
3. Checkbox(String label, boolean state) It constructs a checkbox with
the given label and sets the
given state.
Methods
• void setLabel(String label): It sets the checkbox's label to the string
argument.
• String getLabel(): It gets the label of checkbox.
Example
import java.awt.*;
class CheckboxExample
{
public static void main (String a[])
{
Frame f = new Frame("Checkbox Example");
Checkbox c1 = new Checkbox("C++");
Checkbox c2 = new Checkbox("Java", true);
f.setLayout(null);
c1.setBounds(100, 100, 50, 50);
c2.setBounds(100, 180, 50, 50);
f.add(c1);
f.add(c2);
f.setSize(400,400);
f.setVisible(true);
}
}
Java AWT CheckboxGroup
• The object of CheckboxGroup class is used to group together a set
of Checkbox.
• At a time only one check box button is allowed to be in "on" state and
remaining check box button in "off" state.
• It inherits the object class.
Cont…
• Note: CheckboxGroup enables you to create radio buttons in AWT. There is
no special control for creating radio buttons in AWT.
Example
import java.awt.*;
class CheckboxGroupExample
{
public static void main (String a[])
{
Frame f = new Frame("CheckboxGroupExample");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox c1 = new Checkbox("C++", cbg, true);
Checkbox c2 = new Checkbox("Java", cbg, false);
f.setLayout(null);
c1.setBounds(100,100, 50,50);
c2.setBounds(100,180, 50,50);
f.add(c1);
f.add(c2);
f.setSize(400,400);
f.setVisible(true);
}
}
Java AWT Choice
• The object of Choice class is used to show popup menu of choices.
• Choice selected by user is shown on the top of a menu.
• Choice class extends Component implements ItemSelectable,
Accessible
Constructor
Sr. no. Constructor Description
1. Choice() It constructs a new choice
menu.
Method
• void add(String item): It adds an item to the choice menu.
• String getItem(int index): It gets the item (string) at the given index
position in the choice menu.
Example
import java.awt.*;
class ChoiceExample
{
public static void main (String a[])
{
Frame f = new Frame("ChoiceExample");
Choice c = new Choice();
c.add("Item 1");
c.add("Item 2");
c.add("Item 3");
c.add("Item 4");
f.setLayout(null);
c.setBounds(100, 100, 75, 75);
f.add(c);
f.setSize(400,400);
f.setVisible(true);
}
Java AWT List
• The object of List class represents a list of text items.
• With the help of the List class, user can choose either one item or
multiple items.
• List class extends Component class and implements ItemSelectable,
Accessible interface.
Constructors
Sr. no. Constructor Description
1. List() It constructs a
new scrolling list.
Method
Sr. no. Method name Description
1. void add(String item) It adds the specified item into the
end of scrolling list.
2. void add(String item, int index) It adds the specified item into list
at the given index position.
Example
import java.awt.*;
class ListExample
{
public static void main (String a[])
{
Frame f = new Frame("ListExample");
List l1 = new List();
l1.add("Item 1");
l1.add("Item 2");
l1.add("Item 3");
l1.add("Item 4");
l1.add("Item 5");
f.setLayout(null);
l1.setBounds(100, 100, 75, 75);
f.add(l1);
f.setSize(400,400);
f.setVisible(true);
}
}
Java AWT Scrollbar
• The object of Scrollbar class is used to add horizontal and vertical
scrollbar.
• Scrollbar is a GUI component allows us to see invisible number of
rows and columns.
• Scrollbar class extends Component class and implements Adjustable,
Accessible interface.
Scrollbar Class Fields
• public static final int HORIZONTAL = 0 - It is a constant to
indicate a horizontal scroll bar.
• public static final int VERTICAL = 1 - It is a constant to indicate a
vertical scroll bar.
Constructors
Sr. no. Constructor Description
1 Scrollbar() Constructs a new vertical scroll bar.
2 Scrollbar(int orientation) Constructs a new scroll bar with the
specified orientation.
Where orientation specified whether the scrollbar will be horizontal or vertical.
Method
• void setOrientation(int orientation): It sets the orientation for the scroll
bar.
• int getOrientation(): It returns the orientation of scroll bar.
Example
import java.awt.*;
class ScrollbarExample
{
public static void main (String a[])
{
Frame f = new Frame("ScrollbarExample");
Scrollbar s = new Scrollbar();
f.setLayout(null);
s.setBounds(100, 100, 50, 100);
f.add(s);
f.setSize(400,400);
f.setVisible(true);
}
}
MenuBar
• The MenuBar class provides menu bar bound to a frame.
• MenuBar class extends MenuComponent class and implements
MenuContainer, Accessible interface.
Constructor
• MenuBar(): Creates a new menu bar.
Method
• Menu add(Menu m): Adds the specified menu to the menu bar.
• Menu getMenu(int i): Gets the specified menu.
Menu
• A menu has a pull-down list of menu items from which the user can
select one at a time.
• When many options in multiple categories exist for selection by the
user, menus are the best choice since they take less space on the frame.
• Menu class extends MenuItem class and implements MenuContainer,
Accessible interface.
Menu Hierarchy
• Menu creation involves many classes, like MenuBar, MenuItem
and Menu and one is added to the other.
Cont…
• A MenuBar is capable of holding the menus and a Menu can hold
menu items.
• Menus are placed on a menu bar.
Procedure for Creating Menus
• Create menu bar
• Add menu bar to the frame
• Create menus
• Add menus to menu bar
• Create menu items
• Add menu items to menus
• Event handling
Example
import java.awt.*;
class MenuExample
{
public static void main (String a[])
{
Frame f = new Frame("MenuExample");
f.setLayout(null);
MenuBar mb=new MenuBar();
Menu menu=new Menu("Menu");
MenuItem i1=new MenuItem("Item 1");
Menu submenu=new Menu("Sub Menu");
MenuItem i2=new MenuItem("Item 2");
submenu.add(i2);
menu.add(i1);
menu.add(submenu);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setVisible(true);
}
}

More Related Content

AWT New-3.pptx

  • 2. Java AWT • Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI) or windows-based applications in Java. • The java.awt package provides classes for AWT API such as Text Field, Label, TextArea, RadioButton, CheckBox, Choice, List etc.
  • 4. Components • All the elements like the button, text fields, scroll bars, etc. are called components. • In Java AWT, there are classes for each component as shown in above diagram. • In order to place every component in a particular position on a screen, we need to add them to a container.
  • 5. Container • The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. • The classes that extends Container class are known as container such as Frame, Dialog and Panel.
  • 6. Types of containers • There are four types of containers in Java AWT: 1. Window 2. Panel 3. Frame 4. Dialog
  • 7. Window • The window is the container that have no borders and menu bars. • You must use frame, dialog for creating a window. • We need to create an object of Window class to create this container.
  • 8. Panel • The Panel is the container that doesn't contain title bar, border or menu bar. • It can have other components like button, text field etc. • An object of Panel class creates a container, in which we can add components.
  • 9. Frame • The Frame is the container that contain title bar and border and can have menu bars. • It can have other components like button, text field, scrollbar etc.
  • 10. Useful Methods of Component Class Method Description public void add(Component c) Inserts a component on this component. public void setSize(int width,int height) Sets the size (width and height) of the component. public void setLayout(LayoutManager m) Defines the layout manager for the component. public void setVisible(boolean status) Changes the visibility of the component, by default false.
  • 11. Java AWT Button • The Button class is used to create a button.
  • 12. Button Class Constructors Sr. no. Constructor Description 1. Button( ) It constructs a new button with an empty string i.e. it has no label. 2. Button (String text) It constructs a new button with given string as its label.
  • 13. Button Class Methods Sr. no. Method Description 1. void setText (String text) It sets the string message on the button 2. String getText() It fetches the String message on the button.
  • 15. import java.awt.*; class ButtonExample { public static void main (String a[]) { Frame f = new Frame("Button Example"); Button b = new Button("Click Here"); f.setLayout(null); b.setBounds(50,100,80,30); f.add(b); f.setSize(400,400); f.setVisible(true); } }
  • 16. Java AWT Label • The Label class is used to create a Label. • It is used to display a single line of read only text.
  • 17. Label class Constructors Sr. no. Constructor Description 1. Label() It constructs an empty label. 2. Label(String text) It constructs a label with the given string.
  • 18. Label Class Methods Sr. no. Method name Description 1. void setText(String text) It sets the texts for label with the specified text. 2. String getText() It gets the text of the label
  • 20. import java.awt.*; class LabelExample { public static void main (String a[]) { Frame f = new Frame("Button Example"); Label l = new Label("User Name"); l.setBounds(50,100,80,30); f.setLayout(null); f.add(l); f.setSize(400,400); f.setVisible(true); } }
  • 21. TextField • TextField allows a user to enter a single line text and edit it. • It inherits TextComponent class
  • 22. Constructors of TextField Class Sr. no. Constructor Description 1. TextField() It constructs a new text field component. 2. TextField(String text) It constructs a new text field initialized with the given string text to be displayed.
  • 23. Methods of TextField Class 1. void setText(String t): It sets the text presented to the specified text. 2. String getText(): It gets the text from the text field. 3. void setEchoChar(char c): It sets the echo character for text field.
  • 25. import java.awt.*; class TextFieldExample { public static void main (String a[]) { Frame f = new Frame("TextField Example"); TextField t1 = new TextField (); TextField t2 = new TextField ("User name"); f.setLayout(null); t1.setBounds(100, 150, 100, 50); t2.setBounds(100, 250, 100, 50); f.add(t1); f.add(t2); f.setSize(400,400); f.setVisible(true); } }
  • 26. TextArea • TextArea allows a user to enter a multiple line text and edit it. • It inherits TextComponent class
  • 27. Constructors of TextArea Class Sr. no. Constructor Description 1. TextArea() It constructs a new and empty text area with no text in it. 2. TextArea(String text) It constructs a new text area and displays the specified text in it.
  • 28. Methods of TextArea Class 1. void setText(String t): It sets the text presented to the specified text. 2. String getText(): It gets the text from the text area.
  • 30. import java.awt.*; class TextAreaExample { public static void main (String a[]) { Frame f = new Frame("TextArea Example"); TextArea t1 = new TextArea(); TextArea t2 = new TextArea("User name"); f.setLayout(null); t1.setBounds(100, 150, 100, 50); t2.setBounds(100, 250, 100, 50); f.add(t1); f.add(t2); f.setSize(400,400); f.setVisible(true); } }
  • 31. Java AWT Checkbox • The Checkbox class is used to create a checkbox. • It is used to turn an option on (true) or off (false).
  • 32. Constructors Sr. no. Constructor Description 1. Checkbox() It constructs a checkbox with no string as the label. 2. Checkbox(String label) It constructs a checkbox with the given label. 3. Checkbox(String label, boolean state) It constructs a checkbox with the given label and sets the given state.
  • 33. Methods • void setLabel(String label): It sets the checkbox's label to the string argument. • String getLabel(): It gets the label of checkbox.
  • 35. import java.awt.*; class CheckboxExample { public static void main (String a[]) { Frame f = new Frame("Checkbox Example"); Checkbox c1 = new Checkbox("C++"); Checkbox c2 = new Checkbox("Java", true); f.setLayout(null); c1.setBounds(100, 100, 50, 50); c2.setBounds(100, 180, 50, 50); f.add(c1); f.add(c2); f.setSize(400,400); f.setVisible(true); } }
  • 36. Java AWT CheckboxGroup • The object of CheckboxGroup class is used to group together a set of Checkbox. • At a time only one check box button is allowed to be in "on" state and remaining check box button in "off" state. • It inherits the object class.
  • 37. Cont… • Note: CheckboxGroup enables you to create radio buttons in AWT. There is no special control for creating radio buttons in AWT.
  • 39. import java.awt.*; class CheckboxGroupExample { public static void main (String a[]) { Frame f = new Frame("CheckboxGroupExample"); CheckboxGroup cbg = new CheckboxGroup(); Checkbox c1 = new Checkbox("C++", cbg, true); Checkbox c2 = new Checkbox("Java", cbg, false); f.setLayout(null); c1.setBounds(100,100, 50,50); c2.setBounds(100,180, 50,50); f.add(c1); f.add(c2); f.setSize(400,400); f.setVisible(true); } }
  • 40. Java AWT Choice • The object of Choice class is used to show popup menu of choices. • Choice selected by user is shown on the top of a menu. • Choice class extends Component implements ItemSelectable, Accessible
  • 41. Constructor Sr. no. Constructor Description 1. Choice() It constructs a new choice menu.
  • 42. Method • void add(String item): It adds an item to the choice menu. • String getItem(int index): It gets the item (string) at the given index position in the choice menu.
  • 44. import java.awt.*; class ChoiceExample { public static void main (String a[]) { Frame f = new Frame("ChoiceExample"); Choice c = new Choice(); c.add("Item 1"); c.add("Item 2"); c.add("Item 3"); c.add("Item 4"); f.setLayout(null); c.setBounds(100, 100, 75, 75); f.add(c); f.setSize(400,400); f.setVisible(true); }
  • 45. Java AWT List • The object of List class represents a list of text items. • With the help of the List class, user can choose either one item or multiple items. • List class extends Component class and implements ItemSelectable, Accessible interface.
  • 46. Constructors Sr. no. Constructor Description 1. List() It constructs a new scrolling list.
  • 47. Method Sr. no. Method name Description 1. void add(String item) It adds the specified item into the end of scrolling list. 2. void add(String item, int index) It adds the specified item into list at the given index position.
  • 49. import java.awt.*; class ListExample { public static void main (String a[]) { Frame f = new Frame("ListExample"); List l1 = new List(); l1.add("Item 1"); l1.add("Item 2"); l1.add("Item 3"); l1.add("Item 4"); l1.add("Item 5"); f.setLayout(null); l1.setBounds(100, 100, 75, 75); f.add(l1); f.setSize(400,400); f.setVisible(true); } }
  • 50. Java AWT Scrollbar • The object of Scrollbar class is used to add horizontal and vertical scrollbar. • Scrollbar is a GUI component allows us to see invisible number of rows and columns. • Scrollbar class extends Component class and implements Adjustable, Accessible interface.
  • 51. Scrollbar Class Fields • public static final int HORIZONTAL = 0 - It is a constant to indicate a horizontal scroll bar. • public static final int VERTICAL = 1 - It is a constant to indicate a vertical scroll bar.
  • 52. Constructors Sr. no. Constructor Description 1 Scrollbar() Constructs a new vertical scroll bar. 2 Scrollbar(int orientation) Constructs a new scroll bar with the specified orientation. Where orientation specified whether the scrollbar will be horizontal or vertical.
  • 53. Method • void setOrientation(int orientation): It sets the orientation for the scroll bar. • int getOrientation(): It returns the orientation of scroll bar.
  • 55. import java.awt.*; class ScrollbarExample { public static void main (String a[]) { Frame f = new Frame("ScrollbarExample"); Scrollbar s = new Scrollbar(); f.setLayout(null); s.setBounds(100, 100, 50, 100); f.add(s); f.setSize(400,400); f.setVisible(true); } }
  • 56. MenuBar • The MenuBar class provides menu bar bound to a frame. • MenuBar class extends MenuComponent class and implements MenuContainer, Accessible interface.
  • 58. Method • Menu add(Menu m): Adds the specified menu to the menu bar. • Menu getMenu(int i): Gets the specified menu.
  • 59. Menu • A menu has a pull-down list of menu items from which the user can select one at a time. • When many options in multiple categories exist for selection by the user, menus are the best choice since they take less space on the frame. • Menu class extends MenuItem class and implements MenuContainer, Accessible interface.
  • 60. Menu Hierarchy • Menu creation involves many classes, like MenuBar, MenuItem and Menu and one is added to the other.
  • 61. Cont… • A MenuBar is capable of holding the menus and a Menu can hold menu items. • Menus are placed on a menu bar.
  • 62. Procedure for Creating Menus • Create menu bar • Add menu bar to the frame • Create menus • Add menus to menu bar • Create menu items • Add menu items to menus • Event handling
  • 64. import java.awt.*; class MenuExample { public static void main (String a[]) { Frame f = new Frame("MenuExample"); f.setLayout(null); MenuBar mb=new MenuBar(); Menu menu=new Menu("Menu"); MenuItem i1=new MenuItem("Item 1"); Menu submenu=new Menu("Sub Menu"); MenuItem i2=new MenuItem("Item 2"); submenu.add(i2); menu.add(i1); menu.add(submenu); mb.add(menu); f.setMenuBar(mb); f.setSize(400,400); f.setVisible(true); } }