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

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

Introduction. Introduction

Agenda. Container and Component

Window Interfaces Using Swing Objects

Graphical User Interfaces. Comp 152

Part I: Learn Common Graphics Components

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Graphical User Interfaces. Swing. Jose Jesus García Rueda

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

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

Window Interfaces Using Swing. Chapter 12

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Window Interfaces Using Swing Objects

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!

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

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

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

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

Java. GUI building with the AWT

Graphical User Interfaces in Java - SWING

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

To gain experience using GUI components and listeners.

Systems Programming Graphical User Interfaces

Dr. Hikmat A. M. AbdelJaber

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

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

1.00/ Introduction to Computers and Engineering Problem Solving. Quiz 2 / November 5, 2004

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

We are on the GUI fast track path

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

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

Swing/GUI Cheat Sheet

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

Graphical User Interfaces

CS 2113 Software Engineering

Points Missed on Page page 1 of 8

Java Programming Lecture 6

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

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

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

Java IDE Programming-I

Programming graphics

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

CSIS 10A Assignment 7 SOLUTIONS

2110: GUIS: Graphical User Interfaces

Graphical User Interfaces (GUIs)

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

Class 16: The Swing Event Model

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

2IS45 Programming

Introduction to the JAVA UI classes Advanced HCI IAT351

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

PIC 20A GUI with swing

Chapter 7: A First Look at GUI Applications

Containers and Components

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

Part 3: Graphical User Interface (GUI) & Java Applets

COMP16121 Sample Code Lecture 1

Graphical User Interfaces in Java

Object-Oriented Programming Design. Topic : User Interface Components with Swing GUI Part III

Graphical User Interface (GUI)

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

CS 251 Intermediate Programming GUIs: Event Listeners

Unit 6: Graphical User Interface

User interfaces and Swing

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Chapter 6: Graphical User Interfaces

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

JAVA NOTES GRAPHICAL USER INTERFACES

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

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

Laying Out Components. What is Widget Layout?

CS 180 Fall 2006 Exam II

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

CHAPTER 2. Java Overview

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

Java for Interfaces and Networks (DT3010, HT10)

encompass a group of features for building Graphical User Interfaces (GUI).

AP CS Unit 11: Graphics and Events

Chapter 12 Advanced GUIs and Graphics

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

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

Example: Building a Java GUI

MIT AITI Swing Event Model Lecture 17

Java for Interfaces and Networks (DT3029)

Example: Building a Java GUI

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

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

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

This exam is closed textbook(s) and closed notes. Use of any electronic device (e.g., for computing and/or communicating) is NOT permitted.

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

Together, the appearance and how user interacts with the program are known as the program look and feel.

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

EVENTS, EVENT SOURCES AND LISTENERS

Transcription:

Parts of a Contract Syntax - Method signature Method name Parameter list Return type Semantics - Comments Preconditions: requirements placed on the caller Postconditions: what the method modifies and/or returns 1 Contract Example Postcondition! /**! * Create a rectangle of a particular size.! * @param width the width of the rectangle. * This must be > 0.! * @param height the height of the rectangle. * This must be > 0.! */! public Rectangle(double width, double height) { Preconditions Signature 2 Interface as a Contract /** A 2-dimensional shape */ interface Shape { /** Returns the area of the shape. @return the area of the shape. */ public double getarea(); 3

The Supplier Side of the Contract public class Rectangle implements Shape { private double width; private double height; public Rectangle(double width, double height) { this.width = width; this.height = height; public double getarea() { return width*height; Must define this to satisfy the contract. 4 The Client Side of the Contract Shape someshape; Can use interface names as types someshape = new Rectangle (3, 5); double area = someshape.getarea(); Can assign instance of implementing classes to variables declared with interface types 5 The Client Side of the Contract Shape someshape; someshape = new Rectangle (3, 5); double area = someshape.getarea(); Can call methods declared in the interface. 6

Polymorphism public class ShapeArray {! private Shape[] shapes = new Shape[10];! private int nextindex = 0;!! public void addshape (Shape newshape) {!! if (nextindex < shapes.length) {!!! shapes[nextindex] = newshape;!!! nextindex++;!!!!! public double getarea (int index) {!! if (index >= 0 && index < nextindex) {!!! return shapes[index].getarea();!!!! return -1;! Which entries in the array are circles??? 7 Swing Package used to create Graphical User Interfaces (GUIs) in Java JButton JCheckBox JTextField JComboBox JSlider JLabel 8 Displaying Buttons in Swing Construct a JButton, passing in its label JButton click = new JButton ( Click me! ); Add the button to a panel JPanel buttonpanel = new JPanel(); buttonpanel.add (click); Add the panel to the display, no need to specify size or exact location getcontentpane().add (buttonpanel, BorderLayout.CENTER); 9

ActionListener Interface public interface ActionListener { public void actionperformed (ActionEvent e); actionperformed is the method that Java calls when the user clicks on a JButton. To handle button clicks, you must do 3 things: Have your class implement ActionListener Define the actionperformed method Attach your ActionListener object to the JButton: mybutton.addactionlistener (this); 10 ActionListener Contract 11 Handling Button Clicks Tell Java where to find the event handler click.addactionlistener (this); Define the event handler public class ClickMe implements ActionListener public void actionperformed (ActionEvent event) { counter++; counterlabel.settext("" + counter); 12

ActionListener Contract Your side of the contract: Attach a listener to your button implement ActionListener Define the actionperformed method Java s side of the contract: Java will call your actionperformed method when the user clicks the button. 13 Swing Events Every event handling method has a parameter describing the event that occurred. The most useful method for us is: public Object getsource() This returns the Swing component that the user interacted with. Useful if an event handling method handles events for more than 1 object. 14 BorderLayout Rules One component per compass point Component expands to fill the entire area 15

BorderLayout NORTH WEST EAST SOUTH BorderLayout lays out components using compass directions + CENTER Default for a JFrame s content pane. 16 What Gives??? EAST There are 2 buttons in the EAST! The buttons do not fill the entire east area! Conclusion: My professor lied to me! 17 More on add ing components Adding a button to a panel: JPanel buttonpanel = new JPanel(); buttonpanel.add (clickbutton); JPanel lays out its components like words on a page Adding a panel to the frame: getcontentpane().add (buttonpanel, BorderLayout.CENTER); contentpane lays out its components using compass directions: NORTH, SOUTH, EAST, WEST and CENTER 18

Grouping Components with JPanel JPanel buttonpanel = new JPanel(); buttonpanel.add (new JButton("Place order")); buttonpanel.add (new JButton("Cancel order")); contentpane.add(buttonpanel, BorderLayout.EAST); 19 FlowLayout A JPanel uses FlowLayout as its layout manager rather than BorderLayout. With BorderLayout, we say: contentpane.add (buttonpanel, BorderLayout.SOUTH); With FlowLayout, we omit the compass direction: buttonpanel.add(orderbutton); Components are added from left to right. 20 FlowLayout buttonpanel.add (orderbutton); buttonpanel.add (cancelbutton); There are 2 buttons in the button panel. There is 1 button panel in the EAST. FlowLayout sizes components in a more natural way. Conclusion: My professor didn t lie to me! 21

JPanel gives natural size contentpane.add(combobox, BorderLayout.SOUTH); toppingspanel.add (combobox); contentpane.add(toppingspanel, BorderLayout.SOUTH); 22