JavaScript调用Applet的函数

#需求 通过浏览器操作客户端的文件浏览器,打开网站指定文件夹,并对文件进行读写。

#初步调用 ##环境安装

  1. 浏览器Firefox与Java必须是相同位(目前Firefox Windows只有32位,则Java必须是32位)
  2. 在Java控制面板-》安全 中添加例外站点,如果为本地,则file:///I:/
  3. 在Java控制面板->高级->Java控制台,选择显示控制台,方便查看函数是否调用,并打印输出

##程序结构 如果在html想要直接执行applet.class,则必须把class文件放到包的顶端,才能够执行

##页面,与调用Applet函数内容

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
</head>
<body>
<applet id="TacticApplet" code="TacticApplet.class" width="200" height="200">
    Your browser does not support the <code>applet</code> tag.
</applet>
<input class="btn btn-default" type="button" value="Input" onclick="opendir()">
<script type="text/javascript">
function opendir(event)
{
    console.log('Page localfile.html function opendir Debug:run');
    console.log(window);
    var test = TacticApplet;
    console.log(test);
    test.print();
}
</script>
</body>

##Java代码

import java.applet.Applet;
import java.awt.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by zhanpeng on 16-1-28.
 */
public class TacticApplet extends Applet {

    int index = 0;

    @Override
    public void init() {
        System.out.println("Class TacticApplet Method init Debug: run");
        Graphics g = this.getGraphics();
        paint(g);
    }

    public void paint( Graphics g )
    {
        index = index + 1;
        g.drawString("Hello Applet!" + index, 45, 45);
    }

    public void print()
    {
        System.out.println("Class TacticApplet Method print Debug: run");
    }

    public void call() throws IOException {
        File file = new File("/tmp/applet.txt");
        if(!file.exists())
            file.createNewFile();

        FileWriter fileWriter = new FileWriter(file,true);
        fileWriter.write("1");
        fileWriter.close();
    }
}

转载于:https://my.oschina.net/hava/blog/614803

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值