![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Editing, Compiling, and Executing a Java Program
- Create a Java file
- Edit the Java applet
- Code the Java applet
- Create the associated HTML file
- Run the Java Applet
Create a Java fileAll code must be in a subsystem and view (text). A Java applet is not a standalone Java application; it runs within an applet viewer or a Web browser such as Netscape.
Edit the Java applet
package project2; import java.applet.*; import java.awt.*; public class circle extends Applet { private Font font; private Button process_a, process_b; private TextField data_field; //One-time initialization for the applet public void init () { font = new Font ("Helvetica", Font.BOLD, 14); //Add Components to the Applet. Label label = new Label ("Radius (1-100):", Label.RIGHT); add (label); data_field = new TextField(10); GridBagLayout gridBag = new GridBagLayout(); setLayout(gridBag); GridBagConstraints c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.HORIZONTAL; gridBag.setConstraints(data_field, c); add(data_field);
// Create the buttons and add them to the applet. process_a = new Button ("Display Circle"); process_a.setForeground (Color.black); process_a.setBackground (Color.yellow); process_b = new Button ("Reset Button"); process_b.setForeground (Color.black); process_b.setBackground (Color.green); add (process_a); add (process_b); validate (); }
Code the Java appletCompile the Java applet to generate the .class file.
- 1. . Select the Compile:Code command
The Jobs window for the code command (graphics) is displayed.
The Message window for the code command (graphics) is displayed.
The class is displayed in the project2.ss/java.wrk directory viewer (text) in the form circle.class.
Create the associated HTML fileBecause the Java program is an applet, you need to display an HTML file that references the applet in a Web browser. The Web browser runs the Java interpreter internally to interpret the applet's compiled code that is in the .class file.
- 1. . Create an HTML file using the File:New:New HTML command
Apex displays the New HTML dialog box (graphics). Notice that the pathname of the java.wrk view appears in the Context field.
- 2. . Set the dialog box values to create the HTML file in the circle.html file.
- 3. . Select the OK button in the New HTML dialog box (graphics).
Apex creates an HTML file named circle.html and because Visit it is selected, displays the file in an HTML editor (graphics) window. The file is created with the HTML code to run circle.class. Do not modify this file.
Run the Java AppletTo run the circle.java applet, you will open the associated HTMLfile in a Web browser.
You have just completed the Quick Start Java Tutorial. To delete the files you have created, visit the Cleaning Up topic.
Rational Software Corporation http://www.rational.com support@rational.com techpubs@rational.com Copyright © 1993-2001, Rational Software Corporation. All rights reserved. |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |