Presentation is loading. Please wait.

Presentation is loading. Please wait.

AWT Components and Containers

Similar presentations


Presentation on theme: "AWT Components and Containers"— Presentation transcript:

1 AWT Components and Containers
Ch 19. AWT Components and Containers

2 1 getLocation() - This method is used to get position of the component, as a Point. The usage of the method is shown below. Point p = someComponent.getLocation();  int x = someComponent.getX();  int y = someComponent.getY();  getSize() - This method is used to get the current size of component. Dimension d = someComponent.getSize();  int height = d.height;  int width = d.width;  int height = someComponent.getHeight();  int width = someComponent.getWidth(); 

3 setBackground(Color)/setForeground(Color) - This method is used to change the background/foreground colors of the component. setFont(Font) - This method is used to change the font of text within a component. setVisible(boolean) - This method is used for the visibility state of the component. The component appears on the screen if setVisible() is set to true and if its set to false then the component will not appear on the screen. setEnabled(boolean) - This method is used to toggle the state of the component.

4 java.awt package contains many classes and methods that allow us to create and manage GUI based components . It is used in applets to create web applications but can also be used to create stand-alone applications . Component Management Event Handling Layout Management Graphics Rendering

5 UI elements of AWT : UI Components Layout Managers Containers Events

6

7 Label (String Text, int Alignment)
Labels :generally used to show the text or string in your application and label never perform any type of action. Syntax ::  Label () Creates a blank Label instance. Label (String Text) Creates a Label instance with the specified text. Label L1 = new Label ("This is the label text."); Label (String Text, int Alignment) Creates a Label instance with the specified text and horizontal alignment. Label l1 = new Label ("This is the label text.", Label.CENTER); Justification of label can be left, right or centered.  

8 The key thing to remember about adding components to the applet is the three steps:
Declare the component Initialize the component Add the component to the layout.

9 void setBackground (Color BgdColor) void setAlignment (int Alignment)
Sets the color of the background for the label. void setAlignment (int Alignment) Sets the alignment of the label's contents along the X axis. void setFont (Font TextFont) Sets the font for this component.Arguments void setForeground (Color TextColor) Sets the color of the text for the label. void setText (String Text) Sets the text for the label.

10 Buttons : It is used to trigger actions and other events required for your application.
Syntax: Button button_name = new Button (“Label."); Button.setLabel(String) and Button.getLabel() method. Buttons are added to the it's container using the add (button_name) method.   

11 TextField (String Text)
Text Field:. This component contains single line and limited text information. TextField () Creates a blank TextField instance. TextField (String Text) Creates a TextField instance with the specified text. TextField (int Columns) Creates a blank TextField instance with the specified number of columns. TextField (String Text, int Columns) Creates a TextField instance with the specified text and the specified number of columns.

12 void setBackground (Color BackgroundColor)
String getText () Returns the text in the field. void setBackground (Color BackgroundColor) Sets the background color of the TextField. void setEditable (boolean Editable) Sets the field as being editable or fixed. void setFont (Font TextFont) Sets the font for this component. void setText (String Text) Sets the text for the field.

13 Check Boxes : creates check boxes in your applications. CheckBox ()
Creates a blank CheckBox instance. CheckBox (String Text) Creates a CheckBox instance with the specified text. CheckBox (String Text, boolean Selected) Creates a CheckBox instance with the specified text and the designated selection state.

14 void setBackground (Color BackgroundColor)
boolean isSelected () Returns the state (checked or not checked) of the checkbox. void setBackground (Color BackgroundColor) Sets the background color of the checkbox. void setState (boolean State) Sets the state (checked or not checked) of the checkbox. void setFont (Font TextFont) Sets the font for this component. void setForeground (Color TextColor) Sets the color of the text for the checkbox. void setLabel (String Text) Sets the text for the checkbox.

15 Radio Button : This is used as a group of checkboxes which group name is same. Only one CheckboxGroup chkgp = new CheckboxGroup(); add (new Checkbox ("One", chkgp, false); add (new Checkbox ("Two", chkgp, false);

16 Text Area: The TextArea class is a GUI component which can be used to provide multiple line text input or output. TextArea () Creates a blank TextArea instance. TextArea (String Text) Creates a instance with the specified text. TextArea (int Rows, int Columns) Creates a blank instance with the specified number of rows and columns. TextArea (String Text, int Rows, int Columns) Creates a TextArea instance with the specified text ,number of rows and columns.

17 Conatants for SCrollBars; SCROLL_BARS_BOTH SCROLL_BARS_VETICAL_ONLY
SCROLL_BARS_HORIZONTAL_ONLY SCROLL_BARS_NONE String getText () Returns the text in the field. void setBackground (Color BackgroundColor) Sets the background color of the TextArea. void setEditable (boolean Editable) Sets the field as being editable or fixed. void setFont (Font TextFont) Sets the font for this component. void setText (String Text) Sets the text for the field.

18 Choice () Creates an empty Choice instance.
void insert (String Item, int Index) Inserts the item into this choice at the specified position. int getSelectedIndex () Returns the index of the selected item. void setBackground (Color BackgroundColor) Sets the background color of the choice box. void setFont (Font TextFont) Sets the font for this component. void setForeground (Color TextColor) Sets the color of the text for the choice box. void Select (int Selection) Sets the index of the selected item.

19 List (int Rows, boolean MultiSelect)
Creates an empty List instance with the default 4 rows visible. List (int Rows) Creates an empty List instance with the specified number of visible rows. List (int Rows, boolean MultiSelect) Creates an empty List instance with the specified number of visible rows and the specified MultiSelect switch.

20 void add (String Item, int Index)
Inserts the item into this choice at the next position. void add (String Item, int Index) Inserts the item into this choice at the specified position. int getSelectedIndex () Returns the index of the selected item. String getSelectesItem(); In case of Multiselect : String[]getSelectesItem(); Int getSelectedIndexes();

21 Scrollbar() Scrollbar(int orientation)
Scrollbar(int orientation, int value, int visible, int min, int max) Orientation Scrollbar.HORIZONTAL or Scrollbar.VERTICAL. visible represents the size of the visible portion of the scrollable area in pixels. The Scrollbar uses this when moving up or down a page.

22 Canvas is a region where you can draw things such as circles, triangles, ovals or any other shape.
It is a graphical component that represents a region. It has got a default method which is paint() method.

23 Other Container Classes
Panels:To make Applet or Frame layout easier, we break a frame up into regions and compose each of them separately. Each region is called a Panel. Each can have its own different LayoutManager. Panels don’t have any visible borderlines,title or menu. We can delimit them with differing background colours.

24 A Window is a top-level window with no borders and no menubar.
Window(Frame) Constructs a new Window initialized to an invisible state. pack() Packs the components of the WIndow. show() Shows the Window. dispose() Disposes of the Window.

25 A Frame is a top-level window ,a subclass of Window.
It has title bar,menubar,borders and resize corners. It is normally used as the main window of a standalone application. Frame () Creates a blank instance. Frame (String Text) Creates a Frame instance with the specified text in the title bar.

26 setSize (int Width, int Height)
Resizes this window so that it has the specified Width and Height. setTitle (String Text) Sets the text for the title bar. show () Makes the window visible.

27 Hierarchy Sample Frame Panel Panel Panel Panel Label Label Label
Text Field Text Field Text Field Scrollbar Scrollbar Scrollbar Panel Button Button Button

28 Dialog A Dialog is a top-level window with a title and a border that is typically used to take some form of input from the user. Dialogs are useful specifically to take input because they can be made to block input to other Window objects until the user finishes with the Dialog Restrictions : Cannot have menu. Cannot be resized. Must be opened by parent window .

29 The FileDialog class displays a dialog window from which the user can select a file .
getFile() method returns the current file selection from the FileDialog . getDirectory() returns the current directory for the FileDialog .

30 Menu Component –an abstract class ,is super class of all menu related classes.
To create an instance of MenuBar Class: MenuBar mb=new MenuBar(); setMenuBar(mb); Attatch it to frame . To create popup Menu m=new Menu(“File”); MenuItems m.add(new MenuItem(open”);


Download ppt "AWT Components and Containers"

Similar presentations


Ads by Google