Abstract, Interface, GUIs. Ch. 11 & 16

Similar documents
An example from Zoology

MIT AITI Swing Event Model Lecture 17

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Introduction. Introduction

Using Several Components

Person class. A class can be derived from an existing class by using the form

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

Person class. A class can be derived from an existing class by using the form

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

SampleApp.java. Page 1

SINGLE EVENT HANDLING

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

We are on the GUI fast track path

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

User interfaces and Swing

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

CSC 1214: Object-Oriented Programming

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Swing from A to Z Some Simple Components. Preface

Programming Language Concepts: Lecture 8

First Name: AITI 2004: Exam 2 July 19, 2004

Java. GUI building with the AWT

Programming Languages and Techniques (CIS120)

Graphical User Interfaces (GUIs)

Building Graphical User Interfaces. GUI Principles

Event Driven Programming

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphical User Interfaces

Window Interfaces Using Swing Objects

First Name: AITI 2004: Exam 2 July 19, 2004

Example: CharCheck. That s It??! What do you imagine happens after main() finishes?

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

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 :) )

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

GUI Forms and Events, Part II

H212 Introduction to Software Systems Honors

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 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!

AP CS Unit 11: Graphics and Events

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

RAIK 183H Examination 2 Solution. November 11, 2013

State Application Using MVC

Window Interfaces Using Swing Objects

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

EPITA Première Année Cycle Ingénieur. Atelier Java - J5

Original GUIs. IntroGUI 1

CMPS 12A Fall 2017 Final (A)

COMPSCI 230. Software Design and Construction. Swing

RAIK 183H Examination 2 Solution. November 10, 2014

Example 3-1. Password Validation

Building Graphical User Interfaces. Overview

Swing - JButton. Adding buttons to the main window

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09

Midterm assessment - MAKEUP Fall 2010

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Programming Languages and Techniques (CIS120e)

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Queens College, CUNY Department of Computer Science. CS 212 Object-Oriented Programming in Java Practice Exam 2. CS 212 Exam 2 Study Guide

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

CMPS12A Practice Final Fall 2017

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall (total 7 pages)

Final Examination Semester 2 / Year 2010

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

Swing/GUI Cheat Sheet

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

import javax.swing.*; import java.awt.*; import java.awt.event.*;

CSIS 10A Assignment 7 SOLUTIONS

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

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Packages: Putting Classes Together

Java Programming Lecture 6

Graphical User Interfaces. Comp 152

Swing Programming Example Number 2

Final Examination Semester 2 / Year 2011

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Programmierpraktikum

CS Exam 1 Review Suggestions

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

CS 251 Intermediate Programming GUIs: Event Listeners

Lecture 5: Java Graphics

Swing - JLabel. Adding a text (and HTML) labels to a GUI

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

To gain experience using GUI components and listeners.

CHAPTER 2. Java Overview

1 Looping Constructs (4 minutes, 2 points)

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Part I: Learn Common Graphics Components

DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

CMSC 132: Object-Oriented Programming II. Inheritance

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Swing - JTextField. Adding a text field to the main window (with tooltips and all)

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

Week 9. Abstract Classes

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

Transcription:

Abstract, Interface, GUIs Ch. 11 & 16

Abstract A class declared abstract cannot be instan:ated (we can t create an object of its type). A method declared abstract MUST be implemented if a class subclasses from abstract class. Useful when we know certain methods should be implemented, and we know how other methods should be implemented.

public abstract class Shape { private boolean issymmetric; public void setissymmetric(boolean s){ issymmetric = s; public boolean getissymmetric(){ return issymmetric; abstract double computearea();

public class Rectangle extends Shape { private Point lowerleft, upperright; Rectangle(Point lowerleft, Point upperright) { this.lowerleft = lowerleft; this.upperright = upperright; @Override public double computearea() { // computes and returns area of rectangle

public class Circle extends Shape { private double radius; private Point center; public Circle(Point center, double radius) { this.radius = radius; this.center = center; @Override public double computearea() { return (Math.PI * Math.pow(radius, 2));

public class PolymorphismExample { public static void main(string[] args) { Shape[] shapes = new Shape[2]; Circle circle = new Circle(new Point(0.0,0.0), 1.0); Rectangle rectangle = new Rectangle( shapes[0] = circle; shapes[1] = rectangle; new Point(0.0, 0.0), new Point(2.0, 2.0)); for (Shape shape : shapes) { System.out.println("Shape is: " + shape.getclass() + " and area is: " + shape.computearea());

Interface An interface is not a class. We cannot instan:ate interfaces. An interface specifies a set of methods that an implemen:ng class must override and define. A class implements an interface. Meaning the class must implement (override) all methods defined by interface.

Interface Why use interface? It separates what a class should do vs. how it should do it. Why not use abstract? Because a class can implement mul:ple interfaces. Something to note: instance variables cannot be declared in an interface.

interface Person { void setage(int age); void setgender(char gender); void setname(string name); int getage(); char getgender(); String getname();

interface Student extends Person { void setcollege(string college); void setgpa(double gpa); void setyear(byte year); String getcollege(); double getgpa(); byte getyear();

interface Staff extends Person { void setsalary(double salary); void setstartdate(date start); void setenddate(date end); void setssn(string ssn); double getsalary(); Date getstartdate(); Date getenddate(); String getssn();

class StudentEmployee implements Student, Staff { // methods required by Person public void setage(int age){...... public String getname(){... // methods required by Student public void setcollege(string college){...... public YearInSchool getyear(){... // methods required by Staff public void setsalary(double salary){...... public String getssn(){...

GUI GUI stands for Graphical User Interface. enables the user to interface with a program via the use of graphical components such as windows, buoons, text boxes, etc. as opposed to text-based interfaces like the tradi:onal command line.

import javax.swing.*; import java.awt.*; class SampleAWT { public static void main(string[] args) { JFrame frame = new JFrame("Sample GUI Components"); Container display = frame.getcontentpane(); display.setlayout(new FlowLayout()); display.add(new JLabel("What programming languages do you know?")); display.add(new JLabel("Check all that apply:")); display.add(new JCheckBox("Java",true)); display.add(new JCheckBox("C",false)); display.add(new JCheckBox("C++",false)); display.add(new JButton("Submit")); frame.pack(); frame.setvisible(true); System.out.println("The end of main().");

The Event Thread No:ce that although main() ends, the program is s:ll running. Crea:ng a JFrame, implicitly creates a separate thread of execu:on that enters an infinite loop, looking for events. This is called an event driven program. How do we get it to do something when an event occurs?

The Event Model Swing components are event sources - e.g. clicking a JButton creates an ActionEvent. To respond to an event you create an event listener, and tell the event source about the event listener. Event sources have methods like addsomethinglistener(), where Something is a par:cular event type.

import javax.swing.*; import java.awt.*; class SampleAWT2 { public static void main(string[] args) { JFrame frame = new JFrame("Sample GUI Components"); Container display = frame.getcontentpane(); // some lines deleted - same as before display.add(new JCheckBox("C++",false)); JButton submit = new JButton("Submit"); display.add(submit); submit.addactionlistener(new Submit()); frame.pack(); frame.setvisible(true); System.out.println("The end of main().");

import java.awt.event.*; class Submit implements ActionListener { public void actionperformed(actionevent e) { System.out.println("Submitted");

import javax.swing.*; import java.awt.*; class SampleAWT2 { public static void main(string[] args) { JFrame frame = new JFrame("Sample GUI Components"); Container display = frame.getcontentpane(); // some lines deleted - same as before display.add(new JCheckBox("C++",false)); JButton submit; display.add(submit = new JButton("Submit")); submit.addactionlistener(new Submit()); frame.addwindowlistener(new Exiter()); frame.pack(); frame.show(); System.out.println("The end of main().");

Listening to a JButton Create the buoon with new JButton() Get the Container for the JFrame using getcontentpane(). Add the buoon to the content pane using add(). Create an ActionEventListener by adding implements ActionEventListener to the class declara:on and defining an actionperformed() method. Add the listener object to the list of listeners for the buoon by calling button.addactionlistener(listener)

//TextInput.java import java.awt.*; import javax.swing.*; class TextInput { public static void main(string[] args) { JFrame frame = new JFrame("TextInput"); Container pane = frame.getcontentpane(); JTextField input = new JTextField( Echo listener = new Echo(); "Edit this text then hit <return>"); input.addactionlistener(listener); pane.add(input); frame.pack(); frame.show();

//Echo.java import javax.swing.*; import java.awt.event.*; class Echo implements ActionListener { public void actionperformed(actionevent e) { JTextField source = (JTextField)e.getSource(); String text = source.gettext(); System.out.println(text);

//TextInput.java import java.awt.*; import javax.swing.*; What does hixng <return> do here? class TextInput { public static void main(string[] args) { JFrame frame = new JFrame("TextInput"); Container pane = frame.getcontentpane(); JTextField input = new JTextField("Edit this text then hit <return>"); Echo listener = new Echo(); input.addactionlistener(listener); input.addactionlistener(new Submit()); pane.add(input); frame.pack(); frame.show();