260 likes | 366 Views
Java Programming, Second Edition. Chapter Fourteen Using Layout Managers and the Event Model. In this chapter, you will:. Learn about layout managers Use JPanels Learn about advanced layout managers Understand events and event handling Use the AWTEvent class methods
E N D
Java Programming, Second Edition Chapter Fourteen Using Layout Managers and the Event Model
In this chapter, you will: Learn about layout managers Use JPanels Learn about advanced layout managers Understand events and event handling Use the AWTEvent class methods Use event methods from higher in the inheritance hierarchy Handle mouse events
Learning about Layout Managers • Layout manager- An interface class that is part of the JDK • Aligns your components so they neither crowd each other nor overlap • Can assign layout managers within layout managers • For example: • One layout manager arranges components in equally spaced columns and rows
Learning about Layout Managers • BorderLayout manager- The default manager for all content panes • Can use the BorderLayout class with any container that has five or fewer components • Components fill the screen with five regions named North, South, East, West, and Center • The compiler determines the exact size of each component based on the components contents • When BorderLayout is used with less than five components, any empty regions disappear
Learning about Layout Managers • FlowLayout- To arrange Components in rows across the width of a container • Each component retains its default size • Each component that you add is placed to the right of previously added components
Learning about Layout Managers • GridLayout- To arrange Components into equal rows and columns • You indicate the numbers of rows and columns you want, and then the container surface is divided into a grid • Specify rows first, columns second • As you add components they are positioned left-to-right across each row in sequence
Learning about Layout Managers • CardLayout- Generates a stack of containers or components, one on top of another • Each container in the group is referred to as a card • Only one component is visible at a time • A card layout is created from the CardLayout class using one of two constructors • CardLayout() creates a new card layout without a horizontal or vertical gap • CardLayout(int hgap, int vgap) creates a new card layout with the specified horizontal and vertical gaps
Using JPanels • JPanels- Increase the number of possible component arrangements by using the JPanel class • A JPanel is similar to a JWindow in that a JPanel is a surface on which you can place components • JPanel is a Container so it can contain other components • By using JPanels within JPanels, you can create an infinite variety of screen layouts
Using JPanels • JPanel object constructors • JPanel() creates a new JPanel with a double buffer and a flow layout • JPanel(LayoutManager layout) creates a new buffered JPanel with the specified layout manager
Learning about Advanced Layout Managers • GridBagLayout- Allows you to add components to precise locations within the grid, as well as to indicate that specific Components should span multiple rows or columns within the grid • This class is difficult to use because you must set the position and size for each component
Learning about Advanced Layout Managers • BoxLayout- Components are arranged in either a single row or a single column • The box layout manager tries to make all the components the same height(row) or width(column) so components do not spill over
BoxLayout • Requires two arguments • The first refers to the container to which the layout manager applies • The second is a constant • BoxLayout.X_Axis for a row arrangement • BoxLayout.Y_Axis for a column arrangement
Understanding Events and Event Handling • Events are Objects that the user initiates • For example a mouse click or a key press • The parent class for all event objects is EventObject • Event object descends from the Object class • EventObject is the parent class to AWTEvent which is the parent to specific classes such as ActionEvent
Understanding Events and Event Handling • When you want to listen for an event, you can implement an appropriate interface for the class such as ActionListener or Window Listener • The class becomes an event listener • For every event class there is a similarly named listener • Every <name> listener interface method has a return type of void, and each takes one argument
Understanding Events and Event Handling • Event handlers- Interface methods, such as actionPerformed(), that are called automatically when an appropriate event occurs
Understanding Events and Event Handling • The KeyListener interface- Used when you are interested in events that users initiate from the keyboard • keyPressed()- provides information about keys that don’t generate characters • keyTyped()- provides information about what key was typed • keyReleased()- provides information about keys that don’t generate characters
Using AWTEvent Class Methods • AWTEvent Class methods- Use many of these methods to determine the nature of and the facts about an event in question • The AWTEvent classes themselves contain methods
Using Event Methods from Higher in the Inheritance Hierarchy • When you use an event such as KeyEvent, you can use any of the event’s methods • Because of inheritance, you can also use methods that belong to any class that is a superclass of the event with which you are working
Handling Mouse Events • When you write GUI programs, you probably expect users to spend most of their time operating a mouse • You are interested in more than key presses • The MouseListener interface provides you with methods
Mouse Events • mouseDragged() • mouseMoved() • mousePressed() • mouseClicked() • mouseReleased() • mouseEntered() • mouseExited()