简体   繁体   English

如何为本地开发部署Java Applet

[英]how to deploy a java applet for local development

I simply put the class file and the html file in the same directory. 我只是将类文件和html文件放在同一目录中。 And I call the applet with this: 我这样称呼applet:

<p align="center">
        <applet code="/ShowImage.class" width="200" height="200">
        </applet>
    </p>

Obviously this doesn't work. 显然,这是行不通的。 What is the most convenient way to setup local development? 进行本地开发的最便捷方法是什么?


edit: 编辑:

My applet code: 我的小程序代码:

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

/**
 *
 * @author PCKhoi
 */
public class ShowImage extends Applet {
    private BufferedImage img;

    public void init() {
        try {
             URL url = new URL(getCodeBase(), "what_I_think.jpg");
             img = ImageIO.read(url);
         } catch (IOException e) {
         }
    }
    public void paint(Graphics g){
        g.drawImage(img,20,20, null);
    }
}

Try this 尝试这个

<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET ALIGN="CENTER" CODE="ShowImage.class" WIDTH="800" HEIGHT="500"></APPLET>
</BODY>
</HTML>  

and please post your applet code 并请发布您的小程序代码

Some notes: 一些说明:

  • The code as published (at this instant) does not compile 发布的代码(目前)无法编译
  • Do not swallow exceptions in broken code 不要吞下损坏代码中的异常

To compile and run.. 编译运行

prompt> javac ShowImage.java
prompt> appletviewer ShowImage.java

Code (note that the image name will need to be changed back). 代码(请注意,图像名称将需要改回)。

//<applet code="ShowImage" width="200" height="200"></applet>
import java.applet.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.net.URL;

/**
 * @author PCKhoi
 */
public class ShowImage extends Applet {
    private BufferedImage img;

    public void init() {
        try {
             URL url = new URL(getCodeBase(), "icon.png");
             img = ImageIO.read(url);
         } catch (IOException e) {
             e.printStackTrace();
         }
    }
    public void paint(Graphics g){
        g.drawImage(img,20,20, null);
    }
}

The important line of the source, in relation to your question, is the first, commented line. 与您的问题相关的重要信息是第一行,带有注释的行。 It supplies an HTML element that the Applet Viewer will parse and use as a pseudo-HTML. 它提供了Applet Viewer将解析并用作伪HTML的HTML元素。

I think I know why it doesn't load now. 我想我知道为什么现在不加载。 The applet was wrapped inside a complex netbeans project, that's why putting the class file and the html file inside the same directory didn't work. 小程序被包装在一个复杂的netbeans项目中,这就是为什么将类文件和html文件放在同一目录中不起作用的原因。

My solution is to use a simple IDE such as DrJava if you don't need project functionality. 我的解决方案是如果您不需要项目功能,则使用简单的IDE(例如DrJava)

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

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