This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 1 Chapter 12 W INDOW I NTERFACES U SING S WING O BJECTS W INDOW I NTERFACES U SING S WING O BJECTS 12.1 B ACKGROUND 3 GUIsGraphical User Interfaces 3 Event-Driven Programming 4 12.2 B ASIC S WING D ETAILS 5 Gotcha Save All Your Work Before Running a Swing Program 6 Programming Example A Simple Window 6 Programming Tip Ending a Swing Program 11 Gotcha Forgetting to Program the Close-Window Button 11 Gotcha Forgetting to Use getContentPane 12 More About Window Listeners 12 Size Units for Screen Objects 12 More on setVisible 14 Programming Example A Better Version of Our First Swing Program 16 Programming Example A Window with Color 19 Some Methods of the Class JFrame 23 Layout Managers 26 Programming Tip Copy Other Pro- grammers Code 32 12.3 B UTTONS AND A CTION L ISTENERS 34 Programming Example Adding Buttons 34 Buttons 37 Action Listeners and Action Events 37 Gotcha Changing The Parameter List for actionPerformed 42 Programming Tip Code Look and Actions Separately 43 Programming Tip Use the Method setActionCommand 44 12.4 C ONTAINER C LASSES 46 The JPanel Class 47 The Container Class 50 Programming Tip Guide for Creating Simple Window Interfaces 53 12.5 T EXT I/O FOR GUI S 55 Text Areas and Text Fields 56 Programming Example Labeling a Text Field 60 Inputting and Outputting Numbers 64 Case Study A GUI Adding Machine 67 Catching a NumberFormat- Exception (Optional) 74 Chapter Summary 77 Answers to Self-Test Questions 78 Programming Exercises 85 W INDOW I NTERFACES U SING S WING O BJECTS W INDOW I NTERFACES U SING S WING O BJECTS What is the use of a book, thought Alice, without pictures or conversations? LEWIS CARROLL, Alices Adventures in Wonderland So far, your programs have used the simplest form of input. The user enters simple text at the keyboard, and simple, unadorned text is sent to the screen as output. We have kept the input and output simple to concentrate on other basic features of pro- gramming and the Java language. But, modern programs do not use such simple in- put and output. Modern programs use windowing interfaces with such features as menus and buttons that allow the user to make choices with a mouse. In this chapter and Chap- ter 14, you will learn how to write Java programs that create such modern window- ing interfaces for input and output using a special library of classes called Swing . Swing is a standard library that comes with all versions of Java 2 (also known as JDK 1.2 and as SDK 1.2). (Higher numbered versions should also include the Swing library.) Entire books have been written on Swing, and so we will not have room to give you a complete description of Swing in two chapters. However, in this chapter we will teach you enough to allow you to write simple windowing inter- faces, and in Chapter 14 we will give you some more advanced details about design- ing windowing systems with Swing....
View Full Document