2017-06-26 32 views
0

我已經定義了下面的Java小程序的功能:調用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(); 
     } 
    } 
} 

而且下面的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); 
} 

最後的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> 

我得到以下錯誤:TypeError: combineDataApplet.setUpFile is not a function. (In 'combineDataApplet.setUpFile("blabl")', 'combineDataApplet.setUpFile' is undefined)

我在stackoverflow上發現了一些帖子,指出我需要把它放到一個沒有display:none的div塊中,但是我的任何div都沒有樣式,所以不應該有display:none

我希望有人能幫助我弄清楚什麼是錯的

回答

0

編程小程序是死騎的馬。

未經測試:你應該訪問的小程序是這樣的:

document.combineDataApplet.setUpFile("blabl"); 

,而不是由document.getElementById()。後者只會讓你獲得包含applet的DOM元素。

編輯:

這裏是2016年約Oracle deprecating Applets一月的鏈接。本文提供了進一步鏈接到Oracle的whitepaper for migrating applets。我沒有讀過它,但它可能會給你一些選擇。

+0

你會用什麼來代替applet? –

+0

哦,我知道 - 我的問題是,我需要更多的內存比JS可用 –

+0

後端服務,例如與REST接口和用戶界面的JS前端。 Applets依賴的NSPlugin接口如果尚未從現代瀏覽器中刪除,則不建議使用該接口,因此,如果依賴於Applets,則會強制用戶使用過時的瀏覽器。 –