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.

Slides:



Advertisements
Similar presentations
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
Advertisements

Chapter 16 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.
Graphical User Interfaces
JAVA1 1. JFrame. JAVA2 JApplet import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HerhalingAppletH10 extends JApplet implements.
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.
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.
Graphic User Interfaces Layout Managers Event Handling.
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.
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.
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.
Övning 5. Repetition klasser class Rektangel { private static int antal = 0; private double längd; private double bredd; public Rektangel(double l, double.
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)
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
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,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
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.
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
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.
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.
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)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
© 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,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
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.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
Multiple buttons and action calls
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.
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
A First Look at GUI Applications
CHAPTER 7 & 8 REVIEW QUESTIONS
Graphical User Interface (pronounced "gooey")
Java Programming: From Problem Analysis to Program Design,
Ellen Walker Hiram College
Event-driven programming for GUI
CiS 260: App Dev I Chapter 6: GUI and OOD.
Graphical User Interface
Presentation transcript:

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

Illustrate with Rectangle object Class definition of rectangle including constructor, properties, methods Driver or 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 ; properties or instance variables constructor class= blueprint for object 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 ) Driver or Test program instance r of rectangle saved in file TestRectangle.java 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) ;

Defining & Executing a window-like object

public class X extends JFrame { } import javax.swing.*; import java.awt.*; import java.awt.event.*; X is a JFrame...and maybe more Import Java class libraries to be used currently... just a shell

public class X extends JFrame { } import javax.swing.*; import java.awt.*; import java.awt.event.*; needs a main program to run class X file X.java Compile: ctrl-1 [ in TextPad ] Execute: ctrl-2 " " not this class !

Empty JFrame

import javax.swing.* ; import java.awt.* ; public class TestX { } public static void main (String [ ] args ) { X m = new X() ; m.setVisible(true) ; m.setSize(200, 200) ; } // MyInput.readString(); main program JFrame methods For dos IO or stalling program - requires MyInput class creates instance m like: int k = 3; main

Summary class X - blueprint for object import - provides methods for object extends JFrame - X can use [inherits] JFrame methods naming - class X in file X.java TextPad - can compile separately; but run main Main - public static void main (String [ ] args) frame methods - setVisible(true) & setSize(200, 200) create instance - X m = new X( ) ; DOS IO - MyInput.readString ( ) ;

Calculator example inputsmultiply

Plan of Attack for calculator object Containers: a canvas JPanels Make input fields & place in panelJTextFields Make buttons trigger actionsJButtons Define handler for button eventsactionPerformed Acquire, convert and store field data Containers: content pane getContentPane ( ) Structure Java

1. JFrame2. ContentPane 3. JPanel button JButton hello 4. JTextField

JFrame Title Bar Content PanePanel JTextFieldJLabelJButton

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame { } public Y() { } JPanel p ; Declare panel p Create panel p Add p to pane Panels can contain GUI objects like buttons panels Constructor Y() p = new JPanel(); this.getContentPane().add(p) ;

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame implements ActionListener { } JPanel p; public Y() { p = new JPanel(); this.getContentPane().add(p); } declare fields & buttons create fields & add to p JTextField n1, n2, n3; JButton b1,b2; n1=new JTextField(10); p.add(n1); n2=new JTextField(10); p.add(n2); n3=new JTextField(10); p.add(n3); create buttons & add to p b1=new JButton("+"); p.add(b1); b2=new JButton("*"); p.add(b2); Fields & Buttons

Events import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame { } public Y() {p = new JPanel(); this.getContentPane().add(p); n1=new JTextField(10); p.add(n1); n2=new JTextField(10);p.add(n2); n3=new JTextField(10);p.add(n3); b1=new JButton("+") ;p.add(b1); b2=new JButton("*") ;p.add(b2); } public void actionPerformed (ActionEvent bert) { } JPanel p ; JTextField n1, n2, n3 ; JButton b1, b2 ; make buttons listen for events template for event handler says class has events b1.addActionListener(this); b2.addActionListener(this); implements ActionListener

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame implements ActionListener { } public Y() {p = new JPanel(); this.getContentPane().add(p); n1=new JTextField(10); p.add(n1); n2=new JTextField(10);p.add(n2); n3=new JTextField(10);p.add(n3); b1=new JButton("+") ;p.add(b1); b2=new JButton("*") ;p.add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed (ActionEvent bert ) { } JPanel p ; JTextField n1, n2, n3 ; JButton b1, b2 ; event handler template int num1=Integer.parseInt(n1.getText()); int num2=Integer.parseInt(n2.getText()); int num3=0; if ( bert.getSource()==b1) { num3=num1 + num2; } if (bert.getSource ()==b2) { num3=num1 * num2; } n3.setText ( String.valueOf ( num3 ) ); + or - depending on source of event convert & back/store get/convert data

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Y extends JFrame implements ActionListener { } public Y() {p = new JPanel(); this.getContentPane().add(p); n1=new JTextField(10); p.add(n1); n2=new JTextField(10);p.add(n2); n3=new JTextField(10);p.add(n3); b1=new JButton("+") ;p.add(b1); b2=new JButton("*") ;p.add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed (ActionEvent bert) { int num1=Integer.parseInt(n1.getText()); int num2=Integer.parseInt(n2.getText()); int num3=0; if (bert.getSource()==b1){num3=num1 + num2; } if (bert.getSource()==b2){num3=num1 * num2; } n3.setText ( String.valueOf(num3) ); } JPanel p ; JTextField n1, n2, n3 ; JButton b1, b2 ; class Y definition Global declarations Constructor Y ( ) Event-handler

TextPad Multiple instances Terms and Concepts GUI components JTextFields JButtons JLabels Event-driven programming Asynchronous Inheriting properties & methods 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 dos IO – MyInput.readString ( ) Program structure JPanel’s setBackground ( c) method