Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.

Slides:



Advertisements
Similar presentations
Programming in Java; Instructor:John Punin Graphics and Graphical User Interfaces1 Programming in Java Graphics and Graphical User Interfaces.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Event Handling Events and Listeners Timers and Animation.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and Arrays in Java Outline 25.1Introduction 25.2Primitive.
Unit 12 Object-oriented programming: Event-driven programming for GUI Jin Sa.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 121 Window Interfaces Using Swing Chapter 12.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java I--Copyright © 2000 Tom Hunter. Chapter 6 Methods: Part II.
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,
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Chapter 6 - Methods Outline 6.1Introduction 6.3 Math Class Methods 6.4Methods 6.5Method Definitions 6.6Java API Packages 6.7Random Number Generation 6.8Example:
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Alice in Action with Java Chapter 14 Events and GUIs.
Layout Managers Arranges and lays out the GUI components on a container.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
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.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1Introduction 6.2Program Modules in Java 6.3 Math Class Methods 6.4Methods.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Event Handling CS 21a: Introduction to Computing I First Semester,
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
GUIs & Event-Driven Programming Chapter 11 Review.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
A Quick Java Swing Tutorial
Modular Event Handling
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
CompSci 230 S Programming Techniques
Graphical User Interface (pronounced "gooey")
Ellen Walker Hiram College
Event-driven programming for GUI
Introduction to Event Handling
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for a user at a time.  A message dialog can display only one message.  More Common:  Multiple inputs from user at once (name, addr, etc)  Display many values (values of dice, etc).  Use Java's API, Example:  Attach JLabel's to an Application's content pane  use ActionListeners and corresponding actionPerformed

Deitel Ch 11-part 1 Java GUIs 2 Example: Craps Application (adapted from Fig 6.8) 76 public class Craps extends JFrame implements 76 public class Craps extends JFrame implements ActionListener { ActionListener {  Although Java supports only single inheritance, a Java class can implement multiple interfaces:  Extends: Craps inherits (data&methods) from JFrame  Implements: Craps implements ALL the behaviors specified in ActionListener: ActionListener specifies an actionPerformed method, Craps must define an actionPerformed method (otherwise, Craps would be abstract)

Deitel Ch 11-part 1 Java GUIs 3 *Craps example uses Java API components:

Deitel Ch 11-part 1 Java GUIs 4 FlowLayout: Simplest Layout Manager  Method init() (line 106) from Craps example creates GUI component objects and attaches them to a content pane (instance of Container):  GUI components are placed on Container left to right in the order in which attached to Container.  Wrap to next line when edge reached.  Layout set before any components attached to a Container  Other layout managers:  Border (N,S,E,W, Center) and,  Grid (like a 2-D array).  GridBagLayout (most flexible/complex layout manager)

Deitel Ch 11-part 1 Java GUIs 5 Init() method that uses Flow Layout public void init(){ public void init(){ Container c = getContentPane(); Container c = getContentPane(); c.setLayout( new FlowLayout() ); c.setLayout( new FlowLayout() ); setTitle("The Sum of all Craps"); setTitle("The Sum of all Craps");//110 die1Label = new JLabel( "Die 1" ); die1Label = new JLabel( "Die 1" ); c.add( die1Label ); c.add( die1Label ); firstDie = new JTextField( 5 ); firstDie = new JTextField( 5 ); firstDie.setEditable( false ); firstDie.setEditable( false ); c.add( firstDie ); c.add( firstDie );  init() creates and attaches GUI components to the user interface  107: c assigned reference to this JFrame’s contentPane  115: c.add() adds component to contentPane as per layout manager.

Deitel Ch 11-part 1 Java GUIs 6 Event Driven Programming  Java's use of actionListeners is known as event-driven programming:  User's interaction with GUI "drives" the program  Event handling methods: methods called when an event occurs.  When a Java GUI event occurs: JVM creates object with info about event that occurred program implicitly calls event handling method addActionListener method tells application to listen for action events to respond to an action event, have to define a class that implements ActionListener

Deitel Ch 11-part 1 Java GUIs 7 Registering an event handler. //145 roll = new JButton( "Roll Dice" ); //145 roll = new JButton( "Roll Dice" ); roll.addActionListener( this ); roll.addActionListener( this ); c.add( roll ); c.add( roll );  Line 146 registers event handler, specifies that "this" JFrame instance listens for events from JButton "roll". If roll button is pressed by user at runtime:  an ActionEvent instance is created  indicates to JFrame that an action was performed by user on the JButton  program implicitly calls actionPerformed method to process user's interaction, passing the ActionEvent as an argument

Deitel Ch 11-part 1 Java GUIs 8 actionPerformed() (implicitly invoked) //166 call method play when button is pressed public void actionPerformed( ActionEvent e ){ public void actionPerformed( ActionEvent e ){ if (e.getSource()== roll) play(); if (e.getSource()== roll) play(); }  JVM implicitly calls actionPerformed method when user presses "roll" button (causing event to occur)  actionPerformed is required to be implemented by Craps class since ActionListener is implemented. //207 public void play() { if ( firstRoll ) { // first roll of the dice if ( firstRoll ) { // first roll of the dice sumOfDice = rollDice(); sumOfDice = rollDice();  play() explicitly called from actionPerformed

Deitel Ch 11-part 1 Java GUIs 9 rollDice() //265 roll the dice public int rollDice(){ public int rollDice(){ int die1, die2, workSum; int die1, die2, workSum; die1 = 1 + ( int ) ( Math.random() * 6 ); die1 = 1 + ( int ) ( Math.random() * 6 ); die2 = 1 + ( int ) ( Math.random() * 6 ); die2 = 1 + ( int ) ( Math.random() * 6 ); workSum = die1 + die2; workSum = die1 + die2; firstDie.setText( Integer.toString( die1 ) ); firstDie.setText( Integer.toString( die1 ) ); secondDie.setText( Integer.toString( die2 ) ); secondDie.setText( Integer.toString( die2 ) ); sum.setText( Integer.toString( workSum ) ); sum.setText( Integer.toString( workSum ) ); return workSum; return workSum; }  rollDice(), explicitly called from play(), simulates rolling of two dice, updates GUI textfields and returns to play()  play() completes, and program waits for user to press roll again