Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05."— Presentation transcript:

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

2 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

3 Graphical Components GUI consists of graphical components such as : –Windows –Buttons –Menus –Text Fields

4 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

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

6 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

7 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

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

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

10 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

11 Frames Frame in JAVA = Window The frame holds all the GUI components GUI programs can have one more more frames.

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

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

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

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

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

17 Extending JFrame GUIs are usually written in a class that extends the JFrame Main program then calls this class to run the GUI.

18 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 }

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

20 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

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

22 Event Objects Events in JAVA are represented as Objects (event objects) Event Objects are sent to the listener registered to the GUI component

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

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

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

26 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 }

27 Adding the window listener to the GUI program See GUItester.java

28 Questions?


Download ppt "CIS3931 - Intro to JAVA Lecture Notes Set 8 9-June-05."

Similar presentations


Ads by Google