part 1 – introduction chapter 12
TRANSCRIPT
1
Event Driven Programming
Part 1 – Introduction
Chapter 12
CS 2334University of Oklahoma
Brian F. Veale
2
Graphical User Interfaces● So far, we have only dealt with
console-based programs– Run from the console window
● Suitable for simple textual I/O, but not very user-friendly
● Most of the programs we use daily have Graphical User Interfaces
● Modern Operating Systems usually have GUI interfaces
● If we can learn to write programs with GUIs, we have a much larger range of possible programs
3
Interacting with console and GUI programs
● Console interaction is typically quite limited– MS-DOS only has about 100 commands
– Some command line programs have far fewer
●Such as projects 1 and 2● GUIs have incredibly large number
of possible interactions– NetBeans– Microsoft Office products
4
Events● Wide range of actions with GUI
programs– Moving the mouse– Closing a window– Clicking on a button– Typing into a dialog box
● All of these types of interactions have a generic name, event
● An event is a user interaction with a program with a graphical user interface
● Setting-up and managing events is a major part of GUI programming
5
Event Handling● These events mean little unless
our program reacts to them● We will need to decide which
events having meaning to us, and which are unimportant
● event handling - The act of responding to an event.
6
How to handle an event
● Programs respond to events by calling methods
● Java defines which method responds to which event by declaring Listeners
● Listener - An object that has the responsibility to respond to an event generated by a user interaction.
7
GUI Program Structure● Interaction with a GUI program is controlled by
the main event loop– This is an infinite loop with a large switch statement
– When an interaction occurs, the main event loop checks the switch statement for the type of event
– Inside a case statement for a given event is an indication of which Listener method to call
– Once the listener method has completed its actions, control goes back to the main event loop
● In Java, the compiler generates the main event loop.– We provide that code that implements the Listener
methods.
8
Components and Containers
● Java GUIs consist of components arranged in containers
● Component: A single user interface element– Button– Menu item– Text field
● Container: a GUI element that is used to group together and organize other components– At least one container is always needed– JFrame
● Combines content area and menu bar● Very common type of top-level container
● Demo: Button.java
9
Button.java● Some important methods
– frame.getContentPane().add(button)● Adds the JButton to the container● Specifically the content pane portion of the
container (not the menu bar)– frame.pack()
● Organizes the components that are inside the JFrame
● Causes the JFrame to change shape to accommodate the JButton
● JButton would not be visible without this method call
– frame.setVisible(true)● Tells Java to show the GUI on the screen● You won’t see anything without this method call
10
Other Important Components
● JLabel– Displays textual labels– User cannot change the label
● The program can communicate with the following components to get user input/ineraction”– JTextField
● Displays single line of text● User can change the text
– Optionally non-editable– JTextArea
● Displays multiple lines of text● Designed for plain text entry only
● Demo: Label.java, TextField.java, TextArea.java
11
Containers● GUI programs don’t usually have a
single component● Containers help us organize multiple
components– Containers can also contain other containers
12
Heavyweight vs. Lightweight
● There are two categories of containers– Heavyweight
● Have their own window● Should try to have only one per window displayed● Carries extra baggage● Examples: JFrame
– Lightweight● Must be displayed in a Heavyweight container● Examples:
– JPanel: the content pane of the demo program. ● This is returned by frame.getContentPane() in
our demo programs.● Other examples: JMenuBar: a container itself,
contains JMenu containers (which contain JMenuItems)
13
JFrame, JPanel, and JMenuBar
(JPanel)
(JMenuBar)
From page 716 of the Tymann text
14
Properties of Containers● Some containers have special properties
– JSplitPane● Used to split a space shared by two
components– JScrollPane
● Includes scrollbars for when components are too large for the window
– JTabbedPane● Creates an interface for multiple components
using the same space● These components are accessed via a row of
tabs across the top of bottom of the screen
15
Containers in Excel● What containers exist in Excel as shown below?
16
● There are a large number of different components and containers in Swing– Most of these are the classes that start with ‘J’
– Say it with me, “Read the API”● Being able to find and learn how to
use new methods, components, and containers found in the API documentation is an important skill
Futility of shaking sticks at Java Components and Containers
17
“READ the API”● Download the documentation so you don’t
need network access to get to the API– http://java.sun.com/j2se/1.5.0/download.jsp– Scroll down to “J2SE 5.0 Documentation” and
click on “download”– Unzip the file you downloaded.– Open your browser, and choose “Open File” in
the file menu. Then, browse to the “docs\api” directory inside the directory that you saved the contents of the zip file into.