June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.

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.
Graphic User Interfaces Layout Managers Event Handling.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
CS3157 Java UI Recitation. Material Covered: Overview of AWT components, Event Handling, creating applets, and sample UI. Not covered in recitation: Drawing,
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
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.
GUI Programming in Java Tim McKenna GUI Programming Concepts l conventional programming: sequence of operations is determined by the program.
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
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, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Applets and Frames CS 21a: Introduction to Computing I First Semester,
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition.
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
3461A Readings from the Swing Tutorial. 3461A Overview  The follow is the Table of Contents from the trail “Creating a GUI with JFC/Swing” in the “The.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
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.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Object Oriented Programming.  Interface  Event Handling.
SEEM3460 Tutorial GUI in Java. Some Basic GUI Terms Component (Control in some languages) the basic GUI unit something visible something that user can.
May 12, 1998CS102-01Lecture 7-3 Building GUIs in Java I CS Lecture 7-3 A picture's worth a thousand words.
Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE Java: An Eventful Approach An innovative.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
Event Handling CS 21a: Introduction to Computing I First Semester,
1 Lecture 8: User Interface Components with Swing.
Object Oriented Programming in Java Habib Rostami Lecture 10.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CSC 205 Programming II Lecture 5 AWT - I.
GUI building with the AWT
Welcome To java
Lecture 09 Applets.
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
GUI building with the AWT
Constructors, GUI’s(Using Swing) and ActionListner
Programming Graphical User Interface (GUI)
11.1 Applets & graphics.
GUI building with the AWT
Presentation transcript:

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing Toolkit (AWT)

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 2 Today’s Lecture AWT Fundamentals –

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 3 History AWT in JDK 1.0 AWT JDK 1.1 JFC/Swing

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 4 Purpose of AWT Build Graphical User Interfaces Keep native platform look Uses native platform API

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 5 Hierarchy of GUI Components –superclass to all classes is Component –Component Button Canvas Checkbox Choice Label TextField Container –Panel –SCrollPan –Window –Frame List ScrollBar

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 6 Containers containers group widgets or other containers Containers can embed other containers containers can have a particular layout examples: –Panel –Applet –Frame

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 7 Widgets Button Canvas Label Checkbox Choice List TextField TextArea examples: –Button b = new Button(); –Label l = new Label(“OK”);

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 8 Event Handlers Event handlers are registered with Components and handle events that are generated –mouse clicks –keyboard keys –windows events Event handlers often implement ActionListener example: –ActionListener theListener = new MyListener(); –Button b = new Button(); –b.addActionListener(theListener);

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 9 How to build a GUI Add components to a Container Create Event handler classes Setup event handlers to respond to clicks,... Display the GUI Therefore you always need: –one or more containers –one or more Components –one or more Event Handler

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 10 Extend the Applet class import java.awt.Button; import java.applet.Applet; public class TheApplet extends Applet { public void init() { // add() one or more widgets to the applet itself // create an event handler for a widget }

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 11 Add a widget to the Applet Example: //Create the widget Button aButton = new Button(“some text”); // add(Component c) is a method of Container add(aButton);

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 12 Create an ActionListener import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Component; public class Beeper implements ActionListener { public void actionPerformed(ActionEvent event) { // get the conponent which initiated the event Component c = (Component)event.getSource(); // beep c.getToolkit().beep(); }

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 13 Create HTML

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 14 View the file Use the appletviewer or use your browser to bring up the html file

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 15 Standalone GUI Create a class with a main method Create a Frame: f = new Frame(“frame name”); Create a Button and add it to the frame Create an ActionListener class (reuse Beeper) Register the event handlers with the button Bring up the frame: f.pack(); f.show();

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 16 Enhanced standalone GUI Use Button class’ setActionCommand(String s) method This method gives a name to the click “action” of the button example: –b.setActionCommand(“QUIT”) Add a few more buttons to your frame and give them various actions

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 17 Enhanced Action Listener Within the actionPerformed(ActionEvent event) method, check for which action command is performed 1) Use the ActionEvent method: getActionCommand() 2) check to see which event was raised 3) when event “QUIT” perform a System.exit(0);

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 18 Layouts Layouts define how the components are laid within the containers You have used a default layout all along You can assign a layout explicitly example –Frame f = new frame(“frame name”); –f.setLayout(new GridLayout(3,2));

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 19 Types of layouts example –GridLayout –FlowLayout –CardLayout –BorderLayout –GridBagLayout

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 20 Nested Classes Nested Classes are a powerful feature of Java starting with JDK 1.1 with nested classes, Java lets you define a class as a member of another class. Such a class is a nested class Nested class can see all of the class members of its enclosing class, even the private ones

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 21 Nested Class Class EnclosingClass{ class InnerClass{... } An inner class is a nested class whose instance exists within an instance of its enclosing class and has direct access to the instance members of its enclosing instance EnclosingClass InnerClass

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 22 Use Inner Classes for Listeners Create a class that extends Frame Add Components to the frame in the constructor Define an inner class which extends ActionListener Instantiate the inner class within the frame’s constructor: the inner class now has access to the Frame’s variables

June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 23 Example of Inner Class import java.awt.*; import java.awt.event.*; public class AppFrame extends Frame { Button b1, b2; public AppFrame(String title) { super(title); // instantiate buttons … // instantiate inner class ActionListener al = new MyHandler(); b1.addActionListener(al); } class MyHandler extends ActionListener { public void actionPerformed(ActionEvent event) { // get the conponent which initiated the event Component c = (Component)event.getSource(); if (c==b1) { //do this…} if (c==b2) { // do that...} }