Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.

Slides:



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

Introduction to Java 2 Programming
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interfaces
Graphical User Interfaces (Part IV)
Java Software Development Paradigm Lecture # 12. Basics of GUI.
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
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.
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.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
CPSC150 JavaLynn Lambert CPSC150 Week 12 InheritanceInterfaces.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
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.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
GUI Programming in Java
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
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,
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.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
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.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
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.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Java Programming, Second Edition Chapter Thirteen Understanding Swing Components.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Graphical User Interface (GUI)
1 Lecture 8: User Interface Components with Swing.
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.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
Timer class and inner classes
Advanced Programming in Java
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces

Chapter 10 - Writing Graphical User Interfaces2 Understanding Java’s GUI Classes  Java has two sets of GUI classes  Original GUI class – AWT – Abstract Window Toolkit  Located in java.awt package  Updated GUI class – Swing  Located in javax.swing  Both contain classes to create controls  Windows  Push buttons  Text fields  Menus 10

Chapter 10 - Writing Graphical User Interfaces3 Understanding Java’s GUI Classes 10

Chapter 10 - Writing Graphical User Interfaces4 Understanding Java’s GUI Classes

Chapter 10 - Writing Graphical User Interfaces5 Understanding Java’s GUI Classes  Style and appearance of GUI components are called their look and feel  AWT classes adopt look and feel of the local platform  With Swing you have the option of using local platform look and feel or a standard look and feel (called “metal”)  Both AWT and Swing take advantage of inheritance 10

Chapter 10 - Writing Graphical User Interfaces6 Understanding Java’s GUI Classes 10

Chapter 10 - Writing Graphical User Interfaces7 Using AWT Classes  Class definition of a GUI generally follows the structure you used for a problem domain class  One difference is that you create an instance of GUI and make it visible  Write statements to instantiate GUI components  Then write statements to add them to the window

Chapter 10 - Writing Graphical User Interfaces8 Creating a Window with a Button  Frame inheritance hierarchy is Windows, Container, and Component  Your GUI inherits from all three super classes  Need to import the AWT package: import java.awt.*;  Class is a subclass of Frame – must extend the Frame class public class AWTFrameWithButton extends Frame

Chapter 10 - Writing Graphical User Interfaces9 Creating a Window with a Button  Constructor must accomplish five tasks: 1.Create and instance of Button 2.Add the button to the frame 3.Establish the frame size 4.Place a title on the frame 5.Make the frame visible 10

Chapter 10 - Writing Graphical User Interfaces10 Creating a Window with a Button

Chapter 10 - Writing Graphical User Interfaces11 Using Layout Managers  AWT includes several classes called layout managers  Used to determine how components are positioned on containers such as frames and panels  Most frequently used layout managers:  FlowLayout  BorderLayout  GridLayout

Chapter 10 - Writing Graphical User Interfaces12 Using Layout Managers  To use:  Instantiate the manager you want to use  Invoke the setLayout method, passing a reference to the layout manager you want to use  Components placed on a container using the BorderLayout manager expand to fill the space available 10

Chapter 10 - Writing Graphical User Interfaces13 Using Layout Managers

Chapter 10 - Writing Graphical User Interfaces14 Using Layout Managers  FlowLayout manager places components on the frame as you add them, left to right  GridLayout manager arranges the container into a grid consisting of rows and columns  When you instantiate you need to specify the number of rows and columns you want to have

Chapter 10 - Writing Graphical User Interfaces15 Using Layout Managers

Chapter 10 - Writing Graphical User Interfaces16 Handling Java Events  Users interact with GUI by entering data and clicking on components  An event is a signal that the user has taken some action  Event is also an instance of a class – such as MouseEvent, WindowEvent, etc.

Chapter 10 - Writing Graphical User Interfaces17 Handling Java Events 10

Chapter 10 - Writing Graphical User Interfaces18 Handling Java Events  Event listener registers with event source by invoking a method in the event source  Button events are called action events – registration method is named addActionListener  Next step is to write a method to handle the event – for buttons this method is actionPerformed

Chapter 10 - Writing Graphical User Interfaces19 Handling Java Events  There are three methods for handling events  Implementing interfaces  Extending Adapter classes  Creating Inner classes

Chapter 10 - Writing Graphical User Interfaces20 Implementing Interfaces  When you implement an interface you must override all of its methods  Implement the appropriate interface for the event  Write methods to override the interface’s methods and deal with the events  Interface for buttons is ActionListener  Need to import interface: import java.awt.event.*;

Chapter 10 - Writing Graphical User Interfaces21 Implementing Interfaces

Chapter 10 - Writing Graphical User Interfaces22 Extending Adapter Class  Second way of dealing with events is to use an adapter class  Supplied class that implements a listener interface, and then overrides all of the interface methods with null methods  Most override methods you want to respond to  Need to create two classes – one that extends Frame, and another that extends WindowAdapter

Chapter 10 - Writing Graphical User Interfaces23 Extending Adapter Class

Chapter 10 - Writing Graphical User Interfaces24 Creating Inner Classes  Third way of dealing with events is to use an anonymous inner class  Anonymous inner class is an inner class without a name  Used to simplify code needed to handle events  See pp

Chapter 10 - Writing Graphical User Interfaces25 Creating Inner Classes

Chapter 10 - Writing Graphical User Interfaces26 Creating Inner Classes

Chapter 10 - Writing Graphical User Interfaces27 Creating Inner Classes  When you compile an inner class – the compiler produces two class files  If your outer class is named Outer and your inner class is named Inner – a filed called Outer$Inner.class is created  If the inner class is anonymous, the class file for the inner class has the same name as the outer class with a number beginning with 1 concatenated after the $ 10

Chapter 10 - Writing Graphical User Interfaces28 Converting an Application to an Applet  An applet is a class you write that extends the Applet class  Runs under the control of a web browser  Can convert AWTFrameAndComponent to an applet by:  Delete the main method  Delete all references to close button  Delete the anonymous inner class  Delete the shutDown method  Delete the setVisible, setSize, and setTitle methods

Chapter 10 - Writing Graphical User Interfaces29 Converting an Application to an Applet

Chapter 10 - Writing Graphical User Interfaces30 Using Swing Classes  Swing classes are newer improved versions of the original AWT classes  Want to use for there improved appearance and capability

Chapter 10 - Writing Graphical User Interfaces31 Converting AWT GUI to Swing  Swing components reside in javax.swing: import javax.swing.*;  Change class names of Button, Label, TextField, and Panel to JButton, JLabel, JTextField, and JPanel  Use JFrame instead of Frame  Compare Figure vs. Figure

Chapter 10 - Writing Graphical User Interfaces32 Converting AWT GUI to Swing 10 AWT

Chapter 10 - Writing Graphical User Interfaces33 Adding Drop-down Menus  Swing drop down menus consist of three classes  JMenuBar  JMenu  JMenuItem  See Figure on page

Chapter 10 - Writing Graphical User Interfaces34 Adding Drop-down Menus 10

Chapter 10 - Writing Graphical User Interfaces35 Converting a Swing Application to an Applet  Steps for conversion include:  Extend JApplet instead of JFrame  Delete the main method  Delete all references to close menu item  Delete the anonymous inner class  Delete the shutDown method  Delete the statements setVisible, setSize, and setTitle

Chapter 10 - Writing Graphical User Interfaces36 Converting a Swing Application to an Applet