如何在java中为小程序使用getParameter
因此,我搜索了很长一段时间,寻找一些关于如何使用这个函数从我的applet的html中检索s值的信息,但我没有找到多少 基本上,我想从命令行(当它作为应用程序运行时)或html(当它作为小程序运行时)获得5个参数。这是我的代码,Board app是一个小程序。它在应用程序中运行良好,因为会抛出NullPointerException,它只从命令行读取参数,但在小程序中不会,它实际上什么都不做,在空白屏幕上。我还需要做什么才能让它工作如何在java中为小程序使用getParameter,java,html,web-applications,applet,getparameter,Java,Html,Web Applications,Applet,Getparameter,因此,我搜索了很长一段时间,寻找一些关于如何使用这个函数从我的applet的html中检索s值的信息,但我没有找到多少 基本上,我想从命令行(当它作为应用程序运行时)或html(当它作为小程序运行时)获得5个参数。这是我的代码,Board app是一个小程序。它在应用程序中运行良好,因为会抛出NullPointerException,它只从命令行读取参数,但在小程序中不会,它实际上什么都不做,在空白屏幕上。我还需要做什么才能让它工作 Board app=new Board(); try{
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);
}
}
Board app=新板();
试试{
x=Integer.parseInt(app.getParameter(“x”));
y=Integer.parseInt(app.getParameter(“y”);
delay=Integer.parseInt(app.getParameter(“delay”);
wolfNumber=Integer.parseInt(app.getParameter(“狼群”);
hareNumber=Integer.parseInt(app.getParameter(“hares”);
}
捕获(NullPointerException执行){
试一试{
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]);
如果(args.length您需要在applet标记的code param中传递实际的applet类(这里是Board)。现在为了在命令行和http上工作,您需要重新组织代码。是否可以在Board类中添加一个main方法
另外,请注意您的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>