Things to mention public static void main(String [] args) imports comments –block comments /* … */ –single-line comments // –javadoc comments and tags.

Slides:



Advertisements
Similar presentations
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Advertisements

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 Foundation Classes (JFC/Swing) Carl Alphonce revised Spring 2007 (with contributions from Alan Hunt)
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Agenda –interfaces and realization –type hierarchy –introduction to graphics and event handling.
Unit 091 Introduction to GUI Programming Introduction to User Interfaces Introduction to GUI Programming GUI Design Issues GUI Programming Issues Java.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
Java GUI Libraries Swing Programming. Swing Components Swing is a collection of libraries that contains primitive widgets or controls used for designing.
Java Swing Toolkit Graphics The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns.
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.
Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.
Unit 111 Java GUI Components and Events  Learning Outcomes oDistinguish between GUI components and containers. oIdentify and distinguish top-level containers.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Chapter 8: Graphical User Interfaces Objectives - by the end of this chapter, you should be able to do the following: –write a simple graphical user interface.
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 
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
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,
Java Swing, Events and MVC Optional Readings: Eckel’s Thinking in Java: Chap 14 (
CSE 219 Computer Science III Graphical User Interface.
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.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Chapter 9: Visual Programming Basics Object-Oriented Program Development Using Java: A Class-Centered Approach.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
– 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.
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.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
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.
COSC 3461: Module 2 Control Flow Paradigms: Reacting to the User.
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.
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java Swing, Events Readings: Just Java 2: Chap 19 & 21, or Eckel’s Thinking in Java: Chap 14 Slide credits to CMPUT 301, Department of Computing Science.
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.
Graphical User Interface (GUI)
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.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
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.
A Quick Java Swing Tutorial
Lecture 15 Basic GUI programming
Multiple buttons and action calls
Graphical User Interfaces
Graphical User Interfaces
Graphical User Interface (pronounced "gooey")
Java Programming: From Problem Analysis to Program Design,
Ellen Walker Hiram College
Event-driven programming for GUI
Course Outcomes of Advanced Java Programming AJP (17625, C603)
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

Things to mention public static void main(String [] args) imports comments –block comments /* … */ –single-line comments // –javadoc comments and tags (?) /** … */ CSE 115/503 Introduction to Computer Science for Majors I1

Using the Java graphics classes In this slide set we will explain the basics of how to create graphical programs. Some advanced issues will be glossed over (e.g. thread safety, graphics library design). In CSE116 we will revisit these and other more advanced topics. CSE 115/503 Introduction to Computer Science for Majors I2

Graphical elements There are two basic types of graphical elements: –Containers able to hold graphical objects, such as containers and components –Components must be put into containers able to generate events when manipulated CSE 115/503 Introduction to Computer Science for Majors I3

Containers Top-level containers –some containers are called “top-level” because they do not need to be place inside any other containers –javax.swing.JFrame is an example (JDialog and JApplet are others) Other containers (not top-level) –most containers must be placed inside some other container –javax.swing.JPanel is an example CSE 115/503 Introduction to Computer Science for Majors I4

Adding elements to a JFrame Top-level containers have multiple panes Content pane is the one which holds components With javax.swing.JFrame, two ways: –call getContentPane() on frame to get frame’s content pane, then call add(…) on content pane to add a component –call add(…) directly on the JFrame object Second approach is just a convenience method, does the same thing the first approach 5

CSE 115/503 Introduction to Computer Science for Majors I Example Creating just a frame –new javax.swing.JFrame() Creating a frame with a title –new javax.swing.JFrame(“My title”) Making the frame visible –call setVisible(true) on the frame Making application close when window is closed: –call setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) on the frame See code in the ex_1_JFrame_visible package in the SP09- CSE115-SwingExamples project in the repository. This is essentially what you did in lab 1. 6

CSE 115/503 Introduction to Computer Science for Majors I A simple component A JLabel is a component that can display text or an image. It can also have a “tooltip”, a note that appears when you hover the mouse pointer over it, without clicking. Look at ex_2_JLabel_tooltip 7

CSE 115/503 Introduction to Computer Science for Majors I Layout managers First, let’s consider what happens if we add another JLabel to the content pane of the JFrame. Look at ex_3_JLabels_noLayout This will show us the need to manage the layout of components added to a container. 8

Applying a layout management strategy Look at ex_4_staticLayout In this example we can apply one of four different layout management strategies to a set of seven JLabels that are added to the JFrame’s content pane. CSE 115/503 Introduction to Computer Science for Majors I9

Another component: a JButton A JButton is a component which is typically set up to react to clicks. Look at ex_5_JButton_noEventHandler, this example just shows a JButton. 10

Events Clicks on buttons, mouse movements, etc. are all considered events. A program can react to events by setting up event handlers. An event handler defines what should happen when a particular event occurs. CSE 115/503 Introduction to Computer Science for Majors I11

CSE 115/503 Introduction to Computer Science for Majors I Event handling – 1 The component which gives rise to an event is decoupled from the part of the code that handles the event. This is called the observer pattern. We have seen this over the past few lectures. A bit of review never hurt :-) General form: – 12

CSE 115/503 Introduction to Computer Science for Majors I Event handling – 2 Observer pattern in Java –An observer is called a listener in Java –Button clicks are “ActionEvents”. –Handlers for ActionEvents are ActionListeners. –An event-generator can have many listeners –Use “addActionListener” method to register a listener with a component 13

CSE 115/503 Introduction to Computer Science for Majors I Event handling for a JButton Look at ex_6_JButton_eventHandler In this example an event handler is defined and attached to the JButton. 14

CSE 115/503 Introduction to Computer Science for Majors I Putting it all together Look at ex_7_dynamicLayoutEventHandling In this example we put everything together: –different components are put into different containers –multiple components are put into each container –different layout managers can be applied dynamically –JButtons’ event handlers select and apply different layout managers to the container containing JLabels 15