内容还是很多的,如果一条条看下去,容易走神。最好是通过调试示例代码或者实际项目驱动学习。
Section12.6 Text Fields and an Introduction to Event Handling with Nested Classes
• GUIs are event driven—when the user interacts with a GUI component, events (p. 485) drive the program to perform tasks.
• An event handler (p. 485)performs a task in response to an event.
• Class JTextField (p. 485) extends JTextComponent (p. 485) of package javax.swing.text, which provides common text-based component features. Class JPasswordField (p. 485) extends JTextField and adds several methods that are specific to processing passwords.
• A JPasswordField (p. 485) shows that characters are being typed as the user enters them, but hides the actual characters with echo characters (p. 485).
• A component receives the focus (p. 486) when the user clicks the component.
• JTextComponent method setEditable (p. 488) can be used to make a text field uneditable.
• To respond to an event for a particular GUI component, you must create a class that represents the event handler and implements an appropriate event-listener interface (p. 488), then register
an object of the event-handling class as the event handler (p. 488).
• Non-static nested classes (p. 488) are called inner classes and are frequently used for event handling.
• An object of a non-static inner class (p. 488) must be created by an object of the top-level class (p. 488) that contains the inner class.
• An inner-class object can directly access the instance variables and methods of its top-level class.
• A nested class that’s static does not require an object of its top-level class and does not implicitly have a reference to an object of the top-level class.
• Pressing Enter in a JTextField or JPasswordField generates an ActionEvent (p. 489) that can be handled by an ActionListener (p. 489) of package java.awt.event.
• JTextField method addActionListener (p. 489) registers an event handler for a text field’s ActionEvent.
• The GUI component with which the user interacts is the event source (p. 490).
• An ActionEvent object contains information about the event that just occurred, such as the event source and the text in the text field.
• ActionEvent method getSource returns a reference to the event source. ActionEvent method getActionCommand (p. 490) returns the text the user typed in a text field or the label on a JButton.
• JPasswordField method getPassword (p. 490) returns the password the user typed.
Section12.7 Common GUI Event Types and Listener Interfaces
• Each event-object type typically has a corresponding event-listener interface that specifies one or more event-handling methods, which must be declared in the class that implements the interface.
Section12.8 How Event Handling Works
• When an event occurs, the GUI component with which the user interacted notifies its registered listeners by calling each listener’s appropriate event-handling method.
• Every GUI component supports several event types. When an event occurs, the event is dispatched (p. 494) only to the event listeners of the appropriate type.
Section12.9 JButton
• A button is a component the user clicks to trigger an action. All the button types are subclasses of AbstractButton (p. 495; package javax.swing). Button labels (p. 495) typically use book-title
capitalization (p. 495).
• Command buttons (p. 495)are created with class JButton.
• A JButton can display an Icon. A JButton can also have a rollover Icon (p. 497)—an Icon that’s displayed when the user positions the mouse over the button.
• Method setRolloverIcon (p. 498) of class AbstractButton specifies the image displayed on a button when the user positions the mouse over it.
Section12.10 Buttons That Maintain State
• There are three Swing state button types—JToggleButton (p. 498), JCheckBox (p. 498) and JRadioButton (p. 498).
• Classes JCheckBox and JRadioButton are subclasses of JToggleButton.
• Component method setFont (p. 500) sets the component’s font to a new Font object (p. 500) of package java.awt.
• Clicking a JCheckBox causes an ItemEvent (p. 501) that can be handled by an ItemListener (p. 501) which defines method itemStateChanged (p. 501). Method addItemListener registers
the listener for the ItemEvent of a JCheckBox or JRadioButton object.
• JCheckBox method isSelected determines whether a JCheckBox is selected.
• JRadioButtons have two states—selected and not selected. Radiobuttons (p. 495) normally appear as a group (p. 501) in which only one button can be selected at a time.
• JRadioButtons are used to represent mutually exclusive options (p.501).
• The logical relationship between JRadioButtons is maintained by a ButtonGroup object (p. 501).
• ButtonGroup method add (p. 504) associates each JRadioButton with a ButtonGroup. If more than one selected JRadioButton object is added to a group, the selectedone that was added first
will be selected when theGUI is displayed.
• JRadioButtons generate ItemEvents when they’re clicked.
Section12.11 JComboBox; Using an Anonymous Inner Class for Event Handling
• A JComboBox (p. 504) provides a list of items from which the user can make a single selection. JComboBoxes generate ItemEvents.
• Each item in a JComboBox has an index (p. 507). The first item added to a JComboBox appears as the currently selected item when the JComboBox is displayed.
• JComboBox method setMaximumRowCount (p. 507) sets the maximum number of elements that are displayed when the user clicks the JComboBox.
• An anonymous inner class(p. 507) is a class without a name and typically appears inside a method declaration. One object of the anonymous inner class must be created when the class is declared.
• JComboBox method getSelectedIndex (p. 508) returns the index of the selected item.
Section12.12 JList
• A JList displays a series of items from which the user may select one or more items. Class JList supports single-selection lists (p. 508) and multiple-selection lists.
• When the user clicks an item in a JList, a ListSelectionEvent (p. 508) occurs. JList method addListSelectionListener (p. 510) registers a ListSelectionListener (p. 510) for a JList’s selection events. A ListSelectionListener of package javax.swing.event(p. 492) must implement method valueChanged.
• JList method setVisibleRowCount (p. 510) specifies the number of visible items in the list.
• JList method setSelectionMode (p. 510) specifies a list’s selection mode.
• A JList can be attached to a JScrollPane (p. 510) to provide a scrollbar for the JList.
• JFrame method getContentPane (p. 510) returns a reference to the JFrame’s content pane where GUI components are displayed.
• JList method getSelectedIndex (p. 511) returns the selected item’s index.
Section12.13 Multiple-Selection Lists
• A multiple-selection list (p. 511) enables the user to select many items from a JList.
• JList method setFixedCellWidth (p. 513) sets a JList’s width. Method setFixedCellHeight (p. 513) sets the height of each item in a JList.
• Normally, an external event (p. 513) generated by another GUI component (such as a JButton) specifies when the multiple selections in a JList should be processed.
• JList method setListData (p. 513) sets the items displayed in a JList. JList method getSelectedValues (p. 513) returns an array of Objects representing the selected items in a JList.
Section12.14 Mouse Event Handling
• The MouseListener (p. 513) and MouseMotionListener (p. 513) event-listener interfaces are used to handle mouse events (p. 513). Mouse events can be trapped for any GUI component that
extends Component.
• Interface MouseInputListener (p. 513) of package javax.swing.event extends interfaces MouseListener and MouseMotionListener to create a single interface containing all their methods.
• Each mouse event-handling method receives a MouseEvent object (p. 494) that contains information about the event, including the x- and y-coordinates where the event occurred. Coordinates
are measured from the upper-left corner of the GUI component on which the event occurred.
• The methods and constants of class InputEvent (p. 514; MouseEvent’s superclass) enable an application to determine which mouse button the user clicked.
• Interface MouseWheelListener (p. 515) enables applications to respond to mouse-wheel events.
Section12.15 Adapter Classes
• An adapter class (p.518) implements an interface and provides default implementations of its methods. When you extend an adapter class, you can override just the method(s) you need.
• MouseEvent method getClickCount (p. 521) returns the number of consecutive mouse-button clicks. Methods isMetaDown (p. 528) and isAltDown (p. 521) determine which button was clicked.
Section12.16 JPanel Subclass for Drawing with the Mouse
• JComponents method paintComponent (p. 522) is called when a lightweight Swing component is displayed. Override this method to specify how to draw shapes using Java’s graphics capabilities.
• When overriding paintComponent, call the superclass version as the firststatement in the body.
• Subclasses of JComponent support transparency. When a component isopaque (p. 522), paint- Component clears its background before the component is displayed.
• The transparency of aSwing lightweight component can be set with method setOpaque (p. 522; a false argument indicates that the component istransparent).
• Class Point (p. 523) package java.awt represents an x-y coordinate.
• Class Graphics (p. 523) is used to draw.
• MouseEvent method getPoint (p. 524) obtains the Point where a mouse event occurred.
• Method repaint (p. 524), inherited indirectly from class Component, indicates that a component
should be refreshed onthe screen as soon as possible.
• Method paintComponent receives a Graphics parameter and is called automatically whenever a lightweight component needs to be displayed on the screen.
• Graphics method fillOval (p. 524) draws a solid oval. The first two arguments arethe upper-left x-y coordinate of the bounding box, and the last two are the bounding box’s width and height.
Section12.17 Key Event Handling
• Interface KeyListener (p. 494) is used to handle key events (p.494) that are generated when keys on the keyboard are pressed and released. Method addKeyListener of class Component (p. 525) registers a KeyListener.
• KeyEvent (p. 494) method getKeyCode (p. 528) gets the virtual key code (p. 528) of the pressed key. Class KeyEvent contains virtual key-code constants that represent every key on the keyboard.
• KeyEvent method getKeyText (p. 528) returns a string containing the name of the pressed key.
• KeyEvent method getKeyChar (p. 528) gets the Unicode value of the character typed.
• KeyEvent method isActionKey (p. 528) determines whether the key in an event was an action key (p. 525).
• InputEvent method getModifiers (p. 528) determines whether any modifier keys (such as Shift, Alt and Ctrl) were pressed when the key event occurred.
• KeyEvent method getKeyModifiersText (p. 528) returns a string containing thepressed modifier
keys.
Section12.18 Introduction to Layout Managers
• Layout managers (p. 528)arrange GUI components in a container for presentation purposes.
• All layout managers implement the interface LayoutManager (p. 528) of package java.awt.
• Container method setLayout specifies the layout of a container.
• FlowLayout places components left to right in the order in which they’re added to the container. When the container’s edge is reached, components continue to display on the next line. FlowLayout allows GUI components to be left aligned, centered (the default) and right aligned.
• FlowLayout method setAlignment (p. 532) changes the alignment for a FlowLayout.
• BorderLayout (p. 532) the default for a JFrame) arranges components into five regions: NORTH, SOUTH, EAST, WEST and CENTER. NORTH corresponds to the top of the container.
• A BorderLayout limits a Container to containing at most five components—one in each region.
• GridLayout (p. 536) divides a container into a grid of rows andcolumns.
• Container method validate (p. 537) recomputes a container’s layout based on thecurrent layout manager for the Container and the current set of displayed GUIcomponents.
Section12.19 Using Panels to Manage More Complex Layouts
• Complex GUIs oftenconsist of multiple panels with different layouts. Every JPanel may have components, including other panels, attached to it with Container method add.
Section12.20 JTextArea
• A JTextArea (p. 539)—a subclass of JTextComponent—may contain multiplelines of text.
• Class Box (p. 540) is a subclass of Container that uses a BoxLayout layout manager (p. 541) to arrange the GUI components either horizontally or vertically.
• Box static method createHorizontalBox (p. 541) creates a Box that arranges components from left to right in the order that they’re attached.
• Method getSelectedText (p. 541) returns the selected text from a JTextArea.
• You can set the horizontal and vertical scrollbar policies (p. 542) of a JScrollPane when it’s constructed. JScrollPane methods setHorizontalScrollBarPolicy (p. 542) and setVertical-ScrollBarPolicy (p. 542) can be used to change the scrollbar policies at any time.