1 / 51

Lecture 12 Objectives

Lecture 12 Objectives. Learn about applets. Explore the class Graphics . Learn about the class Font . Explore the class Color . Learn to use additional layout managers. Become familiar with more GUI components. Learn how to create menu-based programs.

jana
Download Presentation

Lecture 12 Objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 12 Objectives • Learn about applets. • Explore the classGraphics. • Learn about the classFont. • Explore the classColor. • Learn to use additional layout managers. • Become familiar with more GUI components. • Learn how to create menu-based programs. • Explore how to handle key and mouse events. Java Programming: From Problem Analysis to Program Design, Second Edition

  2. Inheritance Hierarchy of GUI Classes Java Programming: From Problem Analysis to Program Design, Second Edition

  3. Constructors and Methods of the classComponent Java Programming: From Problem Analysis to Program Design, Second Edition

  4. Constructors and Methods of the classComponent Java Programming: From Problem Analysis to Program Design, Second Edition

  5. Constructors and Methods of the classComponent Java Programming: From Problem Analysis to Program Design, Second Edition

  6. Constructors and Methods of the classComponent Java Programming: From Problem Analysis to Program Design, Second Edition

  7. Applets • A Java program that is embedded within a Web page and executed by a Web browser. • Create an applet by extending the classJApplet. • classJApplet is contained in package javax.swing. Java Programming: From Problem Analysis to Program Design, Second Edition

  8. Applets Java Programming: From Problem Analysis to Program Design, Second Edition

  9. Applets Java Programming: From Problem Analysis to Program Design, Second Edition

  10. Applets • No main method. • Methods init, start, and paint guaranteed to be invoked in sequence. • To develop an applet: • Override any/all of the methods above. Java Programming: From Problem Analysis to Program Design, Second Edition

  11. Applet Methods • init method: • Initializes variables. • Gets data from user. • Places various GUI components. • paint method: • Performs output. Java Programming: From Problem Analysis to Program Design, Second Edition

  12. Skeleton of a Java Applet import java.awt.Graphics; import javax.swing.JApplet; public classWelcomeApplet extends JApplet { } Java Programming: From Problem Analysis to Program Design, Second Edition

  13. Applet Displaying Welcome Message //Welcome Applet import java.awt.Graphics; import javax.swing.JApplet; public class WelcomeApplet extends JApplet { public void paint(Graphics g) { super.paint(g); //Line 1 g.drawString("Welcome to Java Programming" , 30, 30); //Line 2 } } Java Programming: From Problem Analysis to Program Design, Second Edition

  14. HTML to Run Applet Java Programming: From Problem Analysis to Program Design, Second Edition

  15. classFont • Shows text in different fonts. • Contained in package java.awt. • Available fonts: • Serif/Sans Serif • Monospaced • Dialog/DialogInput • Arguments for constructor: • String specifying the font face name. • int value specifying font style. • int value specifying font size. • Expressed in points (72 points = 1 inch) . Java Programming: From Problem Analysis to Program Design, Second Edition

  16. classFont Java Programming: From Problem Analysis to Program Design, Second Edition

  17. classColor • Shows text in different colors. • Changes background color of component. • Contained in package java.awt. Java Programming: From Problem Analysis to Program Design, Second Edition

  18. classColor Java Programming: From Problem Analysis to Program Design, Second Edition

  19. classColor Java Programming: From Problem Analysis to Program Design, Second Edition

  20. classColor Java Programming: From Problem Analysis to Program Design, Second Edition

  21. classGraphics • Provides methods for drawing items such as lines, ovals, and rectangles on the screen. • Contains methods to set the properties of graphic elements including clipping areas, fonts, and colors. • Contained in the package java.awt. Java Programming: From Problem Analysis to Program Design, Second Edition

  22. classGraphics Java Programming: From Problem Analysis to Program Design, Second Edition

  23. Constructors and Methods for the classGraphics Java Programming: From Problem Analysis to Program Design, Second Edition

  24. Constructors and Methods for the classGraphics Java Programming: From Problem Analysis to Program Design, Second Edition

  25. Constructors and Methods for the classGraphics Java Programming: From Problem Analysis to Program Design, Second Edition

  26. Constructors and Methods for the classGraphics Java Programming: From Problem Analysis to Program Design, Second Edition

  27. Applets Derived from JApplet. No main method. Uses init method. Displayed by HTML. Sets title in HTML. Size set in HTML. Applet closes when HTML doc closes. GUI applications Class extends JFrame. Invokes main method. Uses constructors. Uses method setVisible. Uses setTitle method. Uses method setSize. Closes with Exit button. Differences Between Applets and GUI Applications Java Programming: From Problem Analysis to Program Design, Second Edition

  28. Converting a GUI Application to an Applet • Change JFrame to JApplet. • Change constructor to method init. • Remove method calls such as setVisible, setTitle, setSize. • Remove the method main. • If applicable, remove Exit button and all code associated with it (for example, action listener). Java Programming: From Problem Analysis to Program Design, Second Edition

  29. Additional GUI Components • JTextArea • JCheckBox • JRadioButton • JComboBox • JList Java Programming: From Problem Analysis to Program Design, Second Edition

  30. JTextArea • Can collect multiple lines of input from user. • Can display multiple lines of output. • Pressing Enter key separates lines of text. • Each line ends with newline character (\n). • Derived from class JTextComponent. Java Programming: From Problem Analysis to Program Design, Second Edition

  31. JTextArea Java Programming: From Problem Analysis to Program Design, Second Edition

  32. JTextArea Java Programming: From Problem Analysis to Program Design, Second Edition

  33. JTextArea Example Java Programming: From Problem Analysis to Program Design, Second Edition

  34. JCheckBox • User selects from predefined values. • Example of a toggle button. • Clicking JCheckBox generates item event. • Use interfaceItemListener and its abstract method itemStateChanged to handle event. Java Programming: From Problem Analysis to Program Design, Second Edition

  35. JCheckBox Java Programming: From Problem Analysis to Program Design, Second Edition

  36. Constructors and Methods of classJCheckBox Java Programming: From Problem Analysis to Program Design, Second Edition

  37. Constructors and Methods of classJCheckBox Java Programming: From Problem Analysis to Program Design, Second Edition

  38. JRadioButton • Created same way as check boxes. • Placed in content pane of applet. • Forces user to select only one radion button at a time. • You create a button group to group radio buttons. • Generates an ItemEvent. • interfaceItemListener and method itemStateChanged used to handle events. Java Programming: From Problem Analysis to Program Design, Second Edition

  39. JRadioButton Java Programming: From Problem Analysis to Program Design, Second Edition

  40. JRadioButton Java Programming: From Problem Analysis to Program Design, Second Edition

  41. JComboBox • Commonly known as a drop-down list. • Used to select an item from a list of possibilities. • Generates an ItemEvent. • Event monitored by ItemListener. • ItemListener invokes method itemStateChanged. Java Programming: From Problem Analysis to Program Design, Second Edition

  42. JComboBox Java Programming: From Problem Analysis to Program Design, Second Edition

  43. JComboBox Java Programming: From Problem Analysis to Program Design, Second Edition

  44. JList Java Programming: From Problem Analysis to Program Design, Second Edition

  45. JList Java Programming: From Problem Analysis to Program Design, Second Edition

  46. Layout Managers • FlowLayout • Default layout manager. • Places components from left to right until no more items can be placed. • Can align each line left, center, or right. • Default alignment: LEFT. • GridLayout • Similar to FlowLayout. • All rows (columns) have same number of components. • All components have the same size. Java Programming: From Problem Analysis to Program Design, Second Edition

  47. Layout Managers • BorderLayout • Items placed into one of five specific regions: • NORTH/SOUTH • EAST/WEST • CENTER • NORTH and SOUTH components extend horizontally (completely span one edge to the other). • EAST and WEST components extend vertically between components in NORTH and SOUTH regions. • CENTER component expands to occupy any unused regions. Java Programming: From Problem Analysis to Program Design, Second Edition

  48. Menus • Allow for various functions without cluttering GUI with too many components. • Can be attached to objects such as JFrame and JApplet(setJMenuBar method). • To set a menu bar: private JMenuBar menuMB = new JMenuBar(); setJMenuBar(menuMB); • Add menus to menu bar; add menu items to menu. • Order of menus added = order of appearance. Java Programming: From Problem Analysis to Program Design, Second Edition

  49. Keyboard and Mouse Events Java Programming: From Problem Analysis to Program Design, Second Edition

  50. Lecture 12 Summary • Creating applets • classFont • classGraphics • classColor • Differences between applets and GUI applications • Converting GUI applications to applets Java Programming: From Problem Analysis to Program Design, Second Edition

More Related