Chapter 1l Getting Started with GUI Programming Prerequisi七es王 or part工工工 Chapter 8 Inheritance and Polymorphism Chapter 9 Abstract Classes and Interfaces Chapter 11 Getting Started with GUI Programming Chapter 12 Event-Driven Programmin hapter 13 Creating User Interfaces 百闻不如一见 Chapter 14 Applets, Images, Audio Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 11 Getting Started with GUI Programming Prerequisites for Part III Chapter 11 Getting Started with GUI Programming Chapter 8 Inheritance and Polymorphism Chapter 13 Creating User Interfaces Chapter 14 Applets, Images, Audio Chapter 9 Abstract Classes and Interfaces Chapter 12 Event-Driven Programming 百闻不如一见
Objectives ● To distinguish simple GuI components(§1l2) o To describe the Java GUI API hierarchy(8 11.3) To create user interfaces using frames, panels, and simple Ul components (§114) o To understand the role of layout managers($ 11.5) To use the Flowlayout, GridLayout, and Border layout managers to layout components in a container(8 11.5) o To specify colors and fonts using the Color and Font classes(s 11.6-11.7) ● To use jPanel as subcontainers(§11.8) o To paint graphics using the paint Component method on a panel($ 11.9) To draw strings, lines, rectangles, ovals, arcs, and polygons using the drawing methods in the graphics class($ 11.9 o To center display using the FontMetrics Class(811.10) To develop a reusable component Message Panel to display a message on a panel (§111) o to develop a reusable component StillClock to emulate an analog clock(s 11.12 Optional) Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives ⚫ To distinguish simple GUI components (§11.2). ⚫ To describe the Java GUI API hierarchy (§11.3). ⚫ To create user interfaces using frames, panels, and simple UI components (§11.4). ⚫ To understand the role of layout managers (§11.5). ⚫ To use the FlowLayout, GridLayout, and BorderLayout managers to layout components in a container (§11.5). ⚫ To specify colors and fonts using the Color and Font classes (§11.6-11.7). ⚫ To use JPanel as subcontainers (§11.8). ⚫ To paint graphics using the paintComponentmethod on a panel (§11.9). ⚫ To draw strings, lines, rectangles, ovals, arcs, and polygons using the drawing methods in the Graphics class (§11.9). ⚫ To center display using the FontMetricsClass (§11.10). ⚫ To develop a reusable component MessagePanel to display a message on a panel (§11.11). ⚫ To develop a reusable component StillClock to emulate an analog clock (§11.12 Optional)
Introduction o Graphical User Interface(GUD) oGiⅤ es program distinctive look and“feel O Provides users with basic level of familiarity O Built from GUI components(controls, widgets, etc.) o User interacts with gui component via mouse keyboard, etc 回x marks Tasks Help nchttp://www.deitel.com/ A Home A My Netscape& Net2Phone hstart Message WebMad Rodo People A Yellow Pages Download Calendar ia grammng Trainng Delvered Worldwide(C DEE[端 &ASSOCiAIES INC, 990B Boston Post Road, Sute 200, Sudbury, MA01776 porate Traning Curculin Publications/ Book Store What's New/Upcoming Full-time and Summer-Internship Job Opportunities Available Immediately for Computer Science and/or Information AQ(Frequently Asked Technology Graduates and Students at our Sudbury, MA location
Liang,Introduction to Java Programming,revised by Dai-kaiyu 3 Introduction ⚫ Graphical User Interface (GUI) Gives program distinctive “look” and “feel” Provides users with basic level of familiarity Built from GUI components (controls, widgets, etc.) ⚫User interacts with GUI component via mouse, keyboard, etc
Some basic gui components Component Description JLabel jAn area where uneditable text or icons can be displayed. JTextField An area in which the user inputs data from the keyboard. The area can also display information BUtt。n An area that triggers an event when clicked CHeckbOx A GUI component that is either selected or not selected COmbo。x A drop-down list of items from which the user can make a selection by clicking an item in the list or possibly by typing into the box is七 An area where a list of items is displayed from which the user can make a selection by clicking once on any element in the list Double-clicking an element in the list generates an action event Multiple elements can be selected gig. 12.2 Some ba sic GUI components as can be placed. JPanel JA container in which compone Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 4 Some basic GUI components. Component Description JLabel An area where uneditable text or icons can be displayed. JTextField An area in which the user inputs data from the keyboard. The area can also display information. JButton An area that triggers an event when clicked. JCheckBox A GUI component that is either selected or not selected. JComboBox A drop-down list of items from which the user can make a selection by clicking an item in the list or possibly by typing into the box. JList An area where a list of items is displayed from which the user can make a selection by clicking once on any element in the list. Double-clicking an element in the list generates an action event. Multiple elements can be selected. JPanel A container in which components can be placed. Fig. 12.2 Some basic GUI c omponents
Creating gul objects // Create a button with text OK J Button jbtOK-new JButton"OK); l Create a label with text Enter your name JLabel jlbIName = new JLabel("Enter your name: " abel Text Check Rad field Button Display GUI Components Button OK Enter your name: Type Name Here [ Bold O Red Red Green l Create a text field with text" Type Name Here Combo JText F ield jtfName =new JTextField("Type Name Here") l Create a check box with text bold JCheck Box jchk Bold=new JCheck Box("Bold") // Create a radio button with text red JRadioButton jrbRed=new JRadio Button("Red"); l Create a combo box with choices red, green, and blue JComboBox jco Color==new JComboBox(new Stringl("Red Green"," Blue")) Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 5 Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your name: " JLabel jlblName = new JLabel("Enter your name: "); // Create a text field with text "Type Name Here" JTextField jtfName = new JTextField("Type Name Here"); // Create a check box with text bold JCheckBox jchkBold = new JCheckBox("Bold"); // Create a radio button with text red JRadioButton jrbRed = new JRadioButton("Red"); // Create a combo box with choices red, green, and blue JComboBox jcboColor = new JComboBox(new String[]{"Red", "Green", "Blue"}); Button Label Text field Check Box Radio Button Combo Box