Java Student Project         

LabelsMessage
Ability Level: Beginner 
Estimated Time: 45 minutes
Objectives:
  • To learn to make an applet which prints out a message.
  • To learn to use labels.

 

Materials & Resources:
  • C++ Software
  • Textbook
Overview:
Write an applet which will print out a message from a web browser. The message should have a large font. You must use the label command. Call the label by the use of a method. 

Read the hints below for clarification. Be positive with your message.
Instructions:

Steps for using Visual J++:

  1. From the start, programs menu, click on  Microsoft Visual J++.

  2. Click on New Project, under the New tab, click Web Pages and Applet on HTML.

  3. Enter the name and location and click open.

  4. After it loads, click file, new file, Java J++ and Java File, and click open.

  5. Write your code. Sample:
    import java.applet.*;
    import java.awt.*;


    public class Salutations extends Applet

      Label hi = new Label ("Hi there. ");
      public void init()
      { 
          add (hi); 
       }
    }

     

  6. In the class method on the line after the Label line,
    Declare a Font object named bigFont  like the following : 
    Font bigFont = new Font ("TimesRoman", Font.ITALIC,24); 

  7. In the init method, before the add line, call the font by using the Label identification name with the setFont call.
    Example: hi.setFont(bigFont); 

  8. Click on "Save Java File1 As" and be sure you are in the correct location, enter the name of the class you created with the extension of .java.

  9. An Applet.java is created along with the file.java you created. Click on the Applet.java and press delete to remove it. 

  10. Once this has been done, the file.java which you created copies to the project section. Now you may delete the miscellaneous files by highlighting it and pressing delete. 

  11. Be sure the Page1.htm file includes the name of your class in the code statement. 
    Example:
    code=labelMessage.class

  12. Double click on the Page1.htm file, click on source (middle tab under the code). Click on the tab bar, view, and view controls as text.

  13. Edit the Applet1 statements after the line "insert HTML here" and applet and enter the name of the java file you just created. 
    Example:
    code=labelsMessage.class 
    name=labelsMessage

  14. Be sure your settings have the output going to C drive and click the arrow to output your message.

Hints: 1. Import is necessary to include library files.  Be sure to include Applet.The asterisk indicates all the libraries from the applet field are to be included. AWT is used to indicate Abstract Windows Toolkit package. Examples: 
import java.awt*; 
import java.applet.*; 


2. Class must be declared with the name and extends Applet to indicate Applet is being created.    Example: public class Salutation extends Applet 

3. One of the easiest Windows components is a Label. Label is a built-in class that holds text that can be displayed within an applet. The Label class contains fields that indicate the appearance information including font and alignment.  Example:
      Label salutation = new Label ("Hello. Good to see you!");

4. The method you use to add a component to an applet window is add(). (A method is a small program routine, as a function is in C++.) 
                 Example:    add ( Saluation );

5. The object of the add() method is the applet itself, so when you add a component to a window, you could write this.add(); in place of add();. 

6. With an applet, the browser calls many methods automatically. The following four methods are included in every applet:
    public void init();                            public void start()
    public void stop();                          public void destroy ()

7. Java provides a Font object that holds typeface and size information. The setFont() method requires a Fong object argument. Three arguments, typeface, style, and point size are needed. The typeface is a String representing a font and a request to use the font.

8. The style applies an attribute to displayed text and is one of three arguments, Font.PLAIN, Font.BOLD, or Font.ITALIC.

9. The point size is an integer that represents 1/72 of an inch.

10. To give a Label object a new font, you create the Font object, as in Font headlineFont = new Font ("Helvetica", Font.BOLD,36);, and then you can use the setfont() method to assign the font to a Label with the statement Saluation.setFont (headlineFont);

11. The typeface name is a String, so you must enclose it in double quotation marks when you use it to declare the Font object.