cis3931 - intro to java lecture notes set 8 9-june-05

28
CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

Upload: shanon-hampton

Post on 19-Jan-2016

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

CIS3931 - Intro to JAVA

Lecture Notes Set 8

9-June-05

Page 2: 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

Page 3: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

Graphical Components

• GUI consists of graphical components such as : – Windows– Buttons– Menus– Text Fields

Page 4: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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

Page 5: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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.

Page 6: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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

Page 7: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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

Page 8: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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.

Page 9: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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.

Page 10: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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

Page 11: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

Frames

• Frame in JAVA = Window

• The frame holds all the GUI components

• GUI programs can have one more more frames.

Page 12: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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 ); } }

Page 13: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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);

Page 14: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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)

Page 15: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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).

Page 16: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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 ); } }

Page 17: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

Extending JFrame

• GUIs are usually written in a class that extends the JFrame

• Main program then calls this class to run the GUI.

Page 18: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

Extending JFrame exampleimport 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 } }

Page 19: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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.

Page 20: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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

Page 21: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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)

Page 22: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

Event Objects

• Events in JAVA are represented as Objects (event objects)

• Event Objects are sent to the listener registered to the GUI component

Page 23: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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.

Page 24: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

Event Creation

• JAVA doesn’t know which events will be ignored … so, it must create an event object for every event

Page 25: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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.

Page 26: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

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

}

}

Page 27: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

Adding the window listener to the GUI program

• See GUItester.java

Page 28: CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05

Questions?