CIS 270—Application Development II Chapter 11—GUI Components: Part I
Introduction GUI (graphical user ___________) GUI ____________: An object with which the user interacts. label text field button text area combo box menu
JOptionPane A dialog box (or just dialog) is an application window. The JOptionPane class in the javax._______ package provides simple dialogs for input / output (Fig. 11.2). String numberStr = JOptionPane.showInputDialog (“Enter first integer”); JOptionPane.showMessageDialog (null, “The sum is” + sum, “Sum of Two Integers”, JOptionPane.PLAIN_MESSAGE); The input dialog can input only ________. A string of numeric characters must be converted to a number. int number = Integer.parseInt(numberStr);
Overview of Swing Components Most applications need more than simple I/O dialogs. Before Java 1.2, AWT (_________ Windows Toolkit) was used to create GUIs, but these were platform dependent (heavyweight components). Most Swing components are platform independent (__________) components (pure Java, part of JFC). The inheritance hierarchy for Java GUIs is Object Component Container JComponent JComponent is the superclass of all Swing lightweight components.
Text/Images in a Window JFrame object: A window with a _______ bar and min/max/close buttons. JLabel object: Provides a single line of read-only text, an image, or both text and image. Steps in creating a GUI (see Figs and 11.7): import required classes create a container class (subclass of _________) declare components in the container class create a constructor for the container set the layout instantiate the components set component properties add components to container create an application class that instantiates the container
Introduction to Event Handling GUIs are _______ driven. An event is a user interaction with a GUI component. An event handler is code that performs a task in response to an event. A JTextField object can receive text input. A JPasswordField object does likewise, but hides the input with an ______ character. A component receives _______ when a user clicks on it. Additional steps for event handling (see Figs and 11.10): in the container constructor instantiate an event-handler object register the component with the event-handler using addActionListener create a nested event-handler class that provides functionality
Events, Interfaces, and Event Handling Three objects in the event-handling mechanism: event source object (creates the event object) event ________ (contains info about the event) event listener object (receives the event object) Registering an event source object with an event listener object: textField1.addActionListener( handler ); When an ActionEvent object is created, the JVM dispatches that object to every actionPerformed method (in the interface _________________) with which the event object is registered.
JButton Types of buttons command, check box, toggle, radio The class AbstractButton declares common features of Swing buttons. Command buttons create _______________ objects when clicked. A JButton object (command button) can call the setRolloverIcon method. (Fig )
Buttons That Maintain State Three kinds of button have state (on/off or true/false status) JToggleButton, JCheckBox, JRadioButton When a JCheckBox object is clicked, an _____________ object is created and is handled by an ItemListener object, which calls the itemStateChanged method. JRadioButton objects represent mutually exclusive options (only one in a group can be selected). JRadioButton objects must be added to a ______________ object.
Review the programs in these sections.
Layout Managers Three ways to arrange components in a GUI: _________ positioning—specify size and position of each layout managers—easier than absolute, but control is lost visual programming w/ an _____—drag, drop, resize, etc. Layout managers arrange GUI components in a container for presentation purposes. Types of layout managers FlowLayout— simplest, components flow left to right ___________—arranges components into five regions GridLayout— components are placed in an r x c grid Complex layouts are possible by using a JPanel as a component that holds other components