AWT.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Objectives The objectives of this chapter are: To discuss the classes present in the java.awt package To understand the inheritance hierarchy of the AWT.
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Jan Event Handling -1.1 Yangjun Chen Dept. Business Computing University of Winnipeg.
Events and the AWT The objectives of this chapter are: To understand the principles of the Java 1.1 event model To understand how the event model is used.
Event Handling n Events: an event is an object that describes a state change in a source. n Event Sources: A source is an object that generates an event.
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
Jan Containers Yangjun Chen Dept. Business Computing University of Winnipeg.
Graphical User Interfaces (GUI) Abstract Windows Toolkit (AWT): java.awt GUI elements: Primitive Button, Label, Checkbox, Scrollbar, etc. Container Panel,
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
AWT Components (Chapter 9) Java Certification Study Group January 21, 1999 Mark Roth.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java Programming 1 Java Programming II Events, AWT, and Swing.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
Layout Managers Arranges and lays out the GUI components on a container.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.
Pravin Yannawar, DOCS, NMU Jalgaon. Basic Java : Event handling in AWT and Swing 2 Objectives of This Session Explain the Event handling mechanism & demonstrate.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
Computer Science [3] Java Programming II - Laboratory Course Lab 4: Common GUI Event Types and Listener Interfaces Layout Mangers Faculty of Engineering.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
9.1 Java Packages A collection of classes Allows classes to be grouped arbitrarily Hierarchical structure independent of inheritance Classes can.
Object Oriented Programming.  Interface  Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
1 IM103 week 8 (C&K ch17, p412) Advanced graphic programming Learning objectives By the end of this chapter you should be able to:  create dialogue windows.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 5
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
CSC 205 Programming II Lecture 5 AWT - I.
GUI building with the AWT
Welcome To java
Aum Amriteshwaryai Namah
Event Handling Chapter 2 Objectives
Java Swing.
AWT Components.
Abstract Window ToolKit (AWT)
CSE 114 – Computer Science I Event Programming
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
AWT Components and Containers
Programming Graphical User Interface (GUI)
GUI building with the AWT
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

AWT

Objectives Abstract Window Toolkit (AWT) Containers, Components and Layout Managers Events and Event Handling

Building Java GUI

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

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

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.

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.

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.

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(); } }

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.

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);

AWT Components

Button frame = new Frame("Button Sample"); button = new Button("Button"); button.addActionListener(this); frame.add(button); frame.setSize(200, 200); frame.setVisible(true);

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);

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);

Choice frame = new Frame("Choice Sample"); choice = new Choice(); choice.addItemListener(this); choice.add("First"); choice.add("Second"); choice.add("Third"); frame.add(choice);

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);

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);

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);

Controlling Visual Aspects Colors: setForeground(); setBackground(); Fonts: setFont(); Font ft = new Font("TimesRoman", Font.PLAIN, 14); comp.setFont(ft);

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

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();

Layouts FlowLayout BorderLayout GridLayout CardLayout GridBagLayout

Default Layout Managers Component Container Panel Window Applet Frame Dialog FlowLayout BorderLayout

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));

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) );

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) );

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() );

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

GUI Event Handling

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

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

Event Categories java.awt.event ActionEvent FocusEvent AdjustmentEvent InputEvent KeyEvent MouseEvent ContainerEvent ComponentEvent WindowEvent ItemEvent TextEvent

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

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

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