【问题标题】:Trouble getting a Java Applet program to open in my browser无法在我的浏览器中打开 Java Applet 程序
【发布时间】: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);

}

【问题讨论】:

标签: java applet


【解决方案1】:

你的html:

<applet codebase="SigmaExpression12.java" width= "500" height="400">

应该更像:

<applet codebase="SigmaExpression12.class" width= "500" height="400">

你知道,Java 不能运行 .java 源代码……它只能运行编译后的东西。

【讨论】:

    【解决方案2】:

    首先,Java Applet 技术正在消亡。并非所有浏览器都支持它。 例如谷歌浏览器不支持小程序。

    其次,要运行applet,您需要对applet jar 文件进行签名(参见JDK 中的jarsigner 工具)。

    第三,您需要将 url 小程序位置添加到异常站点列表中(在 Java 控制安全面板中查看)

    我建议您改为查看java web start

    【讨论】:

      猜你喜欢
      • 2011-06-29
      • 2018-06-13
      • 2014-09-26
      • 2014-05-25
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 2017-02-27
      相关资源
      最近更新 更多