如何將HTML中的參數(shù)傳遞給Applet?
- <html>
-
-
- <body>
-
- <Applet code="Appletparam.class" height=400 width=400 >
-
- <param name=myName value="yangxueyong">
- <param name=mySex value="男">
- <param name=myNum value=07061243>
- <param name=myAge value=23>
- </Applet>
-
- </body>
-
- </html>
Applet代碼: - import java.applet.*;
- import java.awt.*;
- public class Appletparam extends Applet
- {
- private String name;
- private String sex;
- private int num;
- private int age;
- public void init()
- {
- setBackground(Color.gray);
- name=getParameter("myName");
- sex=getParameter("mySex");
- num=Integer.parseInt(getParameter("myNum"));
- age=Integer.parseInt(getParameter("myAge"));
- }
- public void paint(Graphics g)
- {
- g.setColor(Color.blue);
- g.drawString("my name is :"+name, 10, 20);
- g.drawString("my sex is :"+sex, 10, 40);
- g.drawString("my num is :"+num, 10,60);
- g.drawString("my age is :"+age, 10, 80);
- }
- }
注意:(1)html文件中 <applet >必須包含code width heigth屬性。 (2)html文件放在applet字節(jié)碼文件目錄下 (3)采用傳遞參數(shù)的方法可以通過HTML文件來修改參數(shù),而不必修改Applet源程序。 HTML文件相對簡明,尤其是在Applet源程序較復(fù)雜時(shí),這種辦法有可取之處。 <!-- #EndEditable --> 運(yùn)行結(jié)果:
|