【发布时间】:2017-02-05 08:25:02
【问题描述】:
这是一个最初设计为 Java 应用程序的程序。我希望它在浏览器上的小程序内工作。问题是我无法让我的浏览器成功打开并运行小程序,这可能是由于代码编写不当,或者只是小程序代码与应用程序代码不同,如果是这样,我在哪里可以找到“如何在小程序中编码“?
我应该使用小程序表达式的代码(使用 mozilla)并指定包,还是将其保留为代码库?是代码有问题吗?它是我调用的 main() 方法吗?该文件是否应该具有 .class 的扩展名,因为 Netbeans 会自动为其提供 .java,即使我选择:新文件 --> Java --> Java 类?
我想创建一个小程序,提示您输入数据,然后它会执行程序并在框内返回结果。
所有代码都不是必需的,但我提供所有代码只是为了提供信息。
HTML 代码
<applet codebase="SigmaExpression12.java" width= "500" height="400">
Java 代码
package sigmaexpression;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JOptionPane;
public class SigmaExpression12 extends Applet {
@Override
public void paint(Graphics g)
{ g.drawRect(0,0,250,100);
g.setColor(Color.blue);
g.drawString("hi.", 100, 100);
}
public static void main() {
double x;
String answer1 = JOptionPane.showInputDialog(null, "What is the first term of your Sigma expression or series?");
double y = Double.parseDouble(answer1);
System.out.println("Your a(1) is :" + y);
String answer2 = JOptionPane.showInputDialog(null, "What is the last term of your Sigma expression or series?");
double z = Double.parseDouble(answer2);
System.out.println("Your a(n) is :" + z);
String answer3 = JOptionPane.showInputDialog(null, "What is the total term of numbers in your sequence? If you don't know this, put 0.");
double n = Double.parseDouble(answer3);
System.out.println("Your n is :" + n);
if(n==0)
{
String answer4 = JOptionPane.showInputDialog(null, "What is the increment of your numbers? How much is it being increased, divided or multiplied by?");
double i = Double.parseDouble(answer4);
System.out.println("Your i is: " + i);
double nn = Math.round(n*100.0)/100.0;
nn = (((z - y)/i) + 1);
System.out.println("Your n is :" + nn);
x = (((y+z)/2) * nn );
System.out.println("The sum of your numbers is: " + x);
}
x = (((y+z)/2) * n );
System.out.println("The sum of your numbers is: " + x);
}
【问题讨论】:
-
1) 见Java Plugin support deprecated 和Moving to a Plugin-Free Web。 2) 小程序中的所有内容都可以使用 JavaScript、表单字段以及自定义绘制部分的 HTML 画布来完成。对这个使用纯 HTML/JS。