Nội dung text Unit-1.pdf
Chapter 1 (a) : Graphics Programming ............................................................. 537 1.1 Introducing Swing ..................................................................................... 538 1.2 Creating a Frame ...................................................................................... 543 1.3 Positioning a Frame .................................................................................. 546 1.3.1 Frame Properties ........................................................................... 549 1.3.2 Determining a Good Frame Size .................................................. 549 1.4 Displaying Information in a Component .................................................... 554 1.5 Working with 2D Shapes .......................................................................... 560 1.6 Using Color ............................................................................................... 569 1.7 Using Special Fonts for Text ..................................................................... 573 1.8 Displaying Images .................................................................................... 582 This chapter starts you on the road to writing Java programs that use a graphical user interface (GUI). In particular, you will learn how to write programs that size and locate windows on the screen, display text with multiple fonts in a window, display images, and so on. 1.1 Introducing Swing • When Java 1.0 was introduced, it contained a class library, which Sun called the Abstract Window Toolkit (AWT), for basic GUI programming. • In theory, run on any of these platforms, with the “look-and-feel” of the target platform—hence Sun’s trademarked slogan: “Write Once, Run Anywhere.” • Developers complained that they had to test their applications on each platform—a practice called “write once, debug everywhere.” • In 1996, Netscape created a GUI library they called the IFC (Internet Foundation Classes) that used an entirely different approach. User interface elements, such as buttons, menus, and so on, were painted onto blank windows. • Sun worked with Netscape to perfect this approach, creating a user interface library with the code name “Swing.” Swing was available as an extension to Java 1.1 and became a part of the standard library in Java SE 1.2. • Swing is now the official name for the non-peer-based GUI toolkit. • Swing is part of the Java Foundation Classes (JFC). • Swing-based user interface elements will be somewhat slower to appear on the user’s screen than the peer- based components used by the AWT. • On the other hand, the reasons to choose Swing are overwhelming: • Swing has a rich and suitableset of user interface elements. • Swing has few dependencies on the underlying platform; it is therefore less liable to platform-specific bugs. • Swing gives a compatible user experience across platforms. Still, the third plus is also a potential drawback: If the user interface elements look the same on all platforms, they look different from the native controls, so users will be less familiar with them. 1.2 Creating a Frame A top-level window (that is, a window that is not contained inside another window) is called a frame in Java. The Abstract Window Toolkit (AWT) library has a class, called Frame, for this top level. The Swing version of this class is called JFrame and extends the Frame class. Thus, the decorations (buttons, title bar, icons, and so on) are drawn by the user’s windowing system, not by Swing. bcanepaltu.com