TODAY WE WILL:
* announcements
* intro to graphical user interfaces (GUIs) in Java
* intro Java event handling and intro to Swing
* prep for next class
* Should be working on Homework 3!
* Current reading:
Course text
* Chapter 10 - Graphical User Interface programming
* Chapter 11 - Subsection 11.3.2 - Labels and Labeling components
=====
* package-level access
* features (classes, methods, or variables for example) that are not marked
as either
public or private are considered to have package-level access
* the feature can be accessed by all methods in the same package
* we're currently using the so-called unnamed package --
no package statement in our classes, and so all the classes
in the same folder/directory are our current package
=====
* we are using items from the package
javax.swing
...which is built atop the old Abstract Windowing Kit AWT package:
java.awt
* top-level window (a window not contained in another window)
is called a frame,
and we'll be using the Swing JFrame class as the basis for our
top-level windows
our first example: see SimpleFrameTest
* it is considered good Swing style
to configure Swing interfaces in the so-called event dispatch thread
(the thread of control that passes mouse clicks, key typing, etc. to
user interface components
...we'll do this by constructing the frame in a lambda expression
that's an argument to EventQueue.invokeLater
EventQueue.invokeLater( () ->
{
desired_statements;
} );
* fun fact: a JFrame becomes only when you make it visible
with its setVisible method
* fun fact: by default, a JFrame's size is 0 by 0 pixels...!
* we'll tend to put components ONTO a JFrame (and draw on those
as opposed to directly on the JFrame)
* we can set a JFrame's title, though, with its setTitle method
* but a common pattern:
set up container instance
add it to the JFrame
=====
JOptionPane - your source for (relatively) easy dialog boxes!
=====
* from package javax.swing
* JOptionPane simpleDialog = newJOptionPane();
simpleDialog.showMessageDialog(null, Welcome to \nCS 235");
=====
what about some actual event handling?
=====
* Java's event handling model (Java 1.1 event model)
* an action (such as clicking on a button, typing in a textfield,
moving a mouse into a container, etc.) is an event
* if I want an application to react to an event,
it needs event handling code
* Java's event handling classes are in the package java.awt.event
(and both Swing and AWT use these)
import java.awt.event.*;
* we have event SOURCES - like a button!
An object interested in receiving events is an event listener.
An event source (like that button) maintains a list of listeners
that want to be notified when an event on that event source occurs.
* so you register event listeneres for that event source
and when an event occurs on that source, control is passed to
the desired method to handle an event;