GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance.
GUI. Swing Class Hierarchy Swing Components Swing Conatiners  JFrame – top-level window to store components.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
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
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Java's Graphical User Interface Toolkit
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
CS GUI Frameworks CS 3331 Fall CS 3331 Outline MVC Model GUI components (widgets) Layout managers Handling events.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Computer Science 209 GUIs Model/View/Controller Layouts.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 14 : Swing II King Fahd University of Petroleum & Minerals College of Computer Science.
Creating a GUI Class An example of class design using inheritance and interfaces.
Lab 4: GUIs, Panels, Mouse and Key Listeners ICOM4015: FALL 2014 A N I NTRODUCTION TO P ANELS AND L AYOUTS CREATED BY KATYA I. BORGOS REVISED BY AMIR H.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Interfaces Describe what classes should do, without specifying how they should do it Not a class, but a set of requirements for classes that want to conform.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Graphical User Interface (GUI)
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.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Chapter 7 A First Look at GUI Applications Layout Managers.
Events and Event Handling
Object-Orientated Analysis, Design and Programming
Lecture 8 Object Oriented Programming Using Java
“Form Ever Follows Function” Louis Henri Sullivan
Modern Programming Language Java
Java GUI.
A Quick Java Swing Tutorial
Programming in Java Event Handling
GUI Programming III: Events
Chapter 13: Advanced GUIs and Graphics
Creating Graphical User Interfaces
GUI building with the AWT
A Quick Java Swing Tutorial
Border Layout, using Panels, Introduction to PinBallGame
GUI building with the AWT
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

GUI

Swing Class Hierarchy

Swing Components

Swing Conatiners  JFrame – top-level window to store components

Swing Conatiners  JPanel – container; can be embedded in JFrame

Layouts  FlowLayout  arranges elements in a row  elements centered by default within container  GridLayout  subdivides container into cells of identical sizes  components take up all available space of a cell  BorderLayout  subdivides container into 5 areas: N, S, E, W, Center

Layouts 3x3 GridLayout4x1 GridLayoutBorderLayout FlowLayout used to place the 3 panels in the Jframe.

Grid Layout 3x3 GridLayout4x1 GridLayoutBorderLayout GridLayout layout = new GridLayout(3, 3); panel.setLayout(layout); button1 = new JButton( "1" ); panel.add( button1 ); button2 = new JButton( "2" ); panel.add( button2 ); button3 = new JButton( "3" ); panel.add( button3 ); button4 = new JButton( "4" ); panel.add( button4 ); Components are stretched to occupy the whole panel area.

Border Layout 3x3 GridLayout4x1 GridLayoutBorderLayout BorderLayout layout = new BorderLayout(); panel.setLayout(layout); button1 = new JButton( "North" ); panel.add( button1, BorderLayout.NORTH ); button2 = new JButton( "South" ); panel.add( button2, BorderLayout.SOUTH ); button3 = new JButton( "East" ); panel.add( button3, BorderLayout.EAST ); button4 = new JButton( "West" ); panel.add( button4, BorderLayout.WEST ); button5 = new JButton( "Center" ); panel.add( button5, BorderLayout.CENTER ); Center area gets most of the space. The other areas are given only as much as they need. Not all areas need to be occupied.

Listeners  Process events from components, containers  ActionListener (JButton, Timer, JComboBox)‏  ChangeListener (JSlider)‏  MouseListener, MouseMotionListener (JPanel, JFrame)  Listeners are interfaces; must implement ALL specified methods  ActionListener: void actionPerformed(ActionEvent e)‏  ChangeListener: void stateChanged(ChangeEvent e)‏  MouseListener: void mouseClicked(MouseEvent e) void mousePressed(MouseEvent e) void mouseReleased(MouseEvent e) void mouseEntered(MouseEvent e) void mouseExited(MouseEvent e)‏  MouseMotionListener: void mouseMoved(MouseEvent e) void mouseDragged(MouseEvent e)

Adapter classes  Convenience classes  server as intermediaries between the available interfaces and the user-defined (listener) classes that implement the interfaces  make it possible to implement only the methods of interest

Adapter classes  Convenience classes  server as intermediaries between the available interfaces and the user-defined (listener) classes that implement the interfaces  make it possible to implement only the methods of interest abstract class MouseAdapter implements MouseListener, MouseMotionListener { void mousePressed(MouseEvent e) { // empty body } void mouseReleased(MouseEvent e) { // empty body } void mouseEntered(MouseEvent e) { // empty body } void mouseExited(MouseEvent e) { // empty body } voidmouseMoved(MouseEvent e) { // empty body } voidmouseDragged(MouseEvent e) { // empty body } } MouseListener methods MouseMotionListener methods

Adapter classes  Convenience classes  server as intermediaries between the available interfaces and the user-defined (listener) classes that implement the interfaces  make it possible to implement only the methods of interest abstract class MouseAdapter implements MouseListener, MouseMotionListener { abstract void mousePressed(MouseEvent e); abstract void mouseReleased(MouseEvent e); abstract void mouseEntered(MouseEvent e); abstract void mouseExited(MouseEvent e); abstract voidmouseMoved(MouseEvent e); asbtract voidmouseDragged(MouseEvent e); }

Adapter classes  Convenience classes  server as intermediaries between the available interfaces and the user-defined (listener) classes that implement the interfaces  make it possible to implement only the methods of interest abstract class MouseAdapter implements MouseListener, MouseMotionListener { … … … … … … … … … … … … … } class LineListener extends MouseAdapter { … implement only the methods of interest … }

File IO

Text File Output  PrintWriter used for writing to file; same methods as in System.out  As if output is written to the screen with the extra steps of opening a file, closing the file, and catching exceptions try { PrintWriter output = new PrintWriter("output-file.txt"); output.println("Hello"); output.printl(42); output.println(3.1459); output.close(); } catch (Exception e) { // report error }

Text File Input  Scanner used for reading from file; same as in CS111  As if input is received from the keyboard with the extra steps of opening a file, closing the file, and catching exceptions  Reading stops as soon as appropriate token found; otherwise fails try { Scanner input = new Scanner(new File("input-file.txt")); String word = input.next(); int answer = input.nextInt(); double pi = input.nextDouble(); input.close(); } catch (Exception e) { // report error } Need to know the file format Whitespaces are ignored

Serialization  Mechanism for making exact copies of objects  For simple classes enough to declare implements Serializable  Application – saving / reading actual objects from a file

Saving with Serialization  ObjectOutputStream and method writeObject(obj) try { FileOutputStream file = new FileOutputStream("pets.ser"); ObjectOutputStream output = new ObjectOutputStream(file); output.write(petsList.size()); for (Pet pet : petsList) { output.writeObject(pet); } output.close(); } catch (Exception e) { System.out.println("Could not write to file. " + e); }

 ObjectInputStream and method readObject() try { FileInputStream file = new FileInputStream("pets.ser"); ObjectInputStream output = new ObjectInputStream(file); int count = input.readInt(); for (int i = 0; i < count; i++) { Pet pet = input.readObject(); } output.close(); } catch (Exception e) { System.out.println("Could not read from file. " + e); } Reading with Serialization

Exceptions

 Mechanism for handling unexpected RUN-TIME conditions (errors)‏  Force the programmer to handle error conditions  Allow for separating the logic of the code from error-handling  Sometimes no other option to report the value:  constructor  minElement, maxElement  Example – see FileIO

Exceptions  Can create our own type of exception (should inherit from Exception)‏ class EmptyArrayException extends Exception { public void EmptyArrayException()‏ { super(); } public void EmptyArrayException(String message)‏ { super(message); }

Exceptions  Example of our own Exception --- throw/throws int minElement(int[] numbers) throws EmptyArrayException { // empty array --- throw an exception if (numbers.length == 0)‏ { throw EmptyArrayException(“Empty array given”); } // //... compute smallest element... // }

extends vs. implements class vs. interface multiple inheritance

Interfaces  An interface specifies a collection of methods  An interface does not have data members or code for methods  A class that implements the interface must provide code (implementation) for all methods listed in the interface interface RemoteControllable { public void play(); public void stop(); public void ffwd(); } class VCR implements RemoteControllable { // must provide code for all methods in RemoteControllable } class DVD implements RemoteControllable { // must provide code for all methods in RemoteControllable }

Multiple Inheritance String name; //species void setName(String n) { } String name; // pet’s name void setName(String n) { } Class MammalClass Pet Class Cat

Multiple Inheritance String name; //species void setName(String n) { } String name; // pet’s name void setName(String n) { } Class MammalClass Pet Which name is inherited? Which setName() is inherited? Class Cat

 Complex rules required to disambiguate in multiple inheritance  Java does not support multiple inheritance; C++ does Multiple Inheritance String name; //species void setName(String n) { } String name; // pet’s name void setName(String n) { } Class MammalClass Pet Which name is inherited? Which setName() is inherited? Class Cat

 What if we still want a Cat to behave like a Mammal and Pet interface Mammal { // all methods (behaviors) common to mammals // no code is specified, just the behavior names (methods) } class Pet { // description of generic pet } class Cat extends Pet implements Mammal { // has all behaviors of a Pet – could override some // must implement all behaviors of Mammal } Multiple Inheritance

 Can now use Cat objects anywhere Mammal behaviors required or where Pet objects are required public void hunt(Mammal predator, Mammal prey) { // do something; could send a Cat as // either prey or predator } public void doTricks(Pet pet) { // do something; could send a Cat for pet } Multiple Inheritance

 A Java class can only extend from one other class (single inheritance)  A Java class can implement multiple interfaces – can ambiguity arise? Multiple Interfaces Implementation

 A Java class can only extend from one other class (single inheritance)  A Java class can implement multiple interfaces – no ambiguity since  an interface cannot have data members  an interface cannot have code (implementation) for methods Multiple Interfaces Implementation