09-June-05 PowerPointPresentation

Download Report

Transcript 09-June-05 PowerPointPresentation

CIS3931 - Intro to JAVA
Lecture Notes Set 8
9-June-05
JAVA GUI Classes
• Based on fundamental classes in the
Application Windowing Toolkit (AWT).
• JAVA releases > 1.1.2 now use Swing,
which is a more versatile version of AWT.
• Swing is the accepted standard for GUI
programming in JAVA
Graphical Components
• GUI consists of graphical components
such as :
– Windows
– Buttons
– Menus
– Text Fields
Events
• An event is an action performed by the user
• Examples :
–
–
–
–
–
Clicking on a button to choose a program option
Making a choice from a menu
Entereing text in a text field
Dragging a scroll bar
Clicking on a window’s close button
Event-Driven Programming
• GUI’s are generally interactive programs.
• A “event-driven” program is one that
responses to events generated by the
user.
• In JAVA, the Swing components generate
the events (w/ user interaction) and the
JAVA methods respond to the events.
The Swing Package
• Swing contains all of the components
necessary for writing a GUI program.
• No need to manually program buttons,
windows, menus, etc.
• Simply call the constructor for a
component
Parts of a GUI Program
• Commonly divided into three parts
– 1. Graphical components
• Make up the visual part of the GUI
– 2. Listener methods
• Receive events and handle/respond to them
– 3. Application methods
• Perform the useful work of the program
GUI Program Flow
• GUI Program displays the graphical
components
• Interactive graphical components are handled
by listener methods
• Listener methods respond to events generated
by the interactive graphical components and
call application methods
• Application methods perform the necessary
functions of the program.
GUI Container Classes
• GUI program consists of a collection of
graphical components that are placed
inside of one or more windows.
• Components are contained by a particular
windows.
• Containers are objects that hold other GUI
components.
Swing Frames
• GUI programs are created by extending the
class JFrame.
• JFrame class holds the basic GUI functionality.
• We will be discussing the following :
–
–
–
–
The JFrame class
Extending the JFrame class
The paint() method
The drawString() method
Frames
• Frame in JAVA = Window
• The frame holds all the GUI components
• GUI programs can have one more more
frames.
The smallest GUI frame program
import java.awt.*;
import javax.swing.*;
public class TestFrame1
{
public static void main ( String[] args )
{
JFrame frame = new JFrame("Test Frame 1");
frame.setSize(200,100);
frame.setVisible( true );
}
}
Program explanation
//Call the new constructor for JFrame and set
//the title = “Test Frame 1”
JFrame frame = new JFrame(“Test Frame 1”);
//Set the initial size to 200w x 100h pixels
frame.setSize(200,100);
//Make the frame visible (default is invisible)
frame.setVisible(true);
setVisible(boolean)
• All windows are initially constructed with
setVisible(false);
• Object exists in memory, but it not drawn
to the screen
• setVisible(true); draws the frame to the
screen.
• setVisible(false); makes the frame invisible
(but retains the frame in program memory)
Closing the frame
• Hitting the “x” in the top right corner closes the
frame in the example program
• HOWEVER – The program continues to run!
• The “close window” event must be handled
somewhere in the program to allow the program
to properly exit (discussed later in this class).
Frame Dimensions
• setSize() can be called anywhere in the program
to change the frame size
• Example :
import java.awt.*;
import javax.swing.*;
public class TestFrame1
{
public static void main ( String[] args )
{
int height=100, width=200;
JFrame frame = new JFrame("Test Frame 1");
frame.setSize( width, height );
frame.setVisible( true );
frame.setSize( width+50, height+75 );
}
}
Extending JFrame
• GUIs are usually written in a class that
extends the JFrame
• Main program then calls this class to run
the GUI.
Extending JFrame example
import java.awt.*;
import javax.swing.*;
class MyFrame extends JFrame
{
// paint() is called automatically by the system to display your customizations to the frame.
public void paint ( Graphics g )
{
g.drawString("A MyFrame object", 10, 50 ); // draw a String at location x=10 y=50
}
}
public class TestFrame2
{
public static void main ( String[] args )
{
MyFrame frame = new MyFrame(); // construct a MyFrame object
frame.setSize( 150, 100 );
// set it to 150 wide by 100 high
frame.setVisible( true );
// ask it to become visible
// paint() is indirectly called
}
}
Example explanation
• MyFrame extends JFrame
• MyFrame does everything JFrame does,
with the addition of whatever methods are
in MyFrame.
• A MyFrame is constructed, which actually
constructs a JFrame in addition to
whatever you added in MyFrame.
Event Listeners
• Users generate events by interacting with
a GUI component.
• Examples of event generation :
– Moving the mouse
– Clicking the mouse
– Clicking on a button
– Typing some text into a text area
Event Listeners
• Event Listener Objects allow the program
to respond to an event.
• Listener methods are contained in Event
Listener Objects to handle various types of
events.
• Events can be ignored (no listener =
ignore event)
Event Objects
• Events in JAVA are represented as
Objects (event objects)
• Event Objects are sent to the listener
registered to the GUI component
Responding to Events
• To respond to events, a program must :
– 1. Create an event listener object for the type
of event
– 2. Register the listener object to the GUI
component that generates the event.
Event Creation
• JAVA doesn’t know which events will be
ignored … so, it must create an event
object for every event
WindowAdapter Class
• WindowAdapter = listener for events generator
by the class Window and its decendants
(JFrame, Frame …)
• Includes listener methods for every type of
Window event.
• By default, these methods receive an event, but
do nothing. In order to make them do something,
you have to extend WindowAdaptor and
override its methods.
Creating a listener for a frame
object
public class WindowQuitter extends WindowAdapter
{
// override a method of WindowAdapter
public void windowClosing( WindowEvent e )
{
System.exit( 0 ); // what to do for this event
}
}
Adding the window listener to the
GUI program
• See GUItester.java
Questions?