【问题标题】:How to use getParameter in java for applet如何在java中为applet使用getParameter
【发布时间】:2014-05-03 20:54:00
【问题描述】:

所以我搜索了很长时间有关如何使用此函数从我的 applet 的 html 中检索 s 值的一些信息,但我没有找到太多信息。

基本上我想从命令行(作为应用程序运行时)或 html(作为小程序运行时)有 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>

【问题讨论】:

  • 为了尽快获得更好的帮助,请发布MCVE(最小完整且可验证的示例)。

标签: java html web-applications applet getparameter


【解决方案1】:

您需要在小程序标签的代码参数中传递您的实际小程序类(此处为 Board)。现在,为了同时在命令行和 http 下工作,您需要重新组织代码。也许在您的 Board 类中添加一个 main 方法?

作为旁注,请注意您的 html,那里有一堆未闭合/重复的标签。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    相关资源
    最近更新 更多