Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Similar documents
CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

CS 251 Intermediate Programming GUIs: Event Listeners

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

CS 251 Intermediate Programming GUIs: Components and Layout

Introduction to the JAVA UI classes Advanced HCI IAT351

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Graphical User Interfaces (GUIs)

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Packages: Putting Classes Together

Chapter 6: Graphical User Interfaces

Programming graphics

Java: Graphical User Interfaces (GUI)

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Lecture 5: Java Graphics

SD Module-1 Advanced JAVA

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

SD Module-1 Advanced JAVA. Assignment No. 4

CS11 Java. Fall Lecture 4

COMPSCI 230. Software Design and Construction. Swing

Graphic User Interfaces. - GUI concepts - Swing - AWT

Systems Programming Graphical User Interfaces

MIT AITI Swing Event Model Lecture 17

PIC 20A GUI with swing

SINGLE EVENT HANDLING

CS11 Java. Fall Lecture 3

CS 2113 Software Engineering

Graphical User Interfaces 2

Graphical User Interfaces 2

GUI in Java TalentHome Solutions

Graphical User Interfaces. Comp 152

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Java GUI Design: the Basics

Window Interfaces Using Swing Objects

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Event Driven Programming

Java. GUI building with the AWT

Class 16: The Swing Event Model

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Introduction. Introduction

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Java IDE Programming-I

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

G51PRG: Introduction to Programming Second semester Applets and graphics

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Dr. Hikmat A. M. AbdelJaber

1005ICT Object Oriented Programming Lecture Notes

Graphical User Interfaces 2

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

Basicsof. JavaGUI and SWING

2110: GUIS: Graphical User Interfaces

Containers and Components

14.2 Java s New Nimbus Look-and-Feel 551 Sample GUI: The SwingSet3 Demo Application As an example of a GUI, consider Fig. 14.1, which shows the SwingS

Starting Out with Java: From Control Structures Through Objects Sixth Edition

AP CS Unit 11: Graphics and Events

Part I: Learn Common Graphics Components

CSE 331 Software Design & Implementation

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Summary. Section 14.1 Introduction. Section 14.2 Java s New Nimbus Look-and-Feel. 618 Chapter 14 GUI Components: Part 1

Graphical interfaces & event-driven programming

BASICS OF GRAPHICAL APPS

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

Lecture 3: Java Graphics & Events

Agenda. Container and Component

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Lecture 19 GUI Events

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted

Unit 7: Event driven programming

Window Interfaces Using Swing Objects

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Come & Join Us at VUSTUDENTS.net

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Introduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1

Jonathan Aldrich Charlie Garrod

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Part 3: Graphical User Interface (GUI) & Java Applets

Building Graphical User Interfaces. GUI Principles

Example: CharCheck. That s It??! What do you imagine happens after main() finishes?

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Swing from A to Z Some Simple Components. Preface

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

CS Exam 1 Review Suggestions

Transcription:

Java & Graphical User Interface II Wang Yang wyang AT njnet.edu.cn

Outline Review of GUI (first part) What is Event Basic Elements of Event Programming Secret Weapon - Inner Class Full version of Event Programming

Review of GUI Programming (first part)

GUI Programming Library java.awt.* Contains all of the classes for creating user interfaces and for painting graphics and images. javax.swing.* a set of "lightweight" (written in Java with no native code) components sub packages: java.awt.font.* javax.swing.border.*

GUI Programming Why sub packages? some feature is so important and complicated that need write a bundle of classes/interfaces for it

GUI Programming Container Top-Container JFrame, JApplet, JDialog manage container & component Container JPanel, JScrollPane, JSplitPane manage container & component container can be nested

GUI Programming Component: Text Component : label,textfield, TextArea Button Component : Button, CheckBox, RadioButton A lot of other Component:

GUI Progamming Layout Manager manage the position and the size of component automatically BorderLayout : JFrame FlowLayout : JPanel others : GridLayout, BoxLayout,.

What is Event

We have a calculator

76+22 We want to use it

Different Way to input Mouse Input move : we enter a component area or we leave a component area we enter 7 button we leave 7 button, and we enter 8 button click : we press the mouse button down and release we click 7 button : we want input 7 into calculator

Different Way to input Keyboard char typed : a Unicode character is entered we typed 7, we want input the 7 to the Cal. code typed : combined key entered, not valid Unicode character input ALT+F4, we want close the Cal.

what happened when we input? when we click the 7 button the JVM generate a ActionEvent() when we type the 7 in keyboard the JVM generate a KeyEvent()

What is Event EventObject The root class from which all event state objects source : The object on which the Event initially occurred. AWTEvent source + type : who create : source component what type : ACTION_EVENT_MASK, KEY_EVENT_MASK

More Specific Event Each Specific Event has its own fields and methods ActionEvent When : the timestamp of when this event occurred Command :the command string associated with this action KeyEvent getkeychar : Returns the character associated with the key in this event getkeycode : Returns the integer keycode associated with the key in this event.

Semantic Event ActionEvent is a Semantic Event high-level event is generated by a component including series of low-level event component-defined action for button : click, type space key semantic : the button is executed Other Semantic Event : DocumentEvent

Low-level Event KeyEvent/MouseEvent is a Low-level Event event generated by the input device more detail than semantic event MouseEvent : press, release, click, enter, and exit when to use : provide tooltips

Basic Elements of Event Programming

We need Sth. to handle the Event Event Handler The handler of events An object receiving events and process them response for change components state EventListener : A tagging interface that all event listener interfaces must extend.

Event Handler Each Event has a corresponding Handler ActionEvent : ActionListener KeyEvent : KeyListener MouseEvent : MouseListener Each Handler has specific methods. ActionListener : actionperformed() MouseListener : MouseEnterer(), MousePressed().

Connect Handler to Source Each component has method register right handler for specific event JButton : addactionlistener(actionlistener l) JTextField : addkeylistener(keylistener k)

Event Handling Model

Example

Example

Example

When we click the button

But if we want more component

First way, we try duplicate two methods

First way, we try duplicate two methods The Compiler won t allow the duplicate method

Then we try see the source of event

Then we try see the source of event It worked, but you combine different component logic together

Last we try get the handler out of GUI

Last we try get the handler out of GUI how can you access the textfield

We want a help Each component has its own handler the handler can access other components easily the code will be easy to read and check

Secret Weapon - Inner Class

What is an Inner Class Inner Class : nested class define inside an outside class

Write in One file doesn t mean inner class Not Inner Class!!!!

benefits of inner class An inner class gets a special pass to use the outer class's stuff. Even the private stuff. they have most of the benefits of a normal class. but with special access rights.

restrict of inner class An Inner class Must be tied to at 1 outer class instance you can t new an inner class directly outside the outer class.

New Inner Class in the Outer Class It is allowed

New Inner Class through an Outer Class It is allowed, but not suggest

New Inner Class Directly out of the Outer Class It is not allowed

New Inner Class Directly out of the Outer Class It is not allowed What x should i access?

Full version of Event Programming

Use inner class define the Handler Use Inner class define the different Handler you can define different Handler classes for different Listener Class you also can define different Handler classes for same listener class

Example

Example

Example

Example

Inner Class is not the end Use event s field or method to combine components for same use all the number buttons do the same thing except the command

Example