Review for Major 2 ICS 201

advertisement
Review for Major 2
ICS 201
JVM
Java Compilation Model
Hello.java
javac
Hello.class
Hello
Windows
X86
Hello
Mac
PowerPC
Hello
Hello
Linux
X86
Organization of JVM
Class Area
Class Information
Constant Pool
Heap
Stack
Native
Stack
Method Area
Internet
*.class
File System
*.class
Class
Loader
PC, FP, SP
Registers
Execution
Engine
Native
Interface
Native
Methods
Swing
Introduction to Swing

The Java AWT (Abstract Window Toolkit) package is
the original Java package for doing GUIs

A GUI (graphical user interface) is a windowing
system that interacts with the user

The Swing package is an improved version of the
AWT



However, it does not completely replace the AWT
Some AWT classes are replaced by Swing classes, but other
AWT classes are needed when using Swing
Swing GUIs are designed using a form of objectoriented programming known as event-driven
programming
Events



Event-driven programming is a programming style
that uses a signal-and-response approach to
programming
An event is an object that acts as a signal to another
object know as a listener
The sending of an event is called firing the event

The object that fires the event is often a GUI component,
such as a button that has been clicked
Event Firing and an Event Listener
A First Swing Demonstration (Part 4 of 4)
Labels

A label is an object of the class JLabel



Text can be added to a JFrame using a label
The text for the label is given as an argument when the
JLabel is created
The label can then be added to a JFrame
JLabel greeting = new JLabel("Hello");
add(greeting);
The Color Constants
Containers and Layout Managers

Multiple components can be added to the content pane of a
JFrame using the add method


However, the add method does not specify how these
components are to be arranged
To describe how multiple components are to be arranged, a
layout manager is used


There are a number of layout manager classes such as
BorderLayout, FlowLayout, and GridLayout
If a layout manager is not specified, a default layout manager is
used
Some Layout Managers
Panels


A GUI is often organized in a hierarchical fashion, with
containers called panels inside other containers
A panel is an object of the JPanel class that serves as a
simple container


It is used to group smaller objects into a larger component (the
panel)
One of the main functions of a JPanel object is to subdivide a
JFrame or other container
Using Panels
Menu Bars, Menus, and Menu Items

The following creates a new menu, and then adds a menu item
to it
JMenu diner = new
JMenu("Daily Specials");
JMenuItem lunch = new
JMenuItem("Lunch Specials");
lunch.addActionListener(this);
diner.add(lunch);

Note that the this parameter has been registered as an action
listener for the menu item
Text field and Text area
Other Components

JCheckBox


(Also JCheckBoxMenuItem)
JRadioButton

(Also JRadioButtonMenuItem)
Other Components

JComboBox

JTree
Other Components

JList

JPasswordField

JSlider
Reference (Important)

The Swing Java tutorial examples :
http://java.sun.com/docs/books/tutorial/uiswing/examples/components/index.html
Screen Coordinate System
The Method paint and the Class Graphics


Almost all Swing and Swing-related components and containers
have a method called paint
The method paint draws the component or container on the
screen



It is already defined, and is called automatically when the figure is
displayed on the screen
However, it must be redefined in order to draw geometric figures
like circles and boxes
When redefined, always include the following:
super.paint(g);
The Method paint and the Class Graphics

Every container and component that can be drawn on the
screen has an associated Graphics object


The Graphics class is an abstract class found in the java.awt
package
This object has data specifying what area of the screen the
component or container covers

The Graphics object for a JFrame specifies that drawing takes
place inside the borders of the JFrame object
Drawing a Very Simple Face (part 5 of 5)
Specifying an Arc (Part 1 of 2)
Programming Applets

The word applet is meant to suggest a small

Applets were intended to be small programs run
over the Internet
application



However, there are no size constraints on applets
Applets can be viewed over the Internet, or without any
connection to the internet
An applet is similar to a Swing GUI

In fact, almost all of the Swing techniques can be used in
applets
Tip: Converting a Swing Application to an
Applet
The fastest and easiest way to explain how to define an
applet, is to explain how to modify a Swing GUI to transform
it into an applet

Derive the class from the class JApplet instead of from the
class Jframe
Remove the main method
Replace the constructor with a no-parameter method named
init
1.
2.
3.
–
4.
5.
6.
7.
–
The body of the init method can be the same as the body of the deleted
constructor, but with some items removed
Delete any invocation of super
Delete any method invocations that program the close-window
button of a windowing GUI
Delete any invocation of setTitle
Delete any invocation of setSize
The following applet was generated in this way
Multithreading

In Java, programs can have multiple threads


Threads are often thought of as computations that run in
parallel



A thread is a separate computation process
Although they usually do not really execute in parallel
Instead, the computer switches resources between threads so
that each one does a little bit of computing in turn
Modern operating systems allow more than one program to
run at the same time

An operating system uses threads to do this
Nonresponsive GUI
Threaded Version of FillDemo
Without
Threads
With
Threads
The Runnable Interface

Another way to create a thread is to have a class implement
the Runnable interface

The Runnable interface has one method heading:
public void run();

A class that implements Runnable must still be run from an
instance of Thread

This is usually done by passing the Runnable object as an
argument to the thread constructor
The Runnable Interface: Suggested
Implementation Outline
public class ClassToRun extends SomeClass implements Runnable
{ . . .
public void run()
{
// Fill this as if ClassToRun
// were derived from Thread
}
. . .
public void startThread()
{
Thread theThread = new Thread(this);
theThread.start();
}
. . .
}
FillDemo with Runnable interface
Download