The document provides an overview of the Abstract Windowing Toolkit (AWT) in Java, including:
1. AWT allows the creation of graphical user interfaces and provides containers, components, layout managers, graphics capabilities, and event handling.
2. Common containers include frames, panels, dialogs. Common components include labels, text fields, text areas, buttons, checkboxes, lists.
3. Layout managers like flow, border, grid, card and gridbag layouts are used to position components in containers.
4. Events in AWT are handled through interfaces like action listener, item listener and mouse listener. Programmers implement the appropriate listener interface.
2. Introduction to AWT AWT stands for Abstract Windowing Toolkit AWT is a set of Java classes that allows us to create a GUI Provides various items to create an attractive and efficient GUI Containers Components Layout managers Graphics and drawing capabilities Fonts Events
3. Introduction to AWT (Contd) AWT package consists of classes, interfaces, and other packages
4. Containers Area where you can place your components Can hold elements, can be drawn on, and painted Can have frames, panes, latches, hooks, and smaller-sized components within it java.awt package contains a class named Container. This class directly or indirectly derives two commonly used containers Frames Panels
5. Frames Are separate windows Are a subclass of Window Are displayed within a separate window, and have borders A Frame is independent of an applet, and of the browser Constructors Frame( ) Frame(String, title)
6. Panels Are areas contained within a window Are displayed within the window that the browser or the appletviewer provides and do not have borders Are used to group a number of components A panel cannot be seen directly. Hence, we need to add it to a frame Constructor Panel()
7. Dialog Is a subclass of the class Window The dialog object is constructed as follows: Frame myframe = new Frame(My frame); String title = Title; boolean modal = true; Dialog dlg = new Dialog( myframe, title, modal);
8. Components Can be placed on a user interface, and can be resized, or made visible Examples textfields, labels, checkboxes, textareas scrollbars, scrollpanes, dialog
9. Label Is used to display the String Constructors Label( ) Label(String labeltext) Label(String labeltext, int alignment) Methods setFont(Font f) setText(String s) getText( )
10. TextField Is a single line area, in which text can be displayed, or entered by the user Constructors TextField( ) TextField(int columns) TextField(String s) TextField(String s, int columns) Methods setEchoChar(char) - setTest(String s) getText( ) - setEditable(boolean) isEditable( )
11. TextArea Is used when text is to be accepted as two or more lines Is an editable text field with multi-line features Steps for creating TextArea Create an element Specify the number of rows or columns it must have (optional) Decide its placement on the screen
12. TextArea (Contd) Constructors TextArea( ) TextArea(int rows, int cols ) TextArea(String text) TextArea(String text, int rows, int cols)
14. Button Push/ Command button is the easiest way to trap user action Steps for creating buttons Create the button element, preferably with a label indicating its purpose Decide where to place it on the screen Display it on the screen Constructors Button( ) Button(String text)
15. Checkboxes and RadioButtons Checkboxes are used for multi-option user input Radiobuttons are used as an option button to specify choices Steps to create checkboxes or radiobuttons Create the element Decide its initial state (as selected or unselected) Decide its placement on the screen Display it on the screen Constructors to create checkboxes Checkbox( ) Checkbox(String text) To create radiobuttons, a CheckBoxGroup object is to be created before creating the buttons
16. Lists Choice class enables to create multiple item lists When a list is first created, it is empty Steps to create lists Create the list element Add items (Strings) to it, one by one Decide where to place it on the screen Display it on the screen Example Choice colors=new Choice( ); colors.addItem(Red); colors.addItem(Green);
17. Layout Manager Different types of layouts Flow Layout Border Layout Card Layout Grid Layout GridBag Layout Layout manager is set with the method called setLayout( )
18. FlowLayout Is the default layout manager for applets and panels Components are arranged from the upper left corner to the bottom right corner Constructors FlowLayout mylayout = new FlowLayout(); FlowLayout exLayout = new flowLayout(FlowLayout.RIGHT);
19. BorderLayout Is the default layout manager for Window, Frame and Dialog Layout arranges up to five components in a container Components can be assigned to the NORTH, EAST, SOUTH, WEST and CENTER of the container Example: To add component to North region Button b1= new Button(North Button); setLayout(new BorderLayout( )); add(b1, BorderLayout.NORTH);
20. CardLayout Can store a stack of several layouts Each layout is like a card in a deck Card is usually a Panel object A separate component such as a button controls the card to be displayed on top Steps for creating card layout Set the layout of the main panel to CardLayout Add the other panels to the main panel
21. GridLayout Helps to divide the container into a grid Components are placed in rows and columns Each grid should contain at least one component Is used when all components are of the same size Constructor GridLayout gl = new GridLayout(no. of rows, no. of columns);
22. GridBagLayout Places components precisely Components need not be of the same size Components are arranged in a grid of rows and columns Order of placing the components is not left-to-right and top-to-bottom Constructor GridBagLayout gb = new GridBagLayout( );
23. GridBagLayout To use this layout, information on the size and layout of each component is needed The class GridBagLayoutConstraints holds all the information that the class GridLayout requires to position and size each component
24. Handling Events Events are handled by Abstract Windowing Toolkit Browser Event handler written explicitly by programmers Application needs to register an event handler with an object Handler will be called whenever the appropriate event takes place for the right object Event Listener listens to a particular event that an object generates Each event listener provides methods handle these events
25. Handling Events (Contd) The class that implements the listener needs to define these methods Steps to follow to use the Event Listener model Implement the appropriate listener interface Identify all components that generate events Identify all the events to be handled Implement the methods of the listener, and write the event handling code within the methods Interfaces define several methods to handle each event, that have to be overridden in the class that implements these interfaces