欢迎来到天天文库
浏览记录
ID:8811903
大小:89.50 KB
页数:14页
时间:2018-04-08
《applet和javascript通信》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、Applet和JavaScript通信Applet和JavaScript通信1.Applet调用JavaScript中的方法Applet调用JavaScript中的方法,主要是通过相应的JSObject方法来获得相关的DOM对象(例如document,formEtc),然后通过eval()来调用相应的方法。下面是一个简单的实例。用户输入相应的姓名,利用applet显示问候语。//MyApplet.html
2、ight="500">ENTERYOURNAME://MyApplet.javaimportjava.applet.Applet;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importnetscape.j3、avascript.JSObject;importnetscape.javascript.JSException;publicclassMyAppletextendsApplet{ Stringtext="welcomefriend."; JSObjectwindow,document,form,textField; Buttondisplay; publicvoidinit(){ this.setFont(newFont("",Font.BOLD,19)); this.setBackground(C4、olor.pink); this.setForeground(Color.blue); window=JSObject.getWindow(this); document=(JSObject)window.getMember("document"); form=(JSObject)document.getMember("form1"); textField=(JSObject)form.getMember("name"); this.setLayout(newBorderLayout()); 5、 Panelpanel=newPanel(); display=newButton("DISPLAY"); display.addActionListener(newButtonEventHandler()); panel.add(display); this.add(BorderLayout.SOUTH,panel); } publicvoidpaint(Graphicsg){ g.drawString(text,120,50); } classButtonEventHandlerimplem6、entsActionListener{ publicvoidactionPerformed(ActionEventevent){ Strings=event.getActionCommand(); if("DISPLAY".equals(s)){ text="Hi"+(String)textField.getMember("value") +",Haveaniceday."; window.eval("alert('pleasesmile,itisgoodforyourhe7、alth')"); repaint(); } } }}•注:compilewhenusingthenetscape.javascript.JSObjectpackage•ForJava1.4.2andlater:addplugin.jartoyourclasspath.ItcanbefoundinthelibdirectoryofyourJREinstallation,e.g.C:ProgramFilesJavajre1.5.0libplugin.jar2.Browser'sJSca8、llsApplet•ThisexampleistoshowthatJavaScriptcallsmethodsofaJavaapplet.Insidethetagoftheappletshownbelow,anameisdefinedusingthename=“app"attribute.Thenthebrowser'sJavaScriptshouldbeabletocallmethodsoftheapplet
//MyApplet.javaimportjava.applet.Applet;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importnetscape.j
3、avascript.JSObject;importnetscape.javascript.JSException;publicclassMyAppletextendsApplet{ Stringtext="welcomefriend."; JSObjectwindow,document,form,textField; Buttondisplay; publicvoidinit(){ this.setFont(newFont("",Font.BOLD,19)); this.setBackground(C
4、olor.pink); this.setForeground(Color.blue); window=JSObject.getWindow(this); document=(JSObject)window.getMember("document"); form=(JSObject)document.getMember("form1"); textField=(JSObject)form.getMember("name"); this.setLayout(newBorderLayout());
5、 Panelpanel=newPanel(); display=newButton("DISPLAY"); display.addActionListener(newButtonEventHandler()); panel.add(display); this.add(BorderLayout.SOUTH,panel); } publicvoidpaint(Graphicsg){ g.drawString(text,120,50); } classButtonEventHandlerimplem
6、entsActionListener{ publicvoidactionPerformed(ActionEventevent){ Strings=event.getActionCommand(); if("DISPLAY".equals(s)){ text="Hi"+(String)textField.getMember("value") +",Haveaniceday."; window.eval("alert('pleasesmile,itisgoodforyourhe
7、alth')"); repaint(); } } }}•注:compilewhenusingthenetscape.javascript.JSObjectpackage•ForJava1.4.2andlater:addplugin.jartoyourclasspath.ItcanbefoundinthelibdirectoryofyourJREinstallation,e.g.C:ProgramFilesJavajre1.5.0libplugin.jar2.Browser'sJSca
8、llsApplet•ThisexampleistoshowthatJavaScriptcallsmethodsofaJavaapplet.Insidethetagoftheappletshownbelow,anameisdefinedusingthename=“app"attribute.Thenthebrowser'sJavaScriptshouldbeabletocallmethodsoftheapplet
此文档下载收益归作者所有