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
Swing vs AWT When Java was introduced, the gui classes were bundled in a library known as the abstract Windows Toolkit(AWt). For every platform on which Java runs, the awT components are automatically mapped to the platform-specific components through their respective agents, known as peers. AWT is fine for developing simple graphical user interfaces, but not for developing comprehensive GuI projects O AWT is prone to platform-specific bugs because its peer-based approach relies heavily on the underlying platform Swing components are painted directly on canvases using Java code except for components that are subclasses of java. awt. Window or java.awt. Panel, which must be drawn using native gui on a specific platform Swing components are less dependent on the target platform and use less of the native gui resource o For this reason, Swing components that don' t rely on native GUI are referred to as lightweight components, and awT components are referred to as heavyweight components Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 Swing vs. AWT ⚫When Java was introduced, the GUI classes were bundled in a library known as the Abstract Windows Toolkit (AWT). For every platform on which Java runs, the AWT components are automatically mapped to the platform-specific components through their respective agents, known as peers. ⚫AWT is fine for developing simple graphical user interfaces, but not for developing comprehensive GUI projects. ⚫AWT is prone to platform-specific bugs because its peer-based approach relies heavily on the underlying platform. ⚫ Swing components are painted directly on canvases using Java code, except for components that are subclasses of java.awt.Window or java.awt.Panel, which must be drawn using native GUI on a specific platform. ⚫Swing components are less dependent on the target platform and use less of the native GUI resource. ⚫For this reason, Swing components that don’t rely on native GUI are referred to as lightweight components, and AWT components are referred to as heavyweight components
GUI Class Hierarchy(Swing) Dimension Classes in the java.awt Layout manage Font Fontmetrics Object Color 匚Pa-K[ Applet Khi JApplet Graphics Component Container K Window KI Frame Jframe Dialog Kh JDialog JComponent in the javax. swing package Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 GUI Class Hierarchy (Swing) Dimension Font FontMetrics Component Graphics Object Color Container Panel Applet Frame Dialog Window JComponent JApplet JFrame JDialog Swing Components in the javax.swing package Lightweight Heavyweight Classes in the java.awt package 1 LayoutManager *
Container classes Dimension Classes in the java.awt Layout Manager Heavier Font FontMetrics Object Color Panel Applet K JApplet Graphics Component K Container Window KI Frame JFrame Dialog JDialog COmponen JPanel Container classes can in the javax. swing package contain other gui components Lightweight Liang, Introduction to Java Programming, revise
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 Container Classes Dimension Font FontMetrics Component Graphics Object Color Container Panel Applet Frame Dialog Window JComponent JApplet JFrame JDialog Swing Components in the javax.swing package Lightweight Heavyweight Classes in the java.awt package 1 LayoutManager * JPanel Container classes can contain other GUI components
GUI Helper Classes Dimension Classes in the java. awt Layout Manager package Heavyweight Font FontMetrics O Color 匚 Panel[Apk{ Japplet Graphi I Component K Container Window K Frame Jframe Dialog JDialog The helper classes are not ar Swing Components subclasses of Component opponet in the javax. swing package They are used to describe the properties of gui components Lightweight such as graphics context colors, fonts, and dimension. tion to Java Programming, revised by dai
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 GUI Helper Classes Dimension Font FontMetrics Component Graphics Object Color Container Panel Applet Frame Dialog Window JComponent JApplet JFrame JDialog Swing Components in the javax.swing package Lightweight Heavyweight Classes in the java.awt package 1 LayoutManager * JPanel The helper classes are not subclasses of Component. They are used to describe the properties of GUI components such as graphics context, colors, fonts, and dimension
Swing GUI Components J Check BoxMenultem JMenultem KHJMenu abstract Butto Button J Radio Button Menuitem JToggleButton KhTICheckBox. JRadioButton Component JEditorPane] JTex Component KH JText Fiels画swde TextArea JLabel DList COmbo Box] JPanel JOptionPane] [ScrollBar JSlider] JTabbedPaneJSplitPanejLayeredPane SEparator[JScrollPane jRootPane ToolBar] DMenuBar] [PopupMenu JFileChooser] JColorChooser Tool JT JTable JTableheade Internal FrameJProgressB Spinner -tang:introduction tordavar Trogrammimgreviseu by Dai-ktar
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 Swing GUI Components JMenuItem JCheckBoxMenuItem AbstractButton JComponent JMenu JRadioButtonMenuItem JToggleButton JCheckBox JRadioButton JComboBox JInternalFrame JLayeredPane JList JMenuBar JOptionPane JPopupMenu JProgressBar JFileChooser JScrollBar JSeparator JScrollPane JSplitPane JSlider JTabbedPane JTable JTableHeader JTextComponent JTextField JTextArea JToolBar JToolTip JTree JRootPane JPanel JPasswordField JColorChooser JLabel JEditorPane JSpinner JButton