2014-05-03 15 views
0

所以我搜索了很長時間瞭解如何使用此函數從我的小程序的html中檢索s的值,但是我沒有找到太多的信息。如何在Java中使用getParameter對於小程序

基本上我想要從命令行(當它作爲app運行時)或html(當它作爲applet運行時)有5個參數。這是我的代碼,Board應用程序是一個小程序。它適用於應用程序,因爲拋出了NullPointerException,它只是從命令行讀取參數,但它不適用於小程序,它實際上什麼也不做,空白屏幕。我還需要做些什麼來使其工作?

Board app=new Board(); 
try{  
    x = Integer.parseInt(app.getParameter("x")); 
    y = Integer.parseInt(app.getParameter("y")); 
    delay = Integer.parseInt(app.getParameter("delay")); 
    wolfNumber = Integer.parseInt(app.getParameter("wolves")); 
    hareNumber = Integer.parseInt(app.getParameter("hares")); 
} 

catch(NullPointerException exec){ 


    try{ 
     x = Integer.parseInt(args[0]); 
     y = Integer.parseInt(args[1]); 
     delay = Integer.parseInt(args[2]); 
     wolfNumber = Integer.parseInt(args[3]); 
     hareNumber = Integer.parseInt(args[4]); 
     if(args.length<5) throw new NumberFormatException(); 

    } 

    catch(NumberFormatException ex){ 
     JOptionPane.showMessageDialog(null,"Nie podano odpowiednich parametrow","Error",JOptionPane.WARNING_MESSAGE); 
     System.exit(0); 
    } 

    catch(ArrayIndexOutOfBoundsException exe){ 
     JOptionPane.showMessageDialog(null,"Nie podano odpowiednich parametrow","Error",JOptionPane.WARNING_MESSAGE); 
     System.exit(0); 
    } 
} 

我的html:

<html> 
    <head> 
     <title>Simulator</title> 
    </head> 
    <body> 
    <center> 
     <h1>Simulator</h1> 
     <hr> 
    <APPLET ARCHIVE="Main.jar" CODE="Main.class" WIDTH=500 HEIGHT=500> 
    <PARAM name="x" value="64"> 
    <PARAM name="y" value="64"> 
    <PARAM name="delay" value="400"> 
    <PARAM name="wolves" value="20"> 
    <PARAM name="hares" value="100"> 
    </APPLET> 
     </applet> 
     <hr> 
    </center> 
    </body> 
</html> 
+0

爲了儘快獲得更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整和可驗證示例)。 –

回答

0

您需要將您的實際Applet類(在這裏,局)在applet代碼的代碼PARAM。現在爲了在命令行AND http上工作,您需要重新組織代碼。也許添加一個主要方法到您的董事會類?

作爲一個方面說明,注意你的html,那裏有一堆未封閉/重複的標籤。