Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS381 - Lab Session 2 
 
 
A. Compile a Java Applet 
 
1. Using “nedit”, or “notepad”, or your tool of choice, create a new text file. 
2. Add the following Java code: 
 
import java.applet.Applet; 
import java.awt.Graphics; 
 
public class helloworld extends Applet 
{ 
  public void paint(Graphics g) 
  { 
    g.drawString("Hello world", 50, 50); 
  } 
 
} 
 
3. Save the file as helloworld.java. 
4. Open a DOS prompt, go to the directory containing the Java applet, and 
compile the file using the command “javac helloworld.java” 
5. Now create a new HTML file and add the required elements. 
6. Add an applet element and define the required attributes related to the Applet 
you compiled previously. 
7. Save the file as lab_02a.html. 
8. Open the lab_02a.html in your browser. 
 
 
B. Applet – Browser Interaction 
 
1. Open the helloworld.java file in your editor. 
2. Add a new Button object and a new int variable.  
3. Create an init() method and within the method create a new instance of Button, 
add it to the Applet container, and add an action listener to the Button. Then, 
initialise the int variable with 0. 
4. Add an actionPerformed method and within the method call the showStatus 
method to change the status of the status-bar in the HTML Browser. The 
message should show the next incremental value of the int variable, every time 
the button is pressed. 
5. Remember that the class has to implement an actionListener, and that you 
have to import the necessary new libraries: “import java.awt.*;” and “import 
java.awt.event.*;”. 
6. Save the file and recompile it. 
7. Open the lab_02a.html in your browser. 
8. Click on the button in the Applet. 
 
C. Passing parameters to the Applet via HTML 
1. Open the helloworld.java file in your editor. 
2. Create a new string variable called msg. 
3. In the init() method use the getParameter(PARAM_NAME) method to get a 
message (string parameter) from the HTML page and assign that to the msg 
variable. 
4. Change the paint method so that it prints out the msg string instead. 
5. Save the file and recompile it. 
6. Open the lab_02a.html file in your editor. 
7. Add the param element within the applet element and define the name and 
value attributes (i.e. ) 
8. Save the file as lab_02c.html. 
9. Open the lab_02c.html in your browser. 
 
 
D. Identifying elements into the document – DOM 
 
1. Using “nedit” or “notepad” or your tool of choice, create a new text file, and 
add the required HTML elements. 
2. Add a form element and within that add different types of input elements 
(button, checkbox, hidden, image, password, text, textarea). 
3. Give each one of the elements created a name and a value attribute. 
4. Create a JavaScript function that will go through all elements in the form and 
then display the element’s name and text using the alert function. 
5. Add a button that will call the JavaScript function. 
6. Save the file as lab_02d.html. 
7. Open the lab_02d.html in your browser. 
8. Press the button… 
9. Try to do that in three different ways of accessing the DOM structure: as 
objects, as nodes, and using an id.