从javascript触发applet按钮

时间:2011-01-18 10:29:45

标签: java javascript applet

我希望使用javascript

点击普通的html按钮来触发applet按钮
public class appletToWrite extends Applet{
Button write;
static String msPath;
static String msData;
public appletToWrite()
{
    this.write = new Button("Save");
}

public static void initializeData(String asPath, String asData){
    msPath = asPath;
    msData = asData;
}

public void init() { 
    add(this.write, "Center");
    this.write.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent paramActionEvent) {
            foo();
        }
    }); }


public void foo(){
    new appletToWrite.WriteText();
}

public class WriteText {
    WriteText() {
        try {
            String str2 = "D:\\Documents and Settings\\varun.aggarwal\\Desktop\\sample\\wow.htm";
            File localFile = new File(str2);
            localFile.createNewFile();
            BufferedWriter localBufferedWriter = new BufferedWriter(new FileWriter(localFile, true));
            String str1="helllllllo";
            localBufferedWriter.write(str1);
            localBufferedWriter.close();
            JOptionPane.showMessageDialog(null, "File Successfully Saved!!! ");

        }
        catch (Exception localException) {
            JOptionPane.showMessageDialog(null, localException.getMessage());
        }
    }
}

}

有可能???

这是与权限相关的内容,因为我发现文件IO权限仅在我点击applet按钮时可用,而不是当我点击html按钮并调用applet方法foo

2 个答案:

答案 0 :(得分:1)

是的,有可能是JavaScript到Applet和Applet到Javascript。

请查看此页面以获取代码示例:Java - Javascript interaction

答案 1 :(得分:1)

在applet中创建一个方法,包装该方法中的所有代码,并从动作侦听器调用该方法foo()。

喜欢

actionPerformed(){
        foo();
    }//just illustration

现在

<applet code="com.yourcompany.MyApplet"
mayscript="true" name="myApplet" width="200" height="100">

并且myApplet将从{javascript

获得foo();
myApplet.foo();//it will call that method