For (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; } The for Statement.

Slides:



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

Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
TCU CoSc Programming with Java Handling Events.
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.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Graphical User Interfaces Lecture 25, Thu Apr
Event-Driven Programming Thus far, our programs have been executed one statement after the other However, many programs depend on user actions to dictate.
Event Handling Events and Listeners Timers and Animation.
Graphical User Interfaces
EVENTS CSC 171 FALL 2004 LECTURE 16. “Traditional” Input In console applications, user input is under control of the program The program asks the user.
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.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
GUIs Mimi Opkins CECS277.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
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.
Java GUI CSCE 190 – Java Instructor: Joel Gompert Mon, July 26, 2004.
Chapter 12 Event Handling. Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons,
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
Fall 2006Adapded from Java Concepts Companion Slides1 Event Handling Advanced Programming ICOM 4015 Lecture 13 Reading: Java Concepts Chapter 12.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Nine and Ten Graphics.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Layout Managers Arranges and lays out the GUI components on a container.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Introduction to GUI Programming with Java Graphical User Interfaces With AWT and Swing Towson University *Ref:
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
CMSC 341 Making Java GUIs Functional. 09/29/2007 CMSC 341 Events 2 More on Swing Great Swing demo at /demos/jfc/SwingSet2/SwingSet2Plugin.html.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
CHAPTER 10 EVENT HANDLING. CHAPTER GOALS To understand the Java event model To install mouse and action listeners To accept mouse and text input To display.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Event Handling and GUI Components.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Event Handling CS 21a: Introduction to Computing I First Semester,
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
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.
TENTH LECTURE Event and listener. Events and Listeners An event can be defined as a type of signal to the program that something has happened. The event.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Lecture 15 Basic GUI programming
Modular Event Handling
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Computer Science 209 Graphics and GUIs.
GUI III IS
GUI Programming III: Events
Event-driven programming for GUI
Chapter 12 Event Handling
Web Design & Development Lecture 12
Making Java GUIs Functional
Final project.
Presentation transcript:

for (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; } The for Statement

The body of the loop contains another loop ◦ Each time through the outer loop, the inner loop goes through its full set of iterations

X=6; while (x >=4) { y = 3; while (y <=6) { y++; System.out.println(y + “ “ + x); } x--; }

 Write a method to: ◦ Receive an int number in a method called count ◦ Using a for loop, print asterisks across the console, the number of asterisks to be the passed number

Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button (Buttons other objects generate events: Source ) Object e.g. button OS Interrupt Listener or middleman (interface) Generates an Event

Class InterfaceEvent Handler Methods (contains instructions to be executed when event occurs) Generated by event source ActionEventactionPerformed Button pressed JTextField-text changed JComboBox-alternative chosen ChangeEventstateChangedJComponent-state changed FocusEventfocusGained focusLost Component-gained or lost focus WindowEventWindow opened, closed, is closing, is iconified, or restored MouseEventmouseClicked mouseEntered mouseExited mousePressed mouseReleased mouseDragged mouseMoved Component Mouse Mouse pressed or released, mouse moved into or out of component Component moved or dragged

GUI Components and Screen Design GUI Demo Message Hello World CloseClearDisplay Panel Instance Textfield Instance Label Instance Button Instance Frame Instance

Building Applications with Buttons

JButton button = new JButton("Add Interest"); JLabel label = new JLabel("balance: " + account.getBalance()); JPanel panel = new JPanel() or extends JPanel; panel.add(button); panel.add(label); frame.add(panel); (or the class that extends a panel) frame.add (aClass);

class AddInterestListener implements ActionListener { public void actionPerformed(ActionEvent event) { double interest = account.getBalance() * INTEREST_RATE / 100; bank.deposit(interest); label.setText("balance=" + account.getBalance()); } } Building Applications with Buttons

1.Add the class within the original class where buttons are activated 2.Create a global listener reference: ActionListener listener; 3. In the constructor listener = new AddInterestListener (); 4.button1.addActionListener(listener ); this.add(button1); STEPS IN THE PROCESS

package investment; import javax.swing.Jframe; public class InvestmentViewer { public static void main (String [] args) { JFrame frame = new JFrame ("Investment Viewer"); frame.setSize(30, 200); Investment inv = new Investment (); frame.add (inv); frame.setDefaultCloseOperation(JFrame.EXIT_ ON_CLOSE); frame.setVisible(true); } InvestmentViewer.java (note changes from text)

package investment; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.Jpanel; public class Investment extends JPanel { private JButton button; private JLabel label, ansLabel; private BankAccount bank; private final int INTEREST_RATE = 7; ActionListener listener = new AddInterestListener (); public Investment () { bank = new BankAccount (); getPanelReady (); } public void getPanelReady () { button = new JButton ("Sample Button"); button.addActionListener(listener); label = new JLabel ("GJY"); ansLabel = new JLabel (); add(button); add(label); add(ansLabel); } class AddInterestListener implements ActionListener { public void actionPerformed (ActionEvent arg0) { double interest; interest = bank.getBalance () + (INTEREST_RATE / 100.0); ansLabel.setText("Balance: " + interest); }

LAB: (Homework through the week was: Chapter 9 : Exercise P14) Lab: P9. 15

public interface MouseListener { void mousePressed(MouseEvent event); // Called when a mouse button has been pressed on a component void mouseReleased(MouseEvent event); // Called when a mouse button has been released on a component void mouseClicked(MouseEvent event); // Called when the mouse has been clicked on a component void mouseEntered(MouseEvent event); // Called when the mouse enters a component Mouse Events

void mouseExited(MouseEvent event); // Called when the mouse exits a component } mousePressed, mouseReleased: called when a mouse button is pressed or released mouseClicked: if button is pressed and released in quick succession, and mouse hasn't moved mouseEntered, mouseExited: mouse has entered or exited the component's area Mouse Events

Add a mouse listener to a component by calling the addMouseListener method: public class MyMouseListener implements MouseListener { // Implements five methods } MouseListener listener = new MyMouseListener(); component.addMouseListener(listener); Sample program: enhance RectangleComponent – when user clicks on rectangle component, move the rectangle Mouse Events