Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

CIS 120 Final Exam 16 December Name (printed): Pennkey (login id):

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Lecture 19 GUI Events

Programming Languages and Techniques (CIS120e)

CIS 120 Final Exam 15 December 2017

CSE 331 Software Design & Implementation

Java Programming Lecture 6

Building custom components IAT351

CIS 120 Final Exam 15 December 2017 SOLUTIONS

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

ITEC 120 4/14/11. Review. Need. Objectives. GUI basics JFrame JPanel JLabel, JButton, JTextField Layout managers. Lecture 38 GUI Interactivity

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Programming Languages and Techniques (CIS120)

1. (5 points) In your own words, describe what an instance is.

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

Packages Inner Classes

Jonathan Aldrich Charlie Garrod

Lecture 5: Java Graphics

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

CS 11 java track: lecture 3

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

Charlie Garrod Michael Hilton

Programming Languages and Techniques (CIS120)

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Java: introduction to object-oriented features

Throughout the exam, write concisely and underline key words or phrases. Have fun! Exam 2. Week 7 (Winter 2013). Dr. Yoder. Sec 031.

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Swing: Building GUIs in Java

Object Orientation Fourth Story. Bok, Jong Soon

Java.net - the Source for Java(tm) Technology Collaboration

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

News and info. Array. Feedback. Lab 4 is due this week. It should be easy to change the size of the game grid.

Mobile Development Workshop DAY 2: INTRODUCTION TO JAVA

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Swing: Building GUIs in Java

Name: CSC143 Exam 1 1 CSC 143. Exam 1. Write also your name in the appropriate box of the scantron

Anonymous Classes. A short-cut for defining classes when you want to create only one object of the class.

AP CS Unit 11: Graphics and Events

C30b: Inner Class, Anonymous Class, and Lambda Expression

We are on the GUI fast track path

Mid-Semester Feedback

Charlie Garrod Bogdan Vasilescu

CS11 Java. Fall Lecture 3

Lecture 3: Java Graphics & Events

Today. cisc3120-fall2012-parsons-lectiii.3 2

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Systems Programming Graphical User Interfaces

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

CS11 Java. Fall Lecture 4

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Chapter 5: Enhancing Classes

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

User Interface Programming OOP/Java Primer. Step 4 Some Final Checkups

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

List ADT. Announcements. The List interface. Implementing the List ADT

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

*An nested class is a class that is defined inside another class.

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CS Exam 1 Review Suggestions

Fundamentals of Object Oriented Programming

Inheritance. Transitivity

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Class 16: The Swing Event Model

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Model-View Controller IAT351

5. In JAVA, is exception handling implicit or explicit or both. Explain with the help of example java programs. [16]

Introduction to Functional Programming in Java 8

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Human-Computer Interaction IS4300

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

MIT AITI Swing Event Model Lecture 17

CS 134 Programming Exercise 7:

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

2. (True/False) All methods in an interface must be declared public.

Announcements. Lab 03 inbase late submissions Midterm #1 Friday. Project 2 is posted. Covers everything through basics of OOP

Lecture 02, Fall 2018 Friday September 7

JavaScript: Sort of a Big Deal,

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

Swing from A to Z Some Simple Components. Preface

CSE 8B Intro to CS: Java

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Transcription:

Programming Languages and Techniques (CIS120) Lecture 35 November 29, 2017 Swing II: Building GUIs Inner Classes Chapter 29

Announcements Game Project Complete Code Due: December 11 th NO LATE SUBMISSIONS Grading will be by demo. Stay tuned for details!

Swing Java's GUI library

OnOff.java revisited public void run() { //... other code omitted LightBulb bulb = new LightBulb(); panel.add(bulb); JButton button = new JButton("On/Off"); panel.add(button); button.addactionlistener(new ButtonListener(bulb)); //... other code omitted

Too much boilerplate code ButtonListener really only needs to do bulb.flip() But we need all this boilerplate code to build the class Often we will only instantiate one instance of a given Listener class in a GUI class ButtonListener implements ActionListener { private LightBulb bulb; public ButtonListener (LightBulb b) { bulb = b; @Override public void actionperformed(actionevent e) { bulb.flip(); bulb.repaint();

Inner Classes

Inner Classes Useful in situations where two objects require deep access to each other s internals Replaces tangled workarounds like owner object Solution with inner classes is easier to read No need to allow public access to instance variables of outer class Also called dynamic nested classes

Basic Example Key idea: Classes can be members of other classes class Outer { private int outervar; public Outer () { outervar = 6; public class Inner { private int innervar; public Inner(int z) { innervar = outervar + z; public int getinnervar() { return innervar; Name of this class is Outer.Inner (which is also the static type of objects that this class creates) Reference from inner class to instance variable bound in outer class

Constructing Inner Class Objects Based on your understanding of the Java object model, which of the following make sense as ways to construct an object of an inner class type? 1. Outer.Inner obj = new Outer.Inner() 2. Outer.Inner obj = (new Outer()).new Inner(); 3. Outer.Inner obj = new Inner(); 4. Outer.Inner obj = Outer.Inner.new () Answer: 2 the inner class instances can refer to non-static fields of the outer class (even in the constructor), so the invocation of "new" must be relative to an existing instance of the Outer class.

Object Creation Inner classes can refer to the instance variables and methods of the outer class Inner class instances usually created by the methods/constructors of the outer class public Outer () { Inner b = new Inner (); Inner class instances cannot be created independently of a containing class instance. Outer.Inner b = new Outer.Inner() Outer a = new Outer(); Outer.Inner b = a.new Inner(); Actually this.new Outer.Inner b = (new Outer()).new Inner();

Anonymous Inner Classes Define a class and create an object from it all at once, inside a method quit.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { System.exit(0); ); Puts button action right with button definition line.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { ); shapes.add(new Line( )); canvas.repaint(); Can access fields and methods of outer class, as well as final local variables

Anonymous Inner class New expression form: define a class and create an object from it all at once New keyword new InterfaceOrClassName() { public void method1(int x) { // code for method1 public void method2(char y) { // code for method2 Normal class definition, no constructors allowed Static type of the expression is the Interface/superclass used to create it Dynamic class of the created object is anonymous! Can't refer to it.

Like first-class functions Anonymous inner classes are roughly* analogous to Ocaml s first-class functions Both create "delayed computation" that can be stored in a data structure and run later Code stored by the event / action listener Code only runs when the button is pressed Could run once, many times, or not at all Both sorts of computation can refer to variables in the current scope OCaml: Any available variable Java: only instance variables (fields) of enclosing object and local variables marked final *see next slide

Anonymous Lambdas Java 8 introduced "lambda expressions" which are simplified syntax for anonymous classes with "functional interfaces" interfaces with just one method persons.foreach(p -> p.setlastname("doe")) Wait until next week for more details

Paint Revisited Using Anonymous Inner Classes Refactoring for OO Design

What layout would you use for this app? What components would you use?

Canvas subclass of JPanel (canvas) JPanel (toolbar) JRadioButton (point, line) JCheckbox (thick) JButton (quit)

Paint demo (See PaintA.java PaintE.java)