Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.

Slides:



Advertisements
Similar presentations
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Advertisements

Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Introduction to Java Applet Programs Section 1.3Intro. to Java Applet Programs: a Greeter Applet Section 3.7Graphical/Internet Java: Applet: An Einstein.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Java Programming, Second Edition Chapter Five Input and Selection.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
GUI programming Graphical user interface-based programming.
Dale Roberts GUI Programming using Java - Introduction Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
FEN IntroJava2006 AAU1 GUI: Graphical User Interface AWT/SWING: Components Drag and Drop in NetBeans Events Listeners.
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.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
CS Lecture 00 Swing overview and introduction Lynda Thomas
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Introduction to Java Applet Programs Section 1.3Intro. to Java Applet Programs: a Greeter Applet Section 3.7Graphical/Internet Java: Applet: An Einstein.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Events Programming with Alice and Java First Edition by John Lewis.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Basics of GUI Programming Chapter 11 and Chapter 22.
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.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
JOptionPane Class JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. While the JOptionPane.
SE-1020 Dr. Mark L. Hornick 1 Graphical User Interfaces.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
1 IM103 week 10 Enhancing the user interface By the end of lecture you should be able to:  distinguish between heavyweight and lightweight components;
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Lesson 28: More on the GUI button, frame and actions.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
A Quick Java Swing Tutorial
A First Look at GUI Applications
Ellen Walker Hiram College
Message, Input, Confirm, and Specialized Dialogs
Timer class and inner classes
CS18000: Problem Solving and Object-Oriented Programming
Message, Input, and Confirm Dialogs
Steps to Creating a GUI Interface
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
CS431 ws99 Half Text Half Graphics
JOptionPane class.
Presentation transcript:

Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation

1980 The first GUI interface Developed at Xerox PARC by a group which included Kay. The key ideas are based on child psychology Alan Kay Alto "Only people born before a technology is invented think of it as a technology" from

1984 The first commercially successful GUI OS Developed at Apple by a group that included Jobs. Based on the Xerox PARC Alto Steven Jobs (1955- ) MacOS from

Example: Building a Temperature-Conversion GUI (similar to Einstein Calculator in §3.7) 4 Problem Scenario: Write a program to read a temperature in Celsius, compute and display the equivalent Fahrenheit temperature. However, instead of the text-based solution from before, use a graphical user interface. Similar to GUI Lab #1

Models of Programming Data-Driven Limited interaction with the user Activity is initiated by the program: 1. input data 2. process the data 3. output the results Single thread of execution. Event-Driven Continuous interaction with the user Activity is initiated by user events: 1. user initiates events 2. system responds Can have multiple threads of execution doing different things simultaneously. One thread can process events, others can generate events, others can do computations.

Kinds of User Interfaces Textual Uses text and keyboard Driven by: – text input prompts – command-line interfaces Relatively easy to program Graphical Uses pictures and mouse (in addition to text & keyboard) Driven by user-initiated graphical events, e.g., – pressing mouse button – releasing mouse button – dragging the mouse – pressing a keyboard key Requires more programming (mostly using libraries)

Open a window containing: –a prompt for a Celsius temperature –a box to enter the temperature –OK and Cancel buttons Let the user enter a Celsius temperature and then click OK (or press Enter key) Compute the equivalent Fahrenheit temperature Open a second window that displays: –the Fahrenheit temperature –a button for terminating the program 7 Behavior of our Temperature-Converter GUI The program will have a graphical user interface that will:

Transition Diagrams : Graphical behavior is frequently modeled with state transition diagrams that show the various states of execution and what causes transitions from one state to another. 8 Please enter the temperature in Celsius: Cancel OK Please enter the temperature in Celsius: Cancel OK degrees Celsius is 74.3 degrees Fahrenheit. It's been a pleasure! OK User clicks in box and enters the temperature value User clicks OK box or presses Enter Stop Execution User clicks OK box or presses Enter

9 Abstract Windowing ToolkitAWT Java's first GUI-building library; still used directly and indirectly A package java.awt of classes — e.g., Component and Applet — for building widgets. Widgets Objects ( ): In addition to the temperature variables and constants from our earlier text-based version, we obviously need new graphical objects, usually called widgets, to build the GUI — windows, buttons, menus, etc.

10 Java Foundation Classes JFC Extension to Java in 1997; now a standard part of Java 2 (JDK1.3), which still supports AWT, but... JFC has a collection of Swing components for enhanced GUIS that should be used whenever possible: –more powerful –easier to use – large collection, but most useful are those in javax.swing package –Names all begin with 'J'

11 Some of the more commonly-used Swing classes: JFrame To create a main frame (window) for a GUI JApplet Like JFrame, but for applets JOptionPane To implement pop-up input or output dialog boxes JLabel To hold a short one-line message or an image JTextArea To hold multi-line messages JPanel To create panes (or panels) to put on a frame JButton To create buttons JMenu To create menus JFileChooser A simple way for a user to choose a file See the "How to Make Dialogs" section of Java's Swing Tutorial: SwingSet

/** GUITemperature.java converts Celsius temperatures to * Fahrenheit. It uses a graphical user interface to * interact with the user. * Author: L. Nyhoff * Date: Nov. 29, 2002 */ import javax.swing.*; class GUITemperature extends Object { public static void main(String [] args) { //--- Get Celsius temperature double fahrenheit = ((9.0/5.0)*celsius) + 32; //--- Output Fahrenheit temperature } Building our GUI Temperature Converter 12

13 Since we need a widget for creating pop-up input and output windows, we will use JOptionPane. Some of its most useful methods are the following; all are class (static) methods: showInputDialog() Prompt for input showMessageDialog() Display message to user showConfirmDialog() Ask user to confirm some- thing, usually with Yes, No, or Cancel showOptionDialog() "a grand unification of the above three" (from Java's API doc.)

14 Input Dialogs: Used to get text input from user Simple form showInputDialog(prompt) Returns a String The prompt can be a String, a graphic image, or another Java Object. So we can implement the first state in our program:

/** GUITemperature.java... */ import javax.swing.*; class GUITemperature extends Object { public static void main(String [] args) { //--- Get Celsius temperature double fahrenheit = ((9.0/5.0)*celsius) + 32; //--- Output Fahrenheit temperature } 15 double celsius = Double.parseDouble(celsiusString); JOptionPane.showInputDialog( "Please enter the temperature in Celsius: ") String celsiusString =

16 Message Dialogs: Used to display information to the user Simple form showMessageDialog(null, message) A void method The message is a String or other Java Object The first parameter refers to the frame in which the message is to be displayed; null causes a default frame to be used. So we can implement the last state in our program:

/** GUITemperature.java... */ import javax.swing.*; class GUITemperature extends Object { public static void main(String [] args) { //--- Get Celsius temperature String celsiusString = JOptionPane.showInputDialog( "Please enter the temperature in Celsius: "); double celsius = Double.parseDouble(celsiusString); double fahrenheit = ((9.0/5.0)*celsius) + 32; //--- Output Fahrenheit temperature } 17 JOptionPane.showMessageDialog(null, celsius + " degrees Celsius is " + fahrenheit + " degrees Fahrenheit.\nIt's been a pleasure!\n"); System.exit(1);

We added the line System.exit(1); to ensure that the application stops running when the OK button is clicked. Execution: 18

19 Some Improvements: Give each dialog box a meaningful title and appropriate symbol for type of dialog: String TITLE = "Celsius-to-Fahrenheit Conversion"; String celsiusString = JOptionPane.showInputDialog( null, "Please enter the temperature in Celsius: ", TITLE, JOptionPane.QUESTION_MESSAGE); JOptionPane.showMessageDialog(null, celsius + " degrees Celsius is " + fahrenheit + " degrees Fahrenheit.\nIt's been a pleasure!\n", TITLE, JOptionPane.PLAIN_MESSAGE);

20

21 Allow repeated conversions: do { String celsiusString = JOptionPane.showInputDialog( null, "Please enter the temperature in Celsius: ", TITLE, JOptionPane.QUESTION_MESSAGE); double celsius = Double.parseDouble(celsiusString); double fahrenheit = ((9.0/5.0)*celsius) + 32; JOptionPane.showMessageDialog(null, celsius + " degrees Celsius is " + fahrenheit + " degrees Fahrenheit.\nIt's been a pleasure!\n", TITLE, JOptionPane.PLAIN_MESSAGE); } while (JOptionPane.showConfirmDialog(null, "More values to convert? ") == JOptionPane.YES_OPTION);

22