简体   繁体   English

从JavaScript调用Java Applet方法=不是函数

[英]Call java applet method from JavaScript = is not a function

I have defined the following Java Applet: 我定义了以下Java Applet:

import java.applet.Applet;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CombineData extends Applet{

    private File destinationFile;
    private FileOutputStream fileOutputStream;


    public boolean setUpFile(String filePath) {

        // This is just to check if it is a new file we write to or not
        // We could return false at once saying file already exists
        boolean result;
        if ((destinationFile = new File(filePath)).exists()) {
            result = false;
        } else {
            result = true;
        }

        try {
            fileOutputStream = new FileOutputStream(destinationFile, true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return result;
    }

    public boolean write_line(byte[] data) {
        if (fileOutputStream == null) {
            return false;
        } else {
            try {
                fileOutputStream.write(data);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return true;
        }
    }

    public void finished() {
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

And the following JavaScript 和以下JavaScript

function start_button() {
    combineDataApplet = document.getElementById('combineDataApplet');
    combineDataApplet.setUpFile("blabl");
    var filename = "~/Downloads/" + prompt("Please enter file name (remember file extenstion)", "combined_data.txt");
    console.log(filename);
}

And finally the HTML 最后是HTML

<!DOCTYPE HTML>
<html lang="en">
  <head>
  <script src="js/page.js"></script>
  </head>
  <body>
    <div id="content">
      <div>
      <applet id="combineDataApplet" code="CombineData.class" width="350" height="350">
        APPLET
      </applet>
      <input id="start_button" type="button" onClick="start_button()" value="start"/>
      </div>
    </div>
  </body>
</html>

And I get the following error: TypeError: combineDataApplet.setUpFile is not a function. (In 'combineDataApplet.setUpFile("blabl")', 'combineDataApplet.setUpFile' is undefined) 我得到以下错误: TypeError: combineDataApplet.setUpFile is not a function. (In 'combineDataApplet.setUpFile("blabl")', 'combineDataApplet.setUpFile' is undefined) TypeError: combineDataApplet.setUpFile is not a function. (In 'combineDataApplet.setUpFile("blabl")', 'combineDataApplet.setUpFile' is undefined)

I have found a few post on stackoverflow which states that I need to put it into a div block without a display:none but there are no styles on any of my divs and by such there should be no display:none . 我发现了一些关于stackoverflow的文章,指出我需要将其放入没有display:none的div块中,但是我的任何div上都没有样式,因此应该没有display:none

I hope someone can help me figure out what is wrong 我希望有人能帮助我找出问题所在

Programming Applets is riding a dead horse. 编写Applets的过程令人毛骨悚然。

untested : you should access the applet this way: 未经测试 :您应该通过以下方式访问小程序:

document.combineDataApplet.setUpFile("blabl");

and not by document.getElementById() . 而不是document.getElementById() The latter only gets you the DOM element containing the applet. 后者仅使您获得包含小程序的DOM元素。

Edit: 编辑:

here is a link from January 2016 about Oracle deprecating Applets . 这是自2016年1月起有关Oracle弃用Applet的链接。 This article gives the further link to Oracle's whitepaper for migrating applets . 本文为Oracle applet的移植提供了进一步的链接。 I haven't read it, but it might show you some alternatives. 我没有看过,但可能会向您显示一些替代方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM