Download presentation
Presentation is loading. Please wait.
1
AWT
2
Objectives Abstract Window Toolkit (AWT)
Containers, Components and Layout Managers Events and Event Handling
3
Building Java GUI
4
Abstract Window Toolkit
Provides graphical user interface (GUI) components. Contains classes that can be extended and inherited. Every GUI component displayed on screen is subclass of Component or MenuComponent. Has Container class which is subclass of Component and includes two subclasses. Panel Window
5
java.awt Package Object PopupMenu MenuBar MenuItem Menu
MenuComponent CheckboxMenuItem Object Button Canvas Applet Checkbox Choice Component Panel Container Label Frame Window Dialog ScrollBar TextComponent ScrollPane FileDialog TextArea TextField
6
Containers Add components with the add() method.
Two main types of containers are Window and Panel. A Window is a free floating window on the display. Two important types of Window: Frame Dialog A Panel is a container of GUI components that must exist in the context of some other container such as Window or Applet.
7
Building Graphical User Interfaces
The position and size of a component in a container is determined by a layout manager. Size or position of a component can be controlled by disabling the layout manager. Use setLocation, setSize() or setBounds() on components to locate components in the container.
8
Frame Subclass of Window. Have Title and resizing corners.
Are initially invisible, use setVisible(true) to show them. Have BorderLayout as the default layout manager. Use setLayout() method to change the default layout manager.
9
Frame Example Import java.awt.*;
public class FrameExample { private Frame f; public FrameExample() { f = new Frame("Hello Out There!"); } public void launchFrame() { f.setSize(170, 170); f.setBackground(Color.blue); f.setVisible(true); } public static void main(String args[]) { FrameExample guiWindow = new FrameExample(); guiWindow.launchFrame(); } }
10
Panel Panel is contained within another Container.
Provide space for Components. FlowLayout is the default layout manager. Allow subpanels to have their own layout manager.
11
Dialog It is a free standing window without the resizing corners.
Two types of Dialog: Modeless: Allows the user to interact with other windows also. Model: Doesn't allow to interact with other windows until it is closed Dialog d = new Dialog(f, "Dialog", true); Button bt = new Button("OK"); d.add(bt); d.pack(); d.setVisible(true);
12
AWT Components
13
Button frame = new Frame("Button Sample"); button = new Button("Button"); button.addActionListener(this); frame.add(button); frame.setSize(200, 200); frame.setVisible(true);
14
Checkbox Frame = new Frame("Checkbox Sample"); checkbox1 = new Checkbox("One", true); checkbox2 = new Checkbox("Two", false); checkbox3 = new Checkbox("Three", false); checkbox1.addItemListener(this); checkbox2.addItemListener(this); checkbox3.addItemListener(this); frame.setLayout(new FlowLayout()); frame.add(checkbox1); frame.add(checkbox2); frame.add(checkbox3);
15
CheckboxGroup frame = new Frame("CheckBoxGroup Sample"); group = new CheckboxGroup (); checkbox1 = new Checkbox("One", group, true); checkbox2 = new Checkbox("Two", group, false); checkbox3 = new Checkbox("Three", group, false); checkbox1.addItemListener(this); checkbox2.addItemListener(this); checkbox3.addItemListener(this); frame.setLayout(new FlowLayout()); frame.add(checkbox1); frame.add(checkbox2); frame.add(checkbox3);
16
Choice frame = new Frame("Choice Sample"); choice = new Choice();
choice.addItemListener(this); choice.add("First"); choice.add("Second"); choice.add("Third"); frame.add(choice);
17
TextField Frame = new Frame("TextField Sample"); tf = new TextField ("Enter Text", 30); tf.addActionListener(this); frame.add(tf); frame.setSize(200, 70); frame.setVisible(true);
18
List frame = new Frame("List Sample"); list = new List(4, true);
list.addItemListener(this); list.add("First"); list.add("Second"); list.add("Third"); frame.add(list);
19
How to create a Menu Create a MenuBar Object and set it into menu container such as a Frame Create one or more Menu objects and add them to menu bar object Create one or more MenuItem objects and add them to menu object Frame f = new Frame("MenuBar"); MenuBar mb = new MenuBar(); Menu m1= new Menu("File"); mb.add(m1); f.setMenuBar(mb);
20
Controlling Visual Aspects
Colors: setForeground(); setBackground(); Fonts: setFont(); Font ft = new Font("TimesRoman", Font.PLAIN, 14); comp.setFont(ft);
21
Toolkit class Toolkit class is an abstract superclass of all platform- specific implementations of the Abstract Window Toolkit getDefaultToolkit - Returns current toolkit object getImage(String fileName) - Loads an image from file getScreenResolution - Returns number of "pixels per inch" getScreenSize - Returns Dimension object having width and height of screen in pixels getPrintJob - Returns PrintJob object
22
Printing Following code fragment prints a Frame
Frame frame = new Frame("Print Test"); Toolkit toolkit = frame.getToolkit(); PrintJob job = toolkit.getPrintJob(frame, "Test Printing", null); Graphics g = job.getGraphics(); frame.printComponents(g); g.dispose(); job.end();
23
Layouts FlowLayout BorderLayout GridLayout CardLayout GridBagLayout
24
Default Layout Managers
Component Container Panel Window Applet Frame Dialog FlowLayout BorderLayout
25
FlowLayout Manager Positions components on a line by line basis
Default Layout Manager for Panel Class Components added from left to right Default alignment is centered Uses components preferred sizes setLayout(new FlowLayout(int align, int hgap, int vgap));
26
BorderLayout Manager It has five distinct areas: NORTH, SOUTH, EAST, WEST and CENTER Default Layout Manager for Frame and Dialog Classes Components are added to specific regions The Resizing behaviour: North, South and Center regions adjust horizontally. East, West and Center regions adjust vertically. setLayout( new BorderLayout(int hgap, int vgap) );
27
GridLayout Manager It divides whole area into cells
Components are added left to right, top to bottom. All regions are equally sized. The constructor specifies the rows and columns. setLayout( new GridLayout(int rows, int cols) );
28
CardLayout Manager It treats the interface as a series of cards
Only one card can be viewed at any time add() method is used to add cards to CardLayout setLayout( new CardLayout() );
29
Drawing in AWT We can draw in any Component
although AWT provides Canvas class for drawing paint() method for drawing paint method is called everytime the component is shown Every Component has a Graphics object Graphics class implements many drawing methods
30
GUI Event Handling
31
What is an Event? Events:
Objects that describe what happened Event sources: The generator of an event Event Handlers: Method that receives an event object and processes the user interaction Frame User clicks button Button ActionEvent Event Handler
32
Delegation Model Client objects register with a GUI component they want to register GUI components only trigger the handlers for the type of event that has occurred Delegation model distributes the work among multiple classes
33
Event Categories java.awt.event ActionEvent FocusEvent
AdjustmentEvent InputEvent KeyEvent MouseEvent ContainerEvent ComponentEvent WindowEvent ItemEvent TextEvent
34
Event Categories Category Interface Name Action ActionListener Item
ItemListener Mouse MouseListener Mouse Motion MouseMotionListener Key KeyListener Focus FocusListener Adjustment AdjustmentListener Component ComponentListener Window WindowListener Container ContainerListener Text TextListener
35
Multiple Listeners Multiple listeners cause unrelated parts of a program to react to the same event The handlers of all registered listeners are called when the event occurs
36
Event Adapters The user defined listener classes can
extend adapter classes and override the methods that are needed Adapter classes implement each interface containing more than one method and provide empty implementation of methods
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.