CS 235 - Week 4 Lab - 2015-09-17
* in Java, you can have INNER CLASSES
* that's a class DEFINED inside the body
of another class!
* why?! [p. 305 of course text]
* inner class methods can access the
data from the scope in which
they are defined
* inner classes can be hidden from other
classes in the same package
* anonynous inner classes are handy when
you want to define callbacks (to be defined
later) without writing a lot of code
* these CAN be complicated --
see Core Java Chapter 6 for more details --
BUT we'll carefully use them in Java GUI and
event-handling programming to make things less
complicated
* regular classes cannot be visibility private --
BUT inner classes CAN have visibility private;
(regular classes have to be public, or package-level)
* an inner class Blink, that is nested within a
class Blah, can access Blah's private data field
thing with the special syntax
Blah.this.thing
<OuterClassName>.this.<outerClassThing>
* anonymous inner classes?! that's a class with NO NAME!!
* turns out the Java Swing is not so-called
"thread safe" --
and we'd like to configure our GUI in a separate
thread, to makes things "safer"
There's an already-created thing called
EventQueue,
which has a method invokeLater,
which we call with an anonymous class as its
argument,
in which we create and start our GUI,
in a sense asking, "please create a thread with
our GUI in it at an appropriate later time"
oh, and there's an interface Runnable
(used to create new threads)
for which you must implement a method with
the signature:
public void run()
EventQueue.invokeLater(new Runnable()
{
public void run()
{
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
* event handling in Java!
* I want a button that when clicked causes something
to happen!
* Java 1.1 event model
* an action like clicking a button is an event
* if I want something to happen because of an event,
I add event handling code
* one or more methods that I want done when
that event occurs
* both AWT and Swing use AWT's event-handling classes,
in package:
java.awt.event
so, for event-handling code, you'll almost always
have:
import java.awt.event.*;
(since you often also want colors and fonts and
layout managers from AWT, you also often classes from
package AWT:
java.awt
and so you ALSO have
import java.awt.*;
* we have event sources, which are sources of events --
e.g., a button
* There are objects interested in receiving events called
event listeners.
* An event source (like a button) maintains a list of
listeners that want to be notified when an event
(like a click) happens to that event source;
and then when such an event occurs to that event source,
each of its listeners is notified
(its appropriate method is called)
how can an event source add a listener?
it is said the REGISTER an event listener
* another class from package java.awt:
Font
one of Font's constructors expects the following
arguments:
* a String name - the name of a physical or logical
font
the logical font guaranteed to be supported
by all Java Runtime Environments (JREs):
Dialog
DialogInput
Monospaced
Serif
SanSerif
* then an int style constant:
Font.PLAIN, Font.ITALIC, Font.BOLD,
Font.BOLD|Font.ITALIC
* then an int size, in points (1 point = 1/72 inch, or
72 points = 1 inch)
new Font("Dialog", Font.PLAIN, 20);
...and many components that have text
have a setFont method that changes that text's display font
TO that call's Font argument