Page 714 - Introduction to Programming with Java: A Problem Solving Approach
P. 714

                680 Chapter 16 GUI Programming Basics
Macintosh look and feel if the program is run on a Macintosh computer, but a Windows look and feel if the
5
program is run on a Windows computer. That leads to portability issues. Your programs are still portable
in the sense that they’ll run on different platforms. But they’ll run differently on different platforms. If you have a persnickety customer who demands one precise appearance on all platforms, then AWT components probably won’t be satisfactory.
One of Java’s strongest selling points was (and is) its portability, so soon after Java’s initial release, the folks at Sun set out to develop a set of more portable GUI components. They put their new, more-portable components in a brand new library named Swing. To make the relationship clear between the new Swing components and the AWT components, they used the same component names except that they prefaced the new Swing components with a “J.” For example, the AWT has a Button component, so Swing has a JButton component.
The AWT GUI components are known as heavyweight components, while the Swing GUI components are known as lightweight components. The AWT components are heavyweight because they are built with graphics commands that are part of the computer’s platform. Being part of the computer’s platform, they’re too “heavy” to move to other platforms. Swing components are lightweight because they’re built with Java code. Being built with Java code means that they’re “light” enough to move to different platforms.
The Swing library includes quite a bit more than just GUI component classes. It adds lots of functional-
ity to the AWT, but it does not replace the AWT entirely. Today, Java GUI application programmers use both 6
libraries—the AWT and Swing. The primary AWT packages are java.awt and java.awt.event. The primary Swing package is javax.swing. The “x” in javax stands for “extension” because the javax packages (javax.swing is one of several javax packages) are considered to be a major exten- sion to the core Java platform.
            Apago PDF Enhancer
16.18 Mouse Listeners and Images (Optional)
Sun provides several different types of listeners. Earlier in this chapter, you learned about the most common listener—the ActionListener. You should use the ActionListener for events where the user does something to a component, such as clicking a button or pressing Enter within a text box. In this section, you’ll learn about mouse listeners. As the name implies, you should use mouse listeners for events where the user does something with the mouse. Also in this section, you’ll learn about images (pictures). You’ll learn how to display an image and drag an image with your mouse.
Mouse Listeners
In creating a mouse listener, you use the same basic steps that you use for the ActionListener—you define a listener class, you define an event handler method(s) within the listener class, and you register your listener class with a component. Although the same basic steps are used, mouse listeners are slightly more complicated than the ActionListener. There are several different types of mouse listeners, and each type of mouse listener handles multiple types of mouse events.
We describe two mouse listener types, and they are defined by their two interfaces—MouseListener and MouseMotionListener. Figure 16.15 shows the API headings and descriptions for the methods
5 Look and feel is a standard GUI term, and it refers to the appearance of something and the way in which the user interacts with it.
6 Java applet programmers typically use the AWT only, even for GUI components, and do not use the Swing library at all. Why? Because applets rely on browsers and, sadly, many of today’s browsers use old versions of Java, versions that don’t include Swing.
  
















































































   712   713   714   715   716