More about GUIs GEEN163

Similar documents
PIC 20A GUI with swing

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

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

SampleApp.java. Page 1

Review for the Third Exam COMP163

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

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

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

AP CS Unit 12: Drawing and Mouse Events

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

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

AP CS Unit 11: Graphics and Events

Graphical User Interface

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

Section Basic graphics

To gain experience using GUI components and listeners.

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

TWO-DIMENSIONAL FIGURES

CompSci 230 S Programming Techniques. Basic GUI Components

Example: Building a Java GUI

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

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

Content Area: Mathematics Course: Computer Programming Grade Level: R14 The Seven Cs of Learning

MIT AITI Swing Event Model Lecture 17

Example: Building a Java GUI

Unit 7: Event driven programming

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

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

COMP16121 Sample Code Lecture 1

2IS45 Programming

Graphics User Defined Forms, Part I

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

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Practice Midterm 1. Problem Points Score TOTAL 50

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

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

JFrame & JLabel. By Iqtidar Ali

Object-Oriented Software Engineering Re-exam, 2012 (Also Object-Oriented Analysis, Design and Programming, Re-exam, 2012)

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Graphics JFrame. import java.awt.*; import javax.swing.*; public class XXX extends JFrame { public XXX() { ... main() public static

GUI Forms and Events, Part II

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

CSE115 Lab 4 Fall 2016

Java Interfaces Part 1 - Events Version 1.1

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

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Programming: You will have 6 files all need to be located in the dir. named PA4:

Dr. Hikmat A. M. AbdelJaber

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects

CHAPTER 2. Java Overview

Implementing Graphical User Interfaces

Computer Science 210: Data Structures. Intro to Java Graphics

Java Programming Lecture 6

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

Lecture 7 A First Graphic Program And Data Structures & Drawing

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

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

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!

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

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

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

public static void main(string[] args) { GTest mywindow = new GTest(); // Title This program creates the following window and makes it visible:

Control Statements: Part Pearson Education, Inc. All rights reserved.

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

CS 134 Programming Exercise 7:

CS 251 Intermediate Programming GUIs: Components and Layout

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

Final Exam CS 251, Intermediate Programming December 13, 2017

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

Lecture 5: Java Graphics

RAIK 183H Examination 2 Solution. November 10, 2014

CS180 Recitation. More about Objects and Methods

GUI Applications. Let s start with a simple Swing application in Java, and then we will look at the same application in Jython. See Listing 16-1.

CSCI 201L Midterm Written SOLUTION Fall % of course grade

We are on the GUI fast track path

Here is a list of a few of the components located in the AWT and Swing packages:

Answer on question #61311, Programming & Computer Science / Java

/* Write a Program implementing GUI based Calculator using Swing */

Introduction to the JAVA UI classes Advanced HCI IAT351

CS108, Stanford Handout #22. Thread 3 GUI

Swing from A to Z Some Simple Components. Preface

JFrame In Swing, a JFrame is similar to a window in your operating system

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

Dr. Hikmat A. M. AbdelJaber

CS 251 Intermediate Programming GUIs: Event Listeners

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

CS 170 Java Programming 1. Week 9: Learning about Loops

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.

Swing I CHAPTER EVENT-DRIVEN PROGRAMMING 921 Events and Listeners 921

MaSH Environment graphics

Swing I Event-Driven Programming Buttons, Events, and Other Swing Basics Containers and Layout Managers 946

Java GUI Design: the Basics

CSIS 10A Assignment 7 SOLUTIONS

9. APPLETS AND APPLICATIONS

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

Transcription:

More about GUIs GEEN163

The best programmers are not marginally better than merely good ones. They are an order-ofmagnitude better, measured by whatever standard: conceptual creativity, speed, ingenuity of design, or problem-solving ability. Randall E. Stross

Short Week There will be no class on: Wednesday, April 12 Honor's Convocation Friday, April 14 Good Friday

Quizzes Next Week There will be a quiz in recitation next week, either Monday, April 17 or Friday, April 21 There will be a lab quiz next week, Thursday, April 20

Interesting Objects Many GUI program can be created with a few simple components JLabel Displays text in the GUI JButton Creates a button you can press JTextField Creates an input text box All of these objects are from the package javax.swing

How do you make an array of 5 buttons for a GUI? A. JButton[5] gazelle = new JButton[]; B. JButton gazelle = new JButton(5); C. JButton[] gazelle = new JButton[5]; D. JButton[] gazelle = { JButton(), JButton(), JButton(), JButton(), JButton() }; E. You cannot make an array of GUI objects

Array of Objects If you create an array of objects, you need to create the objects in the array for (int cow = 0; cow < 5; cow++) { gazelle[cow] = new JButton("Go"); pane.add( gazelle[cow] ); }

Implementing ActionListener Implementing the ActionListener interface makes the program respond to GUI input Programs implementing ActionListener must have an actionperformed method public class MyApplet extends javax.swing.jframe implements java.awt.event.actionlistener {

Interfaces A Java interface has a list of method headers that a program must implement ActionListener is an interface that requires a program to include an actionperformed method Programmers can create their own interfaces This is a topic for GEEN165

Other Interfaces There are many interfaces in the standard Java libraries The Comparable interface requires implementing classes to have a compareto method public int compareto( MyClass other ) Returns a negative, zero, or positive integer as this object is less than, equal to, or greater than the other object

The Comparable interface is implemented by which class? A. Math B. Scanner C. String D. Only classes that you write

Generics Sometime when you create an object or interface in Java, you must specify the class it will work with When you used a HashTable you had to specify the type of the key and data Hashtable<String, WordData> dict = new Hashtable<>(18000); You must specify the type for Comparable public class Mine implements Comparable<Mine>

Adding a Listener The method addactionlistener should be called on each GUI component that can do something Do not call addactionlistener on components that do not do anything, like JLabels javax.swing.jbutton frog = new javax.swing.jbutton("go"); frog.addactionlistener( this );

Taking Action When a user clicks an object with an ActionListener, Java calls the method public void actionperformed( java.awt.event.actionevent rabbit) { } // do something here This method should do what the user expects to happen when the button is pressed

Getting the Text You can get the text from a GUI component with the gettext() method This is useful to get the text a person entered in the JTextField javax.swing.jtextfield llama = new javax.swing.jtextfield(); // in the actionperformed method String answer = llama.gettext();

Changing a Component s Text A useful method is settext It can be used to change the text of any object that has text, such as JButtons, JLabels and JTextfields javax.swing.jlabel msg = new javax.swing.jlabel("do something"); msg.settext("don t do something");

Who Dunit? Many GUIs have more than one active object You can determine where the action was taken by using the getsource() method of the event The result of getsource() should be compared to each GUI object public void actionperformed( java.awt.event.actionevent frog ) { if ( frog.getsource() == GUIobject ) {

Example Use of getsource() JButton delete = new JButton( kill ); JButton save = new JButton( allow ); - - - - - - - - - - - - - - - - - - - - - - - - - - - - public void actionperformed( java.awt.event.actionevent toad) { if (toad.getsource() == delete ) { } else { } // delete something // don t delete anything

Looping in GUI Programs When you want a user to enter multiple values in a GUI program, you probably do not need a for loop or a while loop Each time the user enters a name, it will call actionperformed

What will select the pressed button? JButton deer = new JButton(); public void actionperformed( java.awt.event.actionevent bear) A. if( bear == deer ) B. if( bear.getsource() == JButton ) C. if( bear.getsource() == deer ) D. if( deer == JButton.getSource() ) E. if( this == bear.getsource() )

setvisible Most GUI components have a setvisible method setvisible( boolean ) If the boolean parameter is true, the object shows, if false it does not show Calling setvisible(true) makes the frame appear onscreen

Disabling GUI Components Individual GUI components (i.e. JButtons) can be made invisible or disabled A disabled component is gray and cannot be selected JButton mybutton = new JButton("OK"); mybutton.setenabled(false); This will make the button gray and unusable

settitle The settitle method of a JFrame sets the text in the upper border of the window settitle("my GUI program");

pack The pack method sizes the frame so that all its contents are at their preferred sizes When you use the pack method, you do not have to call setsize for the frame If you use absolute positioning of components instead of a layout manager, you do not need to call pack

pack() Issues Using setsize and not pack() Using pack() Using pack() with spaces in JTextfields

Sample GUI Constructor JTextField instuff = new JTextField(" "); JButton thebutton = new JButton("OK"); public MyGUI( ) { java.awt.container pane = getcontentpane(); BoxLayout where = new BoxLayout(pane, BoxLayout.Y_AXIS); setlayout(where); pane.add(instuff); pane.add(thebutton); thebutton.addactionlistener(this); setdefaultcloseoperation(jframe.exit_on_close); setvisible(true); pack(); }

Method Review Follow the flow of execution. Execution starts in the main method Constructors can be used to initialize objects

How can you call the chew() method on all objects?? Assume you have an array cow that contains 12 objects of the class Bovin which has a method called chew() for (int i = 0; i < 12; i++) { A. cow.chew(i); B. cow.chew[i]; C. cow[i].chew(); D. cow.chew()[i] E. Bovine.chew(i)

Creating Arrays of Objects Often you will create an array and put the objects in the array as you create them Consider a program that will create an array of WordData objects using data from a file WordData[] centaur = new WordData[1000]; The array can hold 1000 objects, but it is initially empty with all elements equal to null

Example Class public class WordData { private String sphinx; private int count = 1; public WordData( String aword ) { sphinx = aword; } public String getword() { return sphinx; } public boolean match( String mermaid ) { return sphinx.equals( mermaid ); }

Putting Data in the Array WordData[] centaur = new WordData[1000]; int numobj = 0; java.io.file griffin = new java.io.file("stuff.txt"); Scanner dragon = new Scanner( griffin ); while ( dragon.hasnext() ) { String unicorn = dragon.next(); centaur[numobj] = new WordData(unicorn); numobj++; }

Incrementing in an Index WordData[] centaur = new WordData[1000]; int numobj = 0; java.io.file griffin = new java.io.file("stuff.txt"); Scanner dragon = new Scanner( griffin ); while ( dragon.hasnext() ) { String unicorn = dragon.next(); centaur[numobj++] = new WordData(unicorn); }

Finding Things in an Array Programs may need to search an array of objects for one that matches an instance variable If the data in the object is private, you will have to use a method to access it

Search Objects with Get String target = "special"; for (int roc = 0; roc < numobj; roc++) { if (target.equals(centaur[roc].getword()) { System.out.println("found it"); break; } }

Search Objects with match() String target = "special"; for (int roc = 0; roc < numobj; roc++) { if (centaur[roc].match( target )) { System.out.println("found it"); break; } }

Graphics Coordinates The screen has a coordinate system with the origin in the upper left corner Coordinates are given in pixels (Picture Elements) An X coordinate specifies the distance from the left edge A Y coordinate specified the distance from the top edge The screen size depends on the device

drawline The drawline method of java.awt.graphics objects draws a line from x 1,y 1 to x 2,y 2 bird.drawline(5, 3, 17, 23); The line is drawn using the current color 5 3 17 23

A. upper left corner C. upper right corner Location 0,0 is D. center B. lower left corner

drawrect The drawrect method of java.awt.graphics objects draws a rectangle of width and height whose upper left corner is x,y bird.drawrect( x, y, width, height); bird.drawrect( 5, 3, 10, 15 ); 5 3 15 10

fillrect The fillrect method of java.awt.graphics objects draws a rectangle of width and height whose upper left corner is x,y and fills it with the current color bird.fillrect( 5, 3, 10, 15 ); 5 3 15 10

Outside and Overlapping It is permissible to have a graphics object (such as a rectangle) extend outside the visible window The portion outside the window will not be visible An object can overlap one another bird.fillrect( 5, 3, 10, 15 ); bird.setcolor( Color.GREEN ); bird.fillrect( 8, 15, 10, 15 );

drawoval The drawoval method of java.awt.graphics objects draws a circle or oval to fit in a box of width and height whose upper left is x,y bird.drawoval( x, y, width, height ); bird.drawoval( 5, 3, 10, 15 ); 5 3 15 10

filloval filloval is just like drawoval, but it colors in the circle with the current color bird.filloval( 5, 3, 10, 15 ); 5 3 15 10

What is the output? public void paint(java.awt.graphics parrot) { parrot.setcolor(java.awt.color.black); parrot.drawrect( 0, 0, 100, 100); parrot.fillrect( 50, 0, 50, 50 ); } A. B. C. D.

drawpolygon The drawpolygon draws lines between the points specified by an array drawpolygon(int[] xpoints, int[] ypoints, int npoints) where there are npoints values in both arrays Lines are drawn from (xpoints[0], ypoints[0]) to (xpoints[1], ypoints[1]) and from there to (xpoints[2], ypoints[2]) and so forth A line is drawn between the last point and the first point

drawpolygon Example int[] xs = { 5, 12, 5}; int[] ys = { 7, 20, 20}; bird.drawpolygon( xs, ys, 3 ); 5, 7 5, 20 12, 20

fillpolygon Example int[] xs = { 5, 12, 5}; int[] ys = { 7, 20, 20}; bird.fillpolygon( xs, ys, 3 ); 5, 7 5, 20 12, 20

Short Week There will be no class on: Wednesday, April 12 Honor's Convocation Friday, April 14 Good Friday

Quizzes Next Week There will be a quiz in recitation next week, either Monday, April 17 or Friday, April 21 There will be a lab quiz next week, Thursday, April 20