1 / 44

Java Programming, Second Edition

Java Programming, Second Edition. Chapter Nine Applets. In this chapter, you will:. Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple Swing applet and use a JLabel Add JTextField and JButton Components to Swing applets.

Download Presentation

Java Programming, Second Edition

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. Java Programming, Second Edition Chapter Nine Applets

  2. In this chapter, you will: • Write an HTML document to host an applet • Understand simple applets • Use Labels with simple AWT applets • Write a simple Swing applet and use a JLabel • Add JTextField and JButton Components to Swing applets

  3. Learn about event-driven programming • Add output to a Swing applet • Understand the Swing applet life cycle • Create a more sophisticated interactive Swing applet • Use the setLocation() and setEnabled() methods

  4. To Write a Java Application: • Write the application in the Java programming language, and then save it with a .java file extension • Compile the application into bytecode using the javac command. The bytecode is stored in a file with a .class file extension • Use the java command to interpret and execute the .class file

  5. Writing an HTML Document to Host an Applet • Applets- Programs that are called from within another application • You run applets within • a page on the Internet • an intranet • or a local computer from within another program called Applet Viewer • To view an applet, it must be called from within another document written in HTML

  6. Writing an HTML Document to Host an Applet To create an applet: • Write the applet in the Java programming language, and save it with a .java file extension • Compile the applet into bytecode using the javac command • Write an HTML document that includes a statement to call your compiled Java class • Load the HTML document into a Web browser or run the AppletViewer program

  7. Writing an HTML Document to Host an Applet • Applets are popular because users can execute them using a Web browser • Web browser- A program that allows you to display HTML documents on your computer screen • Internet Explorer • Netscape Navigator

  8. Writing an HTML Document to Host an Applet • Code to run an applet from within an HTML document • <applet> • </applet> • Applet tag attributes • CODE = is followed by the name of the compiled applet you are calling • WIDTH = is followed by the width of the applet on the screen • HEIGHT = is followed by the height of the applet on the screen

  9. Applets • The WIDTH and HEIGHT attributes are measured in pixels • Pixels- Picture elements, or tiny dots that make up the image on your video monitor

  10. Understanding Simple Applets To write an applet you must also: • Include import statements to ensure that necessary classes are available • Learn to use some Windows components and applet methods • Learn to use the keyword extends

  11. Understanding Simple Applets • Component- A class that defines any object that you want to display • Container- A class that is used to define a component that can contain other components

  12. Understanding Simple Applets • Most AWT applets contain 2 import statements • import java.applet.*; • import java.awt.*; • java.applet- Contains a class named Applet • Every applet you create is based on Applet • java.awt- The Abstract Windows Toolkit, or AWT

  13. Understanding Simple Applets • Most Swing applets contain 2 import statements • import javax.swing.*; • import java.awt.*; • javax.swing- A package that contains classes that define GUI components (Swing components)

  14. Understanding Simple Applets • Swing classes- part of a more general set of GUI programming capabilities that are known as the Java Foundation Classes, or JFC • JFC includes Swing component classes and selected classes from the java.awt package

  15. AWT and Swing Applets AWT and Swing applets • Begin the same way as Java applications • Must also include • extends Applet • extends JApplet • The extends keyword indicates the applet will build upon Applet and JApplet

  16. Applets • Four methods in every applet • public void init() • public void start() • public void stop() • public void destroy() • Java can create these for you

  17. Using Labels with Simple AWT Applets • The java.awt package contains commonly used Windows components • Labels • Menus • Buttons • Label- Built-in class that holds text that you can display within an applet

  18. Using Labels with Simple AWT Applets • Label class contains fields that indicate font and alignment • You can assign some text to a label with the setText() method • Use the add() method to add a component to an applet window

  19. Writing a Simple Swing Applet and Using a JLabel • JLabel- Built-in class that holds text that you can display within an applet • The counterpart to the AWT Label

  20. Writing a Simple Swing Applet and Using a JLabel • Available constructors include: • JLabel() creates a JLabel instance with no image and an empty string for the title • JLabel(Icon image) creates a JLabel instance with the specified image • JLabel(Icon image, int horizontalAlignment) creates a JLabel instance with the specified image and horizontal alignment • JLabel(String text) creates a JLabel instance with the specified text • JLabel(String text, Icon icon, int horizontalAlignment) creates a JLabel instance with the specified text, image, and horizontal alignment • JLabel(String text, int horizontalAlignment) creates a JLabel instance with the specified text and horizontal alignment

  21. Writing a Simple Swing Applet and Using a JLabel • AWT components are added directly to the Applet • Swing components must use a content pane • The content pane is an object of the Container class • A container can be created using the getContentPane() method

  22. Changing a JLabel’s Font • Font object- Holds typeface and size information • setFont() method requires a Font object argument • To construct a Font object you need 3 arguments • Typeface • Style • Point size

  23. Changing a JLabel’s Font • To construct a Font object you need 3 arguments • Typeface • String representing a font • Common fonts are Arial, Courier, and New Times Roman • Is only a request • Style- applies an attribute to displayed text • Font.PLAIN • Font.BOLD • Font.ITALIC • Point size • Integer that represents 1/72 of an inch • Printed text is usually 10- or 12 points

  24. Adding JTextField Components to Swing Applets • JTextField- Component into which a user can type a single line of text data • JText field can be constructed from • public JTextField() constructs a new JTextField • public JTextField(int numColumns) constructs a new empty JTextField with a specified number of columns • public JTextField(String text) constructs a new JTextField initialized with the specific text • public JTextField(String text, int columns) constructs a new JTextField with the specified text and columns

  25. Other JTextField Methods • setText() method- Allows you to change the text in a JTextField that has already been created • getText() method- Allows you to retrieve the string of text in a JTextField

  26. Other JTextField Methods • Keyboard focus- When the user clicks within the JTextField, the JTextField has focus, which means the next entries from the keyboard will be at that location • requestFocus() method- To have the insertion point appear automatically within the TextField without requiring the user to click in it first

  27. Other JTextField Methods • Editable- The capacity for a field to accept keystrokes • setEditable() method- Used to change the editable status of a JTextField

  28. Adding JButton Components to Swing Applets • JButton- Creates a button • JButton can be constructed from • public JButton() constructs a button with no set text • public JButton(Icon icon) creates a button with an icon of type Icon or ImageIcon • public JButton(String text) creates a button with the specific text • public JButton(String text, int columns) constructs a new JTextField with the specified text and columns

  29. Adding JButton Components to Swing Applets • setLabel() method • To change a JButton’s label • readyJButton.setLabel(“Don’t press me again!”)

  30. Adding Multiple Components to a JApplet • To add multiple components in a container use a layout manager • To control component positioning • Default behavior is to use a border layout • Border layouts • Flow layouts

  31. Adding Multiple Components to a JApplet • Border layouts • Created by the BorderLayout class • Divide a container into 5 sections • North, South, East, West, and center • Created with the BorderLayout() or BorderLayout(int, int) methods

  32. Adding Multiple Components to a JApplet • Flow Layouts • Places components in a row, and when a row is filled, it automatically spills components onto the next row • Default positioning of the row of components is centered in the container

  33. Learning about Event-Driven Programming • Event- Occurs when someone using your applet takes action on a component • Procedural- Programmers dictate the order in which events occur • Event-driven programs- The user can initiate any number of events in any order • Source- Component on which an event is generated • Listener- Object that is interested in an event

  34. Preparing Your Swing Applet to Accept Event Messages • Prepare your applet to accept mouse events by: • importing the java.awt.event package • adding the phrase implements ActionListener to the class header • ActionListener is an interface • Interface- A set of specifications for methods that you can use with event objects

  35. Telling Your Swing Applet to Expect Events to Happen • addActionListener() method • To tell the applet to expect ActionEvents • aButton.addActionListener(this);

  36. Telling Your Swing Applet How to Respond to Any Events That Happen • actionPerformed(ActionEvent e) method • When a JApplet has registered as a listener with a JButton, and a user clicks the JButton the actionPerformed method executes

  37. Adding Output to a Swing Applet • You can add components to an applet using the add() method • You can also remove components from an applet using the remove() method • Remove(answer);

  38. Understanding the Swing Applet Life Cycle • Override- When you write a method that has the same method header as an automatically provided method

  39. Understanding the Swing Applet Life Cycle • start() method- Executes after the init() method • Executes every time the applet becomes active after it has been inactive • stop() method- When a user leaves a web page • You do not usually write your own stop() methods

  40. Understanding the Swing Applet Life Cycle • destroy() method- When the user closes the browser or AppletViewer • You do not usually write your own destroy() methods

  41. Using the setLocation() and setEnabled() Methods • setLocation() method- Allows you to place a component at a specific location within the AppletViewer window • X-axis- Horizontal position in a window • X-coordinate- Value increases as you travel from left to right across the window • Y-axis- Vertical position in the window • Y-coordinate- Value increases as you travel from top to bottom in the window

  42. The setEnabled() Method • setEnabled() method- To make a component unavailable and, in turn, to make it available again • True if you want to enable a component • False if you want to disable a component • If (yLoc==280) pressButton.setEnabled(false);

More Related