9.1 Java Packages
9.1.1 A collection of classes
• Allows classes to be grouped arbitrarily
• Hierarchical structure independent of inheritance
• Classes can be grouped by function without needing to be directly related
The PATH environment variable
• The PATH environment variable is very similar to a package
• Files are stored on your local hard disk in an hierarchical structure
• The executable files could be distributed throughout this structure
• The PATH variable defines a package of folders that have executable files in them
9.1.2 Class loading and how it works
• Classes are declared as belonging to a package using the package keyword
• If there is no such declaration, the class belongs to the default package which corresponds to the current directory
• To access any class, you must specify the package
• The only exception to this is the java.lang package
9.1.3 Locating explicit package declarations
• When you specify a class in your program the JVM must find the.class file to load it
• The JVM searches as follows:1. The standard directories (in the JDK
directory structure)2. The default package (current directory)3. Directories specified by the CLASSPATH
environment variable
9.1.3 Locating explicit package declarations
• To include a class that is not in java.lang or in the default package:
java.util.Date myDate;myDate = new java.util.Date();
Orimport java.util.Date;import java.util.*;<…>Date myDate;myDate = new Date();
9.2.2 Packaging classes
• Declare the package that the class is to be a member of
• Create the directory structure exactly matching the package declaration
• Use javac to compile, and specify the –d option, whose argument is the root of the package tree
9.2.2 Packaging classes
• For your program to run correctly, the package directory structure must exist on the target computer
• This can be inconvenient if the program is loading over the Internet
• Your entire package directory structure can be compressed in to one ‘jar’ file
• The jar file can be easily downloaded, and the JVM can search it for the package classes
9.3.1 Accessing packages
• Declaring a class to be a member of a package must be the first non-comment in the file
• Importing other packages must occur before the class definition
9.5.1 GUI and terminology
• Components are the core building blocks of a GUI
• Pre-defined elements, such as Buttons, Windows, TextFields and Panels
• Components that can hold other components are containers
• The Java Foundation Classes support five GUI APIs
• This course focuses on AWT only
9.5.1 Understanding the model view controller pattern
• Users today expect a Graphical User Interface (GUI) when using applications
• Java provides the building blocks to create GUIs
• Model View Controller is a design pattern for class design
• MVC requires separate classes for data modelling, interfacing and event handling
9.5.2 Applying GUI concepts
• Creating a GUI requires a thorough understanding of AWT
• Navigating the AWT API documentation is criticaljava.awt.Frame
java.awt.Button
9.5.3 AWT categories • Draw graphics or manipulate images
Graphics, Image, Insets, Point, Polygon, Rectangle
• Position visual elementsBorderLayout, CardLayout, CheckboxGroup, java.lang.Object,
FlowLayout, GridBagLayout, GridLayout
• Change the properties of the visual elementsToolkit, Color, Font, FontMetrics, CheckboxGroup
• Create graphical componentsMenuComponent, Component, MenuBar, MenuItem, , Menu,
PopupMenu, CheckboxMenuItem, Button, Canvas, Checkbox, Choice, Label, List, Scrollbar, TextComponent, TextArea, TextField,
• Components that hold other componentsContainer, Panel, Window, ScrollPane, Dialog, Frame, Applet,
(java.applet, package), FileDialog