Lecture 7 ISE 582: Web Technology for Industrial Engineering University of Southern California

advertisement
ISE 582: Web Technology for
Industrial Engineering
University of Southern California
DJE Dept of Industrial and Systems Engineering
Lecture 7
JAVA Cup 7: It’s all in the Packaging
Handouts
• Lecture 7 slides
• READ Winston & Narasimhan :
– Chapters 33-38 (pp 201-233)
17 October 2002
Web Technology for IE
2
JAVA Cup 7
• Characters and Strings
– Codestrings and delimiters
• How to Read/Write Objects
• How to Modularize Programs
• Creating a Window
17 October 2002
Web Technology for IE
3
Specifying Delimiters
• You can use codestrings to specify interesting
information in textfile
17 October 2002
Web Technology for IE
4
Specifying Delimiters
• To advise the tokens tokenizer to use
double quotation marks to delimit
strings:
– tokens.quoteChar((int) ‘”’)
• To tell the tokenizer to recognize
carriage returns:
– tokens.eolIsSignificant(true);
17 October 2002
Web Technology for IE
5
Working with O/p File Streams
• To connect to output file
– FileOutputStream stream = new
FileOutputStream(“output.data”);
• To write more than 1-byte-at-a-time
– PrintWriter writer = new PrintWriter(stream);
• It’s good to flush out buffered characters
– writer.flush()
• Close the file
– stream.close()
17 October 2002
Web Technology for IE
6
Reading / Writing Objects
• Serialized input-output operations
– <obj.o/p.stream>.writeObject(<vector>)
– <obj.i/p.stream>.readObject()
• Indicate intentions to use operations by
– implements Serializable
– throws IOException, ClassNotFoundException
• Each instance must belong a class that implements the
Serializable Interface
17 October 2002
Web Technology for IE
7
Review
17 October 2002
stream
reader
buffer
stream
reader
tokens
stream
writer
Web Technology for IE
8
Reading Objects from File
FileInputStream fileInputStream
= new FileInputStream(“in.data”);
ObjectInputStream objectInputStream
= new ObjectInputStream(fileInputStream);
Vector <vector>
= (Vector) objectInputStream.readObject();
17 October 2002
Web Technology for IE
9
To Write Objects to File
FileOutputStream fileOutputStream = new
FileOutputStream(“out.data”);
ObjectOutputStream objectOutputStream = new
ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(<vector>);
objectOutputStream.close();
17 October 2002
Web Technology for IE
10
Modularing Your Program
•
•
•
•
•
Compilation Units
Packages
Setting CLASSPATH
Calling Methods from a Module
Access to Var/Methods in Package
17 October 2002
Web Technology for IE
11
Compilation Units
• Define all classes in same file
• Only one class can be public
• Only one class is universally
accessible
• Convention: each class has its own
compilation unit
17 October 2002
Web Technology for IE
12
Packages
• Use single-class compilation units
• Indicate module using package
statement at beginning of each c.u
– package onto.java.entertainment;
• Convention: package names consist of
components separated by dots
17 October 2002
Web Technology for IE
13
CLASSPATH
• Package names correspond to paths
– package onto.java.entertainment;
– Files stores in ./onto/java
• Setting the path
UNIX
– setenv CLASSPATH <path1>:<path2>:.
– setenv CLASSPATH=<path1>;<path2>;.
PC
17 October 2002
Web Technology for IE
14
Using Methods from a Package
• First import package
– import onto.java.entertainment.*
– Auxiliaries.readMovieFile(“input.data”)
– ((Movie) vectorElement).rating()
• Or, refer to entire name
– onto.java.entertainment.Auxiliaries.readMovieFile
(“input.data”)
– ((onto.java.entertainment.Movie)
vectorElement).rating()
17 October 2002
Web Technology for IE
15
Access to Variables/Methods
• private :
– available only to methods inside own class
• protected :
– available to methods in same class, c.u. or package, subclasses
• No keyword :
– available only to c.u. or package
17 October 2002
Web Technology for IE
16
How to Create Windows
•
•
•
•
•
Nomenclature
Intro to Swing classes
Hierarchy of Swing classes
Listener Classes
Window Creation Pattern
17 October 2002
Web Technology for IE
17
Nomenclature
• GUI (pronounced goo-ey) :
– Graphical User Interface
• Components :
– class instances that have a graphical representation
• Containers :
– components that allow nesting of other containers inside
their boundaries
• Window :
– a container’s graphical representation
17 October 2002
Web Technology for IE
18
Introduction to Swing (J) Classes
• API = Application Programmer’s Interface
• The AWT Package: java.awt.*
– AWT = Abstract Window Toolkit
– contains Component, Container classes
• The Swing Package: java.swing.*
– contains JFrame, JApplet, JComponent, JPanel classes
• Try to restrict display work to Swing classes
17 October 2002
Web Technology for IE
19
Hierarchy of Swing Classes
Component
Container
Window
Panel
Frame
Applet
JFrame
JComponent
JPanel
JList
JLabel
JTextField
JApplet
JButton
17 October 2002
Web Technology for IE
20
Example: A Window
import javax.swing.*;
public class Demonstrate {
public static void main (String argv []) {
JFrame frame = new JFrame (“I am a
window, square and proud”);
frame.setSize(200,200);
frame.show();
}
}
17 October 2002
Web Technology for IE
21
Listener Classes
• Event as a state change
– Mouse clicks and key presses
– Variable-value changes
• Event as instance of EventObject class
– Describes state change
• Java deals with events through Listener Classes
• Listener classes are defined by
– Extending existing listener classes
– Implementing listener interfaces
17 October 2002
Web Technology for IE
22
Example: Link listener to frame
• When mouse clicks:
– a WindowEvent instance is created
– the windowClosing method is called with
connected listener as target and
WindowEvent instance as arg
17 October 2002
Web Technology for IE
23
Adapter Classes
• Adapter classes implement all
interface-required methods as donothing methods.
• You can define shadowing methods in
your subclass for whatever subset of
methods you need
17 October 2002
Web Technology for IE
24
Nesting Class Definitions
• Advantage of nesting classes:
– Method definition (windowClosing)
appears in proximity to window definition
– Can reuse names of private classes such
as LocalWindowListener
17 October 2002
Web Technology for IE
25
Window Creation Pattern
• Define a subclass of JFrame class
• Main method calls constructor
• Constructor:
– assigns title, size, and displays a window
– creates required class instances
– connects class instances
17 October 2002
Web Technology for IE
26
Download