Even-Driven Programming and basic Graphical User Interface.

Slides:



Advertisements
Similar presentations
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
GUI and event-driven programming An introduction.
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.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
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,
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Interfaces & Polymorphism part 2:
CSE 219 Computer Science III Graphical User Interface.
GUI programming Graphical user interface-based programming.
Graphical User Interface in Java
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
DCS2133 Object Oriented Programming Graphical User Interface & Event-Driven Programming.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
A simple swing example GETTING STARTED WITH WIND CHILL.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-2 ( Book Chapter 14) GUI and Event-Driven Programming.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Computer Science 209 GUIs Model/View/Controller Layouts.
Java Swing One of the most important features of Java is its ability to draw graphics.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
Event-Driven Programming F Procedural programming is executed in procedural order. F In event-driven programming, code is executed upon activation of events.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7-3 ( Book Chapter 14) GUI and Event-Driven Programming.
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.
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.
Introduction to GUI in 1 Graphical User Interface Nouf Almunyif.
Block 1 Unit 2 Basic Constructs in Java. Objectives create a simple object using a constructor; create and display a window frame on your computer screen;
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
©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.
CSC 205 Programming II Lecture 5 AWT - I.
Chapter 8 Event Handling.
Java Applets.
Java Programming: Guided Learning with Early Objects
Java Swing.
Java Programming: From Problem Analysis to Program Design,
Graphical user interface-based programming
Java Applets.
Chapter 7-2 (Book Chapter 14)
Chapter 7 (Book Chapter 14)
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
Chapter 7-2 (Book Chapter 14)
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

Even-Driven Programming and basic Graphical User Interface

Exam 2 – Section 02 Avg: 127 (84%)

Exam 2 – Section 01 Avg: (85%)

Exam 2 1. In String class, substring() is a(n) Attribute Method Statement Criteria

Exam 2 2. The following statement: double[] studentGPA = new double[12]; – only declares the array studentGPA and did not allocate memory for this array – only allocates memory for this array studentGPA and did not declare it – creates 12 studentGPA objects – declares the array named studentGPA and allocates memory for this array

Exam 2 3. Suppose an array contains 1024 elements. What are the least and the greatest number of comparisons for a successful search using linear search? – 1 and 10 – 1 and 1024 – 1 and 8 – 1 and 1023

Exam 2 4. Suppose a sorted array contains 1024 elements. What are the least and the greatest number of comparisons for a successful search using binary search? 1 and and 10 1 and 8 1 and 9

Exam 2 5. Which of the followings is the constructor of the class MyString? public String MyString (String str) {…..} public MyString(String str) { ….} public void MyString(String str) {….} public Boolean MyString(String str) {….}

Exam 2 6. The amount of memory allocated to store an array depends on: a. the name of the array b. the size of the array c. the size and type of values in the array d. none of the above

Exam 2 6. The amount of memory allocated to store an array depends on: a. the name of the array b. the size of the array c. the size and type of values in the array d. none of the above

Exam 2 7. How many comparisons we need to perform to find x=10 from the following array with 9 elements using binary search a. 1 b. 2 c. 3 d. 4 Pass 1: (0+8)/2 = 4. 0<10 Pass 2: (5+8)/2 = 6. 20>10 Pass 3: (5+5)/2 = 510=10

Exam 2 8. Array is a collection of data values of the different data types True False

Exam 2 8. Array is a collection of data values of the different data types True False

Exam 2 9. This is a special method that is invoked when a new object of a class is created: a. Main method b. Constructor c. New method d. Void method

Exam In order to run a Java program, this program must include at least one class that has main method a. True b. False

Exam 2 BAD C

E OR C C E D A B Or B A

Exam 2 = AE BDC

CB A D D A

public String ExampleClass(String anExample) { example = new String(anExample); } Constructor doesn’t have a return data type

Exam 2 public class QuestionOne { private double c; public QuestionOne() { c=0; } private void methodOne (int a) { c=a; } public double methodTwo() { return c; } public class QuestionOneMain { public static void main(String args[]) { QuestionOne q1; q1 = new QuestionOne(); q1.c = 12; q1.methodOne( 12); double result = q1.methodTwo(); } C is a private data member and thus can not be assessed outside of QuestionOne Class methodOne is also a private method

Exam obj = new QuestionTwo(); //count = 0 obj.modifyCount(); // count = (0+10)*2 = 20 obj.printCount(); count=20

Exam String resultStr = q3.getSubMessage(16,22);getSubMessage // resultStr = “Easter” System.out.println("A part of this message is "+ resultStr+" length="+resultStr.length()); resultStr = “Easter” and length=6

What is Graphical User Interface (GUI) A graphical user interface is a method of interacting with a computer through a metaphor of direct manipulation of graphical images and widgets in addition to text. interacting with a computer metaphordirect manipulationwidgets GUI display visual elements such as icons, windows, menu, buttons, checkboxes…

Examples

GUI in Java Java.AWT package: GUI objects are implemented by using native GUI objects and may behave differently on different OSs. Javax.swing package (only available from JDK 1.2 onward): GUI objects are implemented fully in Java and therefore behave the same on different OSs. Offers new features compared to Java.AWT package

Event-driven programming An event occurs when the user interacts with a GUI object (click a button, move a mouse, press a key…)

Plan for this week Create a frame Create a button and handling button event

Create a frame

Code import javax.swing.*; public class JFrameSubclass extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; public JFrameSubclass () { setTitle("My First Subclass"); setSize(FRAME_WIDTH, FRAME_HEIGHT); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); setDefaultCloseOperation(EXIT_ON_CLOSE); } Import GUI swing package

Code import javax.swing.*; public class JFrameSubclass extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; public JFrameSubclass () { setTitle("My First Subclass"); setSize(FRAME_WIDTH, FRAME_HEIGHT); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); setDefaultCloseOperation(EXIT_ON_CLOSE); } Create a subclass that inherits JFrame class

JFrame class Type: “JFrame class Java” in Google and choose the link: swing/JFrame.html

Code import javax.swing.*; public class JFrameSubclass extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; public JFrameSubclass () { setTitle("My First Subclass"); setSize(FRAME_WIDTH, FRAME_HEIGHT); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); setDefaultCloseOperation(EXIT_ON_CLOSE); } Constant declarations

Code import javax.swing.*; public class JFrameSubclass extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; public JFrameSubclass () { setTitle("My First Subclass"); setSize(FRAME_WIDTH, FRAME_HEIGHT); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); setDefaultCloseOperation(EXIT_ON_CLOSE); } Constructor for this JFrameSubclass class

Using methods from JFrame class setTitle methods: Description is available at: Title(java.lang.String) setTitle public void setTitle(String title)String – Sets the title for this frame to the specified string. Example: setTitle("My First Subclass”);

Using methods in JFrame setSize(int, int) Avaiable at: nent.html#setSize(int,%20int) public void setSize(int width, int height) – Resizes this component so that it has width width and height height. Example: setSize(FRAME_WIDTH, FRAME_HEIGHT);

Using methods in JFrame setLocation public void setLocation(int x, int y) Moves this component to a new location. The top-left corner of the new location is specified by the x and y parameters in the coordinate space of this component's parent. Example: setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

Using methods in JFrame setDefaultCloseOperation: me.html#setDefaultCloseOperation(int) me.html#setDefaultCloseOperation(int) public void setDefaultCloseOperation(int operation) Example: setDefaultCloseOperation(EXIT_ON_CLOSE);

Using public constants in JFrame EXIT_ON_CLOSE /swing/JFrame.html#EXIT_ON_CLOSE public static final int EXIT_ON_CLOSE

Create a button Create a dumb GUI first Add a module to handle event later

Create a dumb GUI

Code import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JButtonFrame extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; private static final int BUTTON_WIDTH=80; private static final int BUTTON_HEIGHT=30; private JButton cancelButton; private JButton okButton; Packages included for event and GUI objects

Code import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JButtonFrame extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; private static final int BUTTON_WIDTH=80; private static final int BUTTON_HEIGHT=30; private JButton cancelButton; private JButton okButton;

Code import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JButtonFrame extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; private static final int BUTTON_WIDTH=80; private static final int BUTTON_HEIGHT=30; private JButton cancelButton; private JButton okButton;

Code import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JButtonFrame extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; private static final int BUTTON_WIDTH=80; private static final int BUTTON_HEIGHT=30; private JButton cancelButton; private JButton okButton;

Constructor for this class public JButtonFrame () { Container contentPane= getContentPane(); setTitle("My Button class"); setResizable(false); setSize(FRAME_WIDTH, FRAME_HEIGHT); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane.setLayout(null); contentPane.setBackground(Color.white);

Constructor for this class okButton = new JButton("OK"); okButton.setBounds(70,125,BUTTON_WIDTH,BUTTON_HEIGHT)okButton.setBounds(70,125,BUTTON_WIDTH,BUTTON_HEIGHT); contentPane.add(okButton); cancelButton = new JButton("Cancel"); cancelButton.setBounds(160,125,BUTTON_WIDTH,BUTTON_HEIGHT); contentPane.add(cancelButton); setDefaultCloseOperation(EXIT_ON_CLOSE); }