Top Banner

Click here to load reader

Chapter11 graphical components

Nov 29, 2014




  • 1. Graphical User Interface Components: Part 1 Chapter 11
  • 2. 2 What You Will Learn Swing Components Event handling Mouse event handling
  • 3. 3 Graphical User Interface (GUI) Gives program distinctive look and feel Provides users with basic level of familiarity Built from GUI components (controls, widgets, etc.) o User interacts with GUI component via mouse, keyboard, etc Check out this visual index of components
  • 4. 4 Netscape Window With GUI Components menu barbutton combo boxmenus scroll bars
  • 5. 5 Dialog Boxes Used by applications to interact with the user Provided by Javas JOptionPane class o Contains input dialogs and message dialogs View example program, Figure 11.2 Title BarTitle Bar Prompt to user Prompt to user Text field which allows user input Text field which allows user input When user clicks OK, dialog box dismissed When user clicks OK, dialog box dismissed
  • 6. 6 Dialog Boxes Note icon Other icons available Message dialog type Icon Description ERROR_MESSAGE A dialog that indicates an error to the user. INFORMATION_MESSAGE A dialog with an informational message to the user. WARNING_MESSAGE A dialog warning the user of a potential problem. QUESTION_MESSAGE A dialog that poses a question to the user. This dialog normally requires a response, such as clicking a Yes or a No button. PLAIN_MESSAGE no icon A dialog that contains a message, but no icon.
  • 7. 7 Some Basic GUI Components Component Description JLabel Displays uneditable text or icons. JTextField Enables user to enter data from the keyboard. Can also be used to display editable or uneditable text. JButton Triggers an event when clicked with the mouse. JCheckBox Specifies an option that can be selected or not selected. JComboBox Provides a drop-down list of items from which the user can make a selection by clicking an item or possibly by typing into the box. JList Provides a list of items from which the user can make a selection by clicking on any item in the list. Multiple elements can be selected. JPanel Provides an area in which components can be placed and organized. Can also be used as a drawing area for graphics.
  • 8. 8 Overview Swing GUI components o Declared in package javax.swing o Most are pure Java components o Part of the Java Foundation Classes (JFC) Abstract Window Toolkit (AWT) o Precursor to Swing o Declared in package java.awt o Does not provide consistent, cross-platform look- and-feel
  • 9. 9 Lightweight vs. Heavyweight Lightweight components o Not tied directly to GUI components supported by underlying platform Heavyweight components o Tied directly to the local platform o AWT components o Some Swing components
  • 10. 10 Superclasses of Swings Lightweight GUI Components Class Component o (package java.awt) o Subclass of Object o Declares many behaviors and attributes common to GUI components
  • 11. 11 Superclasses of Swings Lightweight GUI Components Class Container o (package java.awt) o Subclass of Component o Organizes Components
  • 12. 12 Superclasses of Swings Lightweight GUI Components Class JComponent o (package javax.swing) o Subclass of Container o Superclass of all lightweight Swing components
  • 13. 13 Common Lightweight Component Features Pluggable look-and-feel o customize the appearance of components Shortcut keys o mnemonics Common event-handling capabilities Brief description of components purpose o tool tips Support for localization
  • 14. 14 Displaying Text and Images in a Window Class JFrame o Most windows are an instance or subclass of this class o Provides title bar o Provides min, max, close buttons Label o Text instructions or information stating the purpose of each component o Created with class JLabel
  • 15. 15 Three Parts of a GUI Application 1. Components that make up the Graphical User Interface 2. Listeners that receive the events and respond to them 3. Application code that does useful work for the user
  • 16. 16 Events Generated by Swing Components Act that results in the event Listener type User clicks a button, presses Return while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener
  • 17. 17 Events Generated by Swing Components Each event is represented by an object o Object gives information about the event o Identifies the event source. Event sources are typically components, o Other kinds of objects can also be event sources. Each event source can have multiple listeners registered on it. o Conversely, a single listener can register with multiple event sources.
  • 18. 18 JLabel Label o Provide text on GUI o Defined with class JLabel o Can display: Single line of read-only text Image Text and image View Figure 11.6 o Note uses of the JLabel Class
  • 19. 19 Creating and Attaching label1 Method setToolTipText of class JComponent o Specifies the tool tip Method add of class Container o Adds a component to a container
  • 20. 20 Creating and Attaching label2 Interface Icon o Can be added to a JLabel with the setIcon method o Implemented by class ImageIcon
  • 21. 21 Creating and Attaching label2 Interface SwingConstants o Declares a set of common integer constants such as those used to set the alignment of components o Can be used with methods setHorizontalAlignment and setVerticalAlignment
  • 22. 22 Creating and Attaching label3 Other JLabel methods o getText and setText For setting and retrieving the text of a label o getIcon and setIcon For setting and retrieving the icon displayed in the label o getHorizontalTextPosition and setHorizontalTextPosition For setting and retrieving the horizontal position of the text displayed in the label
  • 23. 23 Some basic GUI Components. Constant Description Horizontal-position constants SwingConstants.LEFT Place text on the left. SwingConstants.CENTER Place text in the center. SwingConstants.RIGHT Place text on the right. Vertical-position constants SwingConstants.TOP Place text at the top. SwingConstants.CENTER Place text in the center. SwingConstants.BOTTOM Place text at the bottom.
  • 24. 24 Other JFrame Methods setDefaultCloseOperation o Dictates how the application reacts when the user clicks the close button setSize o Specifies the width and height of the window setVisible o Determines whether the win
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.