Introduction to Computing Using Java

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Introduction to Java 2 Programming
Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Event Handling Events and Listeners Timers and Animation.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Intermediate Java1 An example that uses inner classes Week Four Continued.
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.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
GUI Programming in Java
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
CompSci 230 S Software Construction Frameworks & GUI Programming.
More Event Handling Adapters Anonymous Listeners Pop menus Validating User Input.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
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.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
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.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Copyright by Michael P.F. Fung 1 Introduction to Computing Using Java Exception Handling, Array & GUI.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Object Oriented Programming.  Interface  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,
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
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.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 Lecture 8: User Interface Components with Swing.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Welcome To java
Topics Graphical User Interfaces Using the tkinter Module
CompSci 230 S Software Construction
CHAPTER Reacting to the user.
Aum Amriteshwaryai Namah
Appendix I GUI Components and Event Handling
Advanced User Interfaces
Chapter Topics 15.1 Graphical User Interfaces
Event loops 16-Jun-18.
Abstract Window ToolKit (AWT)
Ellen Walker Hiram College
GUI Event Handling Nithya Raman.
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Event-driven programming for GUI
Event Driven Programming
Timer class and inner classes
GUIS: Graphical User Interfaces
GUI Programming using Java - Mouse Events
Event loops.
Web Design & Development Lecture 12
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Events, Event Handlers, and Threads
Chapter 15: GUI Applications & Event-Driven Programming
Constructors, GUI’s(Using Swing) and ActionListner
Event loops 8-Apr-19.
Border Layout, using Panels, Introduction to PinBallGame
Programming Graphical User Interface (GUI)
Event loops.
Event loops 19-Aug-19.
Presentation transcript:

Introduction to Computing Using Java Graphical User Interface 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Overview Human Computer Interface Graphical User Interface Basic Elements Event Driven Model Trigger and Callback 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Human Computer Interface Means of communication. Bi-directional channels. Multiple media. Easy to control. Easy to understand. 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Examples 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Examples 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK 立體 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK

Command-line User Interface Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Keyboard Only? No Way! 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Graphical User Interface Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK GUI – Computer Display Use of graphical representations Windows Icons Buttons … To convey the underlying concepts An icon represents a file A button represents certain function 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK GUI – User Input Use of various interactive input devices Keyboard Mouse Touch screen … To gather command and control from user A mouse click opens a file A mouse drag moves a window Pressing <Enter> means “OK” 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK 7-Minute Break 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK GUI and OOP A window is an object. A button is an object. We create windows and buttons from classes. Such objects store state of the component (field/property); perform certain function (method); generates events (event objects); respond to user actions (callback method); 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK 1) Store State Basic properties Colour Size Visibility Dynamic states On/off state of a button Value of a text field 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK 2) Perform Action Paint the window Draw circles Display text 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

3) Generate Event Detect if you dragged the scrollbar Detect if you clicked a button Detect if you dragged the mouse pointer 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK 4) Handle Event On clicking the buttons, moving the mouse, dragging the mouse, … over a component, events are generated. On receiving an event, the corresponding callback method of a listener is invoked. The method may update the screen, update the state, perform some function, etc. 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK How to Do it Using Java? One of the provided packages java.awt (Abstract Windowing Toolkit) is readily used. There are plenty of component classes well-defined in the package. Frame: basically a window Button: a push button with a label TextField… 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Simple Example import java.awt.*; /* This is not the usual way we call up GUI * Normally, we should subclass (extends) some * GUI components in order to modify the behaviour */ class SimpleGUI { public static void main(String[] args) { Frame myWindow = new Frame(); myWindow.setTitle("Simple GUI"); myWindow.setSize(200, 100); myWindow.setVisible(true); } 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Components List - Classes Michael Fung, CS&E, The Chinese University of HK

How to Use the Components? Read the API Manual and Books! Check for the components you need Create (new) the components Set their properties Basic properties and initial state Relationship between the components Action to be performed on event happening 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Component - Basic Properties Colour : setBackground(Color) setForeground(Color) Size : setSize(int, int) Position : setLocation(int, int) State : setEnabled(boolean) Font : setFont(Font) Visibility : setVisible(boolean) … 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Component - Relationship Sibling/sub-ordinary relationship window.add(button1); Relative position between the components Button1 and Button2 are contained/embedded in the Frame Button1 is on the left of Button2 on the same row 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK 7-Minute Break 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Component - Event Generation Events are automatically generated during interaction with the user. Events are normally happened in conjunction with certain component(s). If the involved component(s) do not listen to (pay attention to) that event, normally nothing would happen. 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Component - Event Listening To listen to an event, we need a listener: Frame myWindow = new Frame(); /* Adapter is a kind of Listener */ WindowAdapter adapter = new WindowAdapter(); myWindow.addWindowListener(adapter); add*Listener() are methods of components. We register a listener to listen to certain kind of events, say MouseEvent. e.g. MouseListener may be MouseAdapter objects. 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Event Handling Button1 Generates  MouseEvent User action MouseListener MouseAdapter mouseClicked mouseEntered mousePressed … Event Handler may: - Update the appearance of the button - Modify the state of the button - Perform programmer-defined action such as “Say Hello” Event Dispatching 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Listener Example File ListenGUI.java import java.awt.*; import java.awt.event.*; class ListenGUI { public static void main(String[] args) { Frame myWindow = new Frame(); myWindow.setTitle("Simple GUI"); myWindow.setSize(200, 100); myWindow.setVisible(true); myWindow.addWindowListener(new MyWindowAdapter()); } class MyWindowAdapter extends WindowAdapter { public void windowClosing(WindowEvent e) { System.out.println(“Terminating the program!”); System.exit(1); // a message to ask the System to exit // ... OR you may open other windows!!! 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Handler (Callback) Method An adapter in fact normally listens to a set of related events. For each kind of event, a corresponding handler method is defined. On receiving a MouseEvent which indicates mouse button pressed, the mousePressed() method of the MouseAdapter object will be invoked. 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Customizing the Handling Without customizing (overriding) the default handler methods provided in the adapter, nothing would happen. That’s why we extends the WindowAdapter, MouseAdapter… classes. By re-defining (overriding) their methods, we can achieve desired behaviour in event handling. 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Examples NormalMouseAdapter NormalWindowAdapter NormalGUI ListenGUI ListenGUI2 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK What Kinds of Events? Examples: WindowEvent needs WindowListener. WindowAdapter is a kind of WindowListener. MouseEvent needs MouseListener. MouseAdapter is a kind of MouseListener. The events themselves are generated by some components under user interaction. 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK After-Life of main() When and how does a GUI program end? main() is the first method to be invoked. Normally, after main() finishes, the program ends. But this is not the case for GUI programs... Why? The underlying event dispatching loop of the system takes over the control. 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK Advanced Topics Using Swing/ AWT with NetBeans Setting properties and layout Creating call-back methods Inner Classes 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK

Michael Fung, CS&E, The Chinese University of HK End Note Readings and References Sections 3.9, 3.10: Components Sections 4.6, 4.7, 4.8: GUI, Buttons Section 5.10: Events Section 6.10: GUI Design Sections 7.9, 7.10: More events Section 8.6: Component Class Hierarchy Section 8.7: Extending Adapter Classes 2005-2009 11a Michael Fung, CS&E, The Chinese University of HK