University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Graphical User Interfaces Lecture 25, Thu Apr 6 2006

Slides:



Advertisements
Similar presentations
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Advertisements

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Inheritance III, Graphical User Interfaces Lecture.
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Graphical User Interfaces
Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
GUIs Mimi Opkins CECS277.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
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,
Chapter 12 Event Handling. Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons,
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Writing Graphic Applications.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. In order to display a drawing in a frame, define a class that extends.
Fall 2006Adapded from Java Concepts Companion Slides1 Event Handling Advanced Programming ICOM 4015 Lecture 13 Reading: Java Concepts Chapter 12.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Copyright © 2013 by John Wiley & Sons. All rights reserved. GRAPHICAL USER INTERFACES CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Nine and Ten Graphics.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
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.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
For (int i = 1; i
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Graphical User Interfaces Lecture 24, Tue Apr
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
CHAPTER 10 EVENT HANDLING. CHAPTER GOALS To understand the Java event model To install mouse and action listeners To accept mouse and text input To display.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Event Handling and GUI Components.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
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:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Lecture 15 Basic GUI programming
CSC 205 Programming II Lecture 5 AWT - I.
A First Look at GUI Applications
GUI AND GRAPHICS.
A Quick Java Swing Tutorial
Ellen Walker Hiram College
Chapter 12 Event Handling
A Quick Java Swing Tutorial
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Graphical User Interfaces Lecture 25, Thu Apr based on slides by Kurt Eiselt

2 News n Midterm solutions going out at end of week n Assignment 3 due Friday Apr 7, 5pm n CSLC will have special exam period hours n Watch for review session announcement n Final exam: Mon Apr 24, 3:30pm, HEBB TH

3 Weekly Questions n Last one due today n Grading: full credit if did 7 or more n Check next week for grade in WebCT

4 Reading n This week: n Chapter 5.1, 5.2, 11.5, 12.1, 12.2, 12.3

5 Exam n Practice exam available under Challenge link from course page

6 What You Should Know Chapter 1, 2, 3, 4, Chapter 5.1, 5.2 Chapter 6, 7 Chapter 8 (but skip 8.2, 8.3, and 8.4) Chapter 9.3, 9.6, 9.7, 9.8 Chapter 11.1, 11.2, 11.3, 11.5 Chapter 12.1, 12.2, 12.3 Chapter 13 (except for ) plusUML diagrams as you've seen in labs/tutorials go back in the powerpoint slides and review sorting

7 How to prepare Read everything that we told you to read on the previous slide Review lecture notes and code written in class (available from web) Practice, practice, practice -- write programs (especially using inheritance and abstract classes) If you're not getting it and want to try a different approach, run to the bookstore (or head to Amazon.ca or Indigo.ca) and get a copy of...

8 How to prepare Head First Java by Kathy Sierra and Bert Bates Read this book, work all the problems (there are zillions), and you should have a better grasp of what's going on with Java. (I have no financial interest in this book or any bookseller.)

9 A Problem The Coca-Cola Company has founded Vending University. VU has two kinds of students. The full time students pay $ per credit in tuition up to a maximum of $ (12 credits), even if they enroll in more than 12 credits. Tuition for students in the executive program is computed differently; these students pay a $ "executive fee" plus $ per credit, with no ceiling or cap on the total. Each student has a name and is enrolled for some integer number of credits. Write an abstract superclass called Student, and write concrete subclasses called FullTimeStudent and ExecutiveStudent. The method for computing the tuition should be called computeTuition(). Now do it again, but with an interface called Student instead of an abstract superclass. Provide a test program that uses polymorphism to test your classes and methods.

10 Programming Practice n Two kinds of practice, both are important! n Using computer, open book, Internet, discussing approach with friends, take as long as you need to fully understand n Closed book, write on paper, don't talk to anybody about the question, time pressure

11 Objectives n Taste of what's under the hood with graphical programming n note: taste, not mastery!

12 Recap: Making a frame window Step 1: Construct an object of the JFrame class. Step 2: Set the size of the frame. Step 3: Set the title of the frame to appear in the title bar (title bar will be blank if no title is set). Step 4: Set the default close operation. When the user clicks the close button, the program stops running. Step 5: Make the frame visible.

13 Recap: Drawing boxes Step 1: Define RectangleComponent extending JComponent Step 2: Override paintComponent() method. Step 2.1: Create Rectangle object Step 2.2: Draw Rectangle using Graphics2D object Step 2.3: Move Rectangle, Draw Rectangle, … Step 3: In driver, construct RectangleComponent, add to JFrame

14 Recap: RectangleComponent code import java.awt.Graphics; // AWT is the Abstract Windowing Toolkit, import java.awt.Graphics2D; // an older graphical user interface import java.awt.Rectangle; // toolkit import javax.swing.JPanel; import javax.swing.JComponent; public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Rectangle box = new Rectangle(5, 10, 50, 75); g2.draw(box); box.translate(80,100); g2.draw(box); }

15 Recap: FrameViewer code import javax.swing.JFrame; public class FrameViewer { public static void main(String[] args) { JFrame myframe = new JFrame(); // make a new JFrame object final int F_WIDTH = 300; // 300 pixels wide final int F_HEIGHT = 400; // 400 pixels high myframe.setSize(F_WIDTH, F_HEIGHT); myframe.setTitle("My Frame"); // this is optional myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); RectangleComponent component = new RectangleComponent(); myframe.add(component); myframe.setVisible(true); }

16 Recap: Here's what we drew > java FrameViewer

17 Graphical user interfaces (GUIs) The graphical user interface allows us to interact with our programs through mouse movements, button clicks, key presses, and so on. Your Windows or Macintosh operating system provides you with a GUI so you don't have to remember all sorts of instructions to type at the command line.

18 Graphical user interfaces (GUIs) The graphical user interface allows us to interact with our programs through mouse movements, button clicks, key presses, and so on. Your Windows or Macintosh operating system provides you with a GUI so you don't have to remember all sorts of instructions to type at the command line. Here's a GUI you've seen me use many times.

19 Event handling How do we make a GUI in Java? We install event listeners. An event listener is an object that belongs to a class which you define. The methods in your event listener contain the instructions to be executed when the events occur. Any event listener is specific to an event source. For example, you'd have one kind of event listener to respond to the click of a button on your mouse, and another to respond to the press of a key on your keyboard. When an event occurs, the event source calls the appropriate methods of all associated event listeners.

20 Event handling Here comes an example, straight from your book. This example is a simple program that prints a message when a button is clicked. An event listener that responds to button clicks must belong to a class that implements the ActionListener interface. That interface, supplied by the Abstract Windowing Toolkit (AWT), looks like this: public interface ActionListener { void actionPerformed(ActionEvent event); } Java uses the event parameter to pass details about the event. We don't need to worry about it.

21 Event handling Here's what our example class that implements the ActionListener interface looks like: import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("I was clicked."); } The actionPerformed() method contains the instructions we want to be executed when our button is clicked.

22 Event handling Next we'll see a program that tests our ClickListener class. It looks very much like the program we wrote earlier. First we create a frame window object so we have a place to put the button that we want to click.

23 Event handling import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionListener; public class ButtonTester { public static void main(String[] args) { JFrame myframe = new JFrame(); final int F_WIDTH = 100; final int F_HEIGHT = 60; myframe.setSize(F_WIDTH, F_HEIGHT); myframe.setTitle("Button Tester"); myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myframe.setVisible(true); }

24 Event handling Next we'll see a program that tests our ClickListener class. It looks very much like the program we wrote earlier. First we create a frame window object so we have a place to put the button that we want to click. Then we create a button object and add it to the frame, just like the rectangles before.

25 Event handling import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionListener; public class ButtonTester { public static void main(String[] args) { JFrame myframe = new JFrame(); final int F_WIDTH = 100; final int F_HEIGHT = 60; myframe.setSize(F_WIDTH, F_HEIGHT); myframe.setTitle("Button Tester"); myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Click me!"); myframe.add(button); myframe.setVisible(true); }

26 Event handling Next we'll see a program that tests our ClickListener class. It looks very much like the program we wrote earlier. First we create a frame window object so we have a place to put the button that we want to click. Then we create a button object and add it to the frame, just like the rectangles before. Finally we create an event listener object called ClickListener and attach it to the button we just made.

27 Event handling import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionListener; public class ButtonTester { public static void main(String[] args) { JFrame myframe = new JFrame(); final int F_WIDTH = 100; final int F_HEIGHT = 60; myframe.setSize(F_WIDTH, F_HEIGHT); myframe.setTitle("Button Tester"); myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Click me!"); myframe.add(button); ActionListener listener = new ClickListener(); button.addActionListener(listener); myframe.setVisible(true); }

28 Event handling > java ButtonTester

29 Event handling A button listener class like ClickListener is likely to be specific to a particular button, so we don't really need it to be widely accessible. We can put the class definition inside the method or class that needs it. So we can put this class: import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("I was clicked."); } inside the main method of the ButtonTester class as an inner class.

30 Event handling import javax.swing.JFrame; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; // note this addition public class ButtonTester2 { public static void main(String[] args) { JFrame myframe = new JFrame(); final int F_WIDTH = 100; final int F_HEIGHT = 60; myframe.setSize(F_WIDTH, F_HEIGHT); myframe.setTitle("Button Tester"); myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Click me!"); myframe.add(button);

31 Event handling class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("I was clicked."); } ActionListener listener = new ClickListener(); button.addActionListener(listener); myframe.setVisible(true); }

32 Making buttons do more This next example is from the book too, but I've changed the BankAccount class to the BunnyFamily class. Why? Because everybody likes bunnies. Let's say we want to compute the growth in the number of bunnies in my bunny family through successive clicks of a button. (OK, it's a stretch, but it's still better than the boring bank account example.) We'd start with a BunnyFamily class, of course. It has a method for retrieving the number of bunnies in the family, and another method for increasing the number of bunnies according to the Fibonacci numbers.

33 Fibonacci numbers Leonardo Pisano ( ), also known as Fibonacci, came up with a model of growth in an idealised bunny (really) population. Assuming that in the first month there is just one newly-born pair new-born pairs become fertile from their second month each month every fertile pair spawns a new pair, and the bunnies never die Then if we have A pairs of fertile and newly-born bunnies in month N and we have B pairs in month N+1, then in month N+2 we'll have A+B pairs.

34 Fibonacci numbers The numbers for our purposes are 2, 3, 5, 8, 13, 21, 34 and so on. Fibonacci was wrong about the growth of bunny populations, but his numbers live on in mathematical history.

35 Making buttons do more public class BunnyFamily { private int totalBunniesNow; private int totalBunniesLastTime; public BunnyFamily() { totalBunniesNow = 2; // first two numbers in the totalBunniesLastTime = 1; // Fibonacci sequence } public int getBunnies() { return totalBunniesNow; } public void updateBunnies() { totalBunniesNow = totalBunniesNow + totalBunniesLastTime; totalBunniesLastTime = totalBunniesNow - totalBunniesLastTime; }

36 Making buttons do more We start by importing everything but the proverbial kitchen sink. Then we create our frame window.

37 Making buttons do more import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class BunnyGrowthViewer { public static void main (String[] args) { JFrame frame = new JFrame(); frame.setSize(400, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Add Bunnies"); final BunnyFamily mybunnies = new BunnyFamily();

38 Making buttons do more We start by importing everything but the proverbial kitchen sink. Then we create our frame window. Next we create the button object.

39 Making buttons do more import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class BunnyGrowthViewer { public static void main (String[] args) { JFrame frame = new JFrame(); frame.setSize(400, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Add Bunnies"); final BunnyFamily mybunnies = new BunnyFamily();

40 Making buttons do more We start by importing everything but the proverbial kitchen sink. Then we create our frame window. Next we create the button object. Now we instantiate a BunnyFamily and call the object mybunnies. Why is it final? Because inner classes can access local variables from the surrounding scope only if the variables are final, and we're going to want to access some local variables from the surrounding scope inside the inner class. (Note that final doesn't keep the internal state of the mybunnies object from changing...it only means that once mybunnies holds a reference to a particular BunnyFamily object, mybunnies cannot then be assigned a different reference.)

41 Making buttons do more import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class BunnyGrowthViewer { public static void main (String[] args) { JFrame frame = new JFrame(); frame.setSize(400, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Add Bunnies"); final BunnyFamily mybunnies = new BunnyFamily();

42 Making buttons do more We need a user interface component that displays a message containing the current number of bunnies. Such a component is called a label. Here's how it's created...

43 Making buttons do more final JLabel label = new JLabel("bunnies = " + mybunnies.getBunnies()); JPanel panel = new JPanel(); panel.add(button); panel.add(label); frame.add(panel); class AddBunniesListener implements ActionListener { public void actionPerformed(ActionEvent event) { mybunnies.updateBunnies(); label.setText("bunnies = " + mybunnies.getBunnies()); } ActionListener listener = new AddBunniesListener(); button.addActionListener(listener); frame.setVisible(true); }

44 Making buttons do more We need a user interface component that displays a message containing the current number of bunnies. Such a component is called a label. Here's how it's created... We now want to put the button and label components in the frame, but Java will place one on top of the other. Instead, we create a panel object -- a panel is a container for other user interface components -- and then add the panel to the frame.

45 Making buttons do more final JLabel label = new JLabel("bunnies = " + mybunnies.getBunnies()); JPanel panel = new JPanel(); panel.add(button); panel.add(label); frame.add(panel); class AddBunniesListener implements ActionListener { public void actionPerformed(ActionEvent event) { mybunnies.updateBunnies(); label.setText("bunnies = " + mybunnies.getBunnies()); } ActionListener listener = new AddBunniesListener(); button.addActionListener(listener); frame.setVisible(true); }

46 Making buttons do more We need a user interface component that displays a message containing the current number of bunnies. Such a component is called a label. Here's how it's created... We now want to put the button and label components in the frame, but Java will place one on top of the other. Instead, we create a panel object -- a panel is a container for other user interface components -- and then add the panel to the frame. Next we define our specific event listener class.

47 Making buttons do more final JLabel label = new JLabel("bunnies = " + mybunnies.getBunnies()); JPanel panel = new JPanel(); panel.add(button); panel.add(label); frame.add(panel); class AddBunniesListener implements ActionListener { public void actionPerformed(ActionEvent event) { mybunnies.updateBunnies(); label.setText("bunnies = " + mybunnies.getBunnies()); } ActionListener listener = new AddBunniesListener(); button.addActionListener(listener); frame.setVisible(true); }

48 Making buttons do more We need a user interface component that displays a message containing the current number of bunnies. Such a component is called a label. Here's how it's created... We now want to put the button and label components in the frame, but Java will place one on top of the other. Instead, we create a panel object -- a panel is a container for other user interface components -- and then add the panel to the frame. Next we define our specific event listener class. Then we create an event listener object and associate it with the button. Finally, we make sure that everything is visible.

49 Making buttons do more final JLabel label = new JLabel("bunnies = " + mybunnies.getBunnies()); JPanel panel = new JPanel(); panel.add(button); panel.add(label); frame.add(panel); class AddBunniesListener implements ActionListener { public void actionPerformed(ActionEvent event) { mybunnies.updateBunnies(); label.setText("bunnies = " + mybunnies.getBunnies()); } ActionListener listener = new AddBunniesListener(); button.addActionListener(listener); frame.setVisible(true); }