简体   繁体   English

如何在Java中为Applet使用getParameter

[英]How to use getParameter in java for applet

so I searched a long time for some info about how to use this function to retrieve values of s from html for my applet but I have not found much. 因此,我搜索了很长时间,以获取有关如何使用此函数从applet的html检索s值的一些信息,但是我发现的不多。

Basically I want to have 5 arguments from either command line (when its run as app) or html (when its run as applet). 基本上,我想从命令行(当其作为应用程序运行时)或html(当其作为小程序运行时)具有5个参数。 Here is my code, Board app is an applet. 这是我的代码,Board应用程序是一个applet。 It works fine for app, because NullPointerException is thrown and it just reads arguments from command line, but it does not for applet, it actually does nothing at all, blank screen. 它适用于应用程序,因为抛出NullPointerException且仅从命令行读取参数,但不适用于applet,实际上它什么也不做,黑屏。 What else do I have to do to make it work? 我还需要做什么才能使其正常工作?

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);
    }
}

my html: 我的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>

You need to pass your actual applet class (here, Board) in the code param of the applet tag. 您需要在applet标记的代码参数中传递实际的applet类(此处为Board)。 Now in order to work both at the command line AND http, you will need to reorganize your code. 现在,为了同时在命令行和http上工作,您将需要重新组织代码。 Maybe add a main method to your Board class ? 也许在您的Board类中添加一个main方法?

And as a side note, pay attention to your html, there is a bunch of unclosed/duplicate tags there. 另外,请注意您的html,那里有一堆未关闭/重复的标签。

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

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