简体   繁体   English

将小程序添加到网站

[英]Adding an applet to a website

First time posting here. 第一次在这里发帖 I made a simple calculator program using java and I am trying to put it onto my website. 我使用java制作了一个简单的计算器程序,我试图把它放到我的网站上。 From what I've gathered from previous help posts is that I need to create a JApplet with all my program contents and compress it into a .jar file. 从我之前的帮助帖子中收集到的是,我需要创建一个包含所有程序内容的JApplet并将其压缩为.jar文件。 Then I need to create a .JNLP file, which describes how to applet should be launched. 然后我需要创建一个.JNLP文件,该文件描述了如何启动applet。

So here is where I am having trouble. 所以这就是我遇到麻烦的地方。

package calculator;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;

public class CalculatorApplet extends JApplet {

public void init()
{
    try
    {
        SwingUtilities.invokeAndWait(new Runnable()
        {
        public void run() {
            Calculator calc = new Calculator();
            add(calc);
        }
        });
    }
    catch(Exception e)
    {
        System.err.println("GUI creation failed");
    }
}
}

It seems my applet was not constructed properly. 看来我的applet构造不正确。 Whenever I run it a "java.lang.reflect.InvocationTargetException" is thrown. 每当我运行它时,都会抛出“java.lang.reflect.InvocationTargetException”。 Whenever I run my Calculator class independently from the applet it works as expected. 每当我独立于applet运行我的Calculator类时,它就会按预期工作。 Any ideas where the source of my error is? 我错误的来源是什么想法?

I think JNLP files are used for Java Web Start. 我认为JNLP文件用于Java Web Start。 That's something you won't need with an average Java applet. 这是普通Java applet不需要的东西。 Please do correct me if I'm wrong. 如果我错了,请纠正我。

If you have the working .jar file, an HTML file that calls the applet will be sufficient to run the applet. 如果你有.jar文件,那么调用applet的HTML文件就足以运行applet了。 Insert the code <applet width="300" height="300" archive="jar.jar" code="class.class"></applet> into an HTML file, where class.class is the class extending Applet or JApplet and jar.jar the location of the jar file. 将代码<applet width="300" height="300" archive="jar.jar" code="class.class"></applet>插入HTML文件,其中class.class是扩展Applet或JApplet的类和jar.jar jar文件的位置。 Loading the HTML file in a browser will display the applet. 在浏览器中加载HTML文件将显示applet。

Alternatively, you can use Java's Applet Viewer to open the HTML page and open the applet locally. 或者,您可以使用Java的Applet Viewer打开HTML页面并在本地打开applet。

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

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