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.

Slides:



Advertisements
Similar presentations
Fondamenti di Java Introduzione alla costruzione di GUI (graphic user interface)
Advertisements

1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Web Design & Development Lecture 19. Java Graphics 2.
Graphics and event-driven programs Learning objectives By the end of this lecture you should be able to: identify and use some of the common components.
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Graphical User Interfaces Java’s AWT and Swing APIs.
Graphical User Interfaces (Part IV)
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
Drawing in a frame – Java GUI
CS18000: Problem Solving and Object-Oriented Programming.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
F27SB2 Programming Languages
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
TCU CoSc Programming with Java Handling Events.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
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)
Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
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.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Java Programming Chapter 10 Graphical User Interfaces.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
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,
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
Alice in Action with Java Chapter 14 Events and GUIs.
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.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Review_6 AWT, Swing, ActionListener, and Graphics.
GUI Components. The Swing package has numerous GUI components that can be added to a window. The Swing package has numerous GUI components that can be.
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 Outline 27.1 Test-Driving the Drawing Shapes Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
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,
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
©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.
Modular Event Handling
CompSci 230 S Programming Techniques
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Java Programming: From Problem Analysis to Program Design,
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:

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 programming works How classes inherit methods Overall program structure

Major Example

Red colorMe GreenBlue darkerreSize

RedGreenBlue Each color is mixture of amounts of Red, Green, and Blue ( 200, ) values in range: 0 … 255

Illustrate with Rectangle object Class definition of rectangle including constructor, properties, methods Test program that creates instance of rectangle Classes or Objects in Java

Class Rectangle int height, width ; { } public Rectangle (int h, int w ) { } height = h; width = w; int findArea ( ) { } return height * width ; instance variables or properties constructor Class blueprint int getHght ( ) { return height } methods

public class TestRectangle { } public static void main ( String [ ] args ) { } Rectangle r ; r = new Rectangle (2, 4) ; int area = r.findArea ( ); System.out.println ( "Area is: " + area ) Test program instance r of rectangle saved in file TestRectangle say: r’s findArea constructor

public class Rectangle { } public static void main ( String [ ] args ) { } Rectangle r ; r = new Rectangle (2, 4) ; int area = r.findArea ( ); System.out.println ( "Area is: " + area ) ; another instance System.out.println ( "Area is: " + s.findArea ( ) ); Rectangle s = new Rectangle (5, 10) ;

Major Example

Red colorMe GreenBlue darkerreSize object Program Design

Design object GUI components Recognize & handle events Make Test program to create instances

GUI design Build object on top of frame class Add panel to frame Put GUI elements in frame: Add labels, textfields, & buttons Make object listen for clicks on buttons inherit properties of frames

Red colorMe GreenBlue darkerreSize object labelsbuttons text fields panel

Event-handler design Recognize source of event Take appropriate action - grab data from textfield - convert data to integer - use value to make RGB color - RGB: ( r, g, b ) - change color of panel Asynchronously called

Program Structure for Demo

import java.awt.*; public class TestColorizer { } public static void main (String[ ] args) { … } class Colorizer { }{ } public void actionPerformed(ActionEvent e) { … } public Colorizer( ) { … } private JTextField Num1 … ; implements ActionListener extends JFrame Program Structure

class Colorizer { }{ } implements ActionListener extends JFrame private JTextField Num1 … ; Instance variables public Colorizer( ) { … } constructor public void actionPerformed(ActionEvent e) { … } Event-handler Class

Content Pane X title bar JFrame

X Red colorMe GreenBlue darkerreSize Colorizer object extends Jframe

JFrame Title Bar Content PanePanel JTextFieldJLabelJButton

public Colorizer ( ) { } p1 = new JPanel ( ); this.getContentPane().add(p1); p1.add(new JLabel(“Red")); p1.add(Num1 = new JTextField(3 )); p1.add(colorMe = new JButton("ColorMe")); colorMe.addActionListener(this ); objects Constructor

public Colorizer ( ) { } p1 = new JPanel ( ); this.getContentPane().add(p1); p1.add(new JLabel(“Red")); p1.add(Num1 = new JTextField(3 )); p1.add(colorMe = new JButton("ColorMe")); colorMe.addActionListener(this ); named so can refer to in event-handler Constructor un-named since not referred to in event- handler system listens for button click

class Colorizer { }{ } public Colorizer( ) { … } private JTextField Num1 ; implements ActionListener extends JFrame private JButton colorMe ; private JPanel p1 ; public void actionPerformed(ActionEvent e) { … } instance variables Class…again

if (e.getSource() == colorMe ) { } public void actionPerformed (ActionEvent e) { } p1.setBackground ( c ); Integer.parseInt ( );int R =Num1.getText( ) recognize event source Get & convert number c = new Color (R, 100, 150); Create color Event-handler method signature

import java.awt.*; public class TestColorizer { } public static void main (String[ ] args) { } Colorizer f = new Colorizer( ); f.setSize (400,400); f.setVisible(true); import javax.swing.*; import java.awt.event.*; create instance of object use inherited methods Test program

public class TestColorizer { } public static void main (String[ ] args) { } Colorizer f = new Colorizer( ); f.setSize (400,400); f.setVisible(true); import java.awt.*; import javax.swing.*; import java.awt.event.*; { }{ } class Colorizer implements ActionListener extends JFrame if (e.getSource() == colorMe ) { } c = new Color (R, 100, 150); p1.setBackground ( c ); Integer.parseInt ( );int R =Num1.getText( ) public void actionPerformed (ActionEvent e) { } private JTextField Num1 ; private JButton colorMe; private JPanel p1 ; private Color c ; p1 = new JPanel ( ); this.getContentPane().add(p1); p1.add(new JLabel(“Red")); p1.add(Num1 = new JTextField(3 )); p1.add(colorMe = new JButton("ColorMe")); colorMe.addActionListener(this ); public Colorizer ( ) { } prototype program

Outline Classes in Java Rectangle example propertiesor instance variables instances of class constructor for class Demo Colorizer Example compilationexecution RGB representationpanel color buttonsdarken-buttonresize-button Colorizer class main program TextPad

Colorizer Class Design program structure constructor event-handler GUI setup constructor recognize event-source grab text make Color reset background color reset window size event-handler instance variables declarations labels textFields buttons panel actionListeners Outline

TextPad Multiple instances Terms and Concepts GUI components JTextFields JButtons JLabels Event-driven programming Asynchronous Inheriting properties & methods Instance variables Constructors Method signature Classes Objects Methods Instances of objects JFrames import statements extend class JFrame’s setSize method JFrame’s setVisible method JPanels JPanel’s add method this notation ActionEvent ActionEvent’e getSource() method Color objects RGB representation Test program Program structure JPanel’s setBackground ( c) method