1-2005

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Java Swing - Lecture 1 An Introduction: 

Java Swing - Lecture 1 An Introduction Chris Coleman ( cqc@cs.nott.ac.uk ) http://www.cs.nott.ac.uk/~cqc/G5BUID

Timeline: 

G5BUID - Java Swing - 2005 2 Timeline 27 th Oct (Now) - Java Necessaries, Swing Basics 28 th Oct - Components and Containers 3 rd Oct - Layout Management 17 th Nov - Event Handling and Listeners 18 th Nov - Graphics with Swing 8 th Nov – Problem Class Session Labs Start Week Beginning: 7 th Nov. Time TBA.

Before we Start… (1): 

G5BUID - Java Swing - 2005 3 Before we Start… (1) Swing is all Java. You must know about, and understand: Classes / Objects Method Overloading Inheritance Polymorphism Interfaces How to read the Java2 API Documents

Before We Start… (2): 

G5BUID - Java Swing - 2005 4 Before We Start… (2) If Java is a problem – learn NOW , not in a month when the coursework is due! Labs Come to the labs and help is there for both Java and Swing. Web Use the Sun/Java tutorials Plenty of web resources out there, even the PRG notes Read a book?

2D interface programming toolkits: 

G5BUID - Java Swing - 2005 5 2D interface programming toolkits Tcl/Tk Motif/UIL IDEs (e.g. VB, MSVC++/MFC) Java AWT – the beginnings Java JFC Swing (Java2 - JDK >= 1.2) JBuilder and other Java IDEs etc…

What is Swing?: 

G5BUID - Java Swing - 2005 6 What is Swing? A part of The Java Foundation Classes Swing Look and feel Accessibility Java 2D (Java 2 onwards) Drag and Drop etc Can be used to build Standalone Apps as well as Servlets and Applets

On-line reading and reference: 

G5BUID - Java Swing - 2005 7 On-line reading and reference The Swing connection - http://java.sun.com/products/jfc/tsc/ The JFC Swing trail - http://java.sun.com/docs/books/tutorial/uiswing/ Fundamentals of JFC/Swing I & II - http://developer.java.sun.com/developer/onlineTraining/

Books: 

G5BUID - Java Swing - 2005 8 Books Java Swing (1998) , R obert Eckstein, Mark Loy, Dave Wood , O'Reilly JFC Swing Tutorial, The: A Guide to Constructing GUIs, 2nd Edition (2004); K. Walrath and M. Campione, Addison Wesley

Getting started with Swing (1) : 

G5BUID - Java Swing - 2005 9 Getting started with Swing (1) Compiling & running programs Swing is standard in Java 2 (JDK >= 1.2) Use: ‘javac <program.java>’ && ‘java <program>’ Or JCreator / IDE

Getting started with Swing (2): 

G5BUID - Java Swing - 2005 10 Getting started with Swing (2) Computer Science Department Stuff… PCs Java 2 (1.5.0) on hard disk at \cs\java Unix (tuck and much): Java 2 (1.4.2) in /usr/bin (or /usr/java) Differences between previous versions http://java.sun.com/j2se/1.4.2/docs/guide/swing/SwingChanges.html http://java.sun.com/j2se/1.5.0/docs/guide/swing/SwingChanges.html Coursework marked on Win XP & JDK 1.5.0

Getting started with Swing (3): 

G5BUID - Java Swing - 2005 11 Getting started with Swing (3) Swing, like the rest of the Java API is subdivided into packages: javax.swing, javax.accessibility, javax.swing.border … At the start of your code - always import javax.swing ; import javax.swing.event ; Most Swing programs also need i mport java.awt.* ; import java.awt.event.*;

Differences between Swing and AWT (1): 

G5BUID - Java Swing - 2005 12 Differences between Swing and AWT (1) Never mix Swing and AWT components If you know AWT, put ‘J’ in front of everything AWT: Button Swing: JButton Swing does all that AWT does, but better and there’s much more of it.

Differences between Swing and AWT (2): 

G5BUID - Java Swing - 2005 13 Differences between Swing and AWT (2) B uttons and labels can display images B ehavio u r and appearance of component s Component shape B orders Assistive technologies L ook and feel of the program’s GUI Windows – (but only on Windows, M$ issues….) UNIX GTK etc

A typical Swing program: 

G5BUID - Java Swing - 2005 14 A typical Swing program Consists of multiple parts Containers Components Events Graphics (Threads) Now look at each in turn

A simple Swing program - Containers: 

G5BUID - Java Swing - 2005 15 A simple Swing program - Containers Containers

Remember this about Containers:: 

G5BUID - Java Swing - 2005 16 Remember this about Containers: The structure of containers is your design decision and should always be thought through in advance particularly for managing components nesting containers Failure to do so usually either results in a messy interface, messy code or both.

A simple Swing program - Components: 

G5BUID - Java Swing - 2005 17 A simple Swing program - Components Components

Remember this about Components:: 

G5BUID - Java Swing - 2005 18 Remember this about Components: There are many components that make your job much easier. Often, you will be able to customise an existing Swing component to do a job for you, instead of having to start from scratch Eg can extend (inherit from) the JButton class and ‘paint’ a new button over the top

A simple Swing program - Events: 

G5BUID - Java Swing - 2005 19 A simple Swing program - Events Events

Remember this about events:: 

G5BUID - Java Swing - 2005 20 Remember this about events: ‘Events’ as seen by GUIs do not happen all that often in an application Consider what is happening between events as well as during them

A simple Swing program - Graphics: 

G5BUID - Java Swing - 2005 21 A simple Swing program - Graphics Graphics Complex drawing and shading API. Can do far more than display images.

Remember this about Graphics:: 

G5BUID - Java Swing - 2005 22 Remember this about Graphics: There are many aspects of Swing that allow you to achieve graphics-like things without actually using ‘Graphics’. Therefore, you don’t have to use them most of the time, and it is often easier not to.

A simple Swing program - Threads: 

G5BUID - Java Swing - 2005 23 A simple Swing program - Threads Most simple Swing GUI applications don’t require use of any (extra) threads As Swing creates event-driven programs, all code is executed on the event-dispatching thread

Remember this about Threads:: 

G5BUID - Java Swing - 2005 24 Remember this about Threads: The single-thread rule “ Once a Swing component has been realized , all code that might affect or depend on the state of that component should be executed in the event-dispatching thread” (with some exceptions)

How to Learn Swing: 

G5BUID - Java Swing - 2005 25 How to Learn Swing Don’t even try. Learn general framework principles and design styles. Then use the API reference, and Swing Tutorials to discover detailed usage of each component.

How to read Java Docs (1): 

G5BUID - Java Swing - 2005 26 How to read Java Docs (1) Java 2 (1.5.0) API Reference available at: http://java.sun.com/j2se/1.5.0/docs/api/ Split into 3 Sections (html frames): Top Left: Packages Bottom Left: Classes in Packages Main Frame: Information about selected Class

How to read Java Docs (2): 

G5BUID - Java Swing - 2005 27 How to read Java Docs (2) General idea is find class, and examine main frame for information. Main frame pages split into sections: Package hierarchy & implemented interfaces Class Description, and links to more info Nested Class Summary – Detail in separate page Fields - 2 types Class (static) and instance, plus fields inherited from parent classes / interfaces Constructor Summary Method Summary & inherited methods from parents Detailed info on all summary sections

Summary: 

G5BUID - Java Swing - 2005 28 Summary Do you know enough Java? 2D interface programming toolkits JFC/Swing AWT and Swing Getting started with Swing Parts of a simple Swing program Tomorrow: Components and Containers Some source code, and design styles