Java 嵌入 HTML

问题描述:

我确定这个问题已经被问过一百万次,但无论我进行多少次谷歌搜索,我都无法解决这个问题.我基本上是想将一个包含多个包的项目嵌入到网页中.我做了一个测试程序,它只是让一些球在屏幕上弹跳,并且能够让它运行.我将主类放在一个包中,将球类放在另一个包中,只是为了测试它,它似乎运行良好.但是我在网页中实际需要的程序(只是称为 FinalProject)拒绝这样做.

I'm sure this question has been asked a million times, but no matter how many Google searches I do I cannot get this working. I'm basically trying to get a project with multiple packages in it to be embedded in a webpage. I made a test program which just made some balls bounce around the screen and was able to get that running. I put the main class in one package and the ball class in another just to test it and it seems to be running fine. But the program that I actually need in a web page (just called FinalProject) refuses to do it.

我能做的最好的事情就是给我一个空白屏幕,没有给出错误,但只有白色.如果我尝试点击应该不会发生任何事情的地方,我想是因为小程序在那里,但只是显示为白色,所以我看不到它.我确实使用了 applet 标签,根据我的理解,它现在已经贬值了,但我需要在网页上提交这个项目,这样老师才能看到它.我们已经测试过其他人的项目(使用了 applet 标签)可以工作,所以我现在尝试坚持这样做,然后担心让它在每个浏览器上都能正常工作.虽然这很可能是问题所在.也许它适用于他的浏览器,但不适用于我的浏览器.我曾尝试在 Google Chrome、Mozilla Firefox 和 Internet Explorer 上运行我的程序,但没有成功.

The best thing I can get it to do is give me a blank screen, without giving an error but just white. If I try clicking where it should be nothing happens, I think because the applet is there but is just showing white so I can't see it. I did use the applet tag, which from my understanding is now depreciated but I need to turn this project in on a webpage just so the teacher can see it. We've already tested that other people's projects (which used the applet tag) work, so I was trying to stick with that for now and worry about getting it working on every browser afterwards. Though that could very well be the problem. Maybe it would work on his browser but not mine here. I've tried running my program on Google Chrome, Mozilla Firefox, and Internet Explorer with no luck.

这是HTML代码:

<html>
<head>  

</head>  
<body>  
    <applet code = "main.FinalProject.class" width = "700px" height = "500px"></applet>  
</body>  
</html>

写入的 HTML 文件位于 [Eclipse Workspace]/FinalProject/bin/test.htm 中.HTML 中引用的 FinalProject.class 文件存在于 [Eclipse Workspace]/FinalProject/bin/main/FinalProject.class 中.FinalProject.class 文件作为主类,所以我很确定这是我需要运行的那个.这是一个带有 init()、actionPerformed()、paint() 方法和所有好东西的方法.

The HTML file this is written in is in [Eclipse Workspace]/FinalProject/bin/test.htm. The FinalProject.class file referenced in the HTML exists in [Eclipse Workspace]/FinalProject/bin/ main/FinalProject.class. The FinalProject.class file acts as the main class, so I'm pretty sure that's the one I need to run. It's the one with the init(), actionPerformed(), paint() methods and all that good stuff.

目前我正在尝试在我的计算机上离线运行它,所以我认为不应该有任何网络 URL.我使用 Eclipse 编写 Java 代码,不知道这有什么区别.不幸的是,Java 代码相当大,太多无法在此处重现,如果您认为有特定问题,我可以查看并发布该小部分.

Currently I'm trying to run this offline on my computer, so there shouldn't be any net URL's I would think. I used Eclipse to write the Java code, dunno if that makes any difference. Unfortunately, the Java code is rather large, too much to reproduce here, if there's something specific you think is the problem I can look and post that small section.

我的一些朋友设法让他们的工作正常运行,但是他们说他们必须删除所有 .png 文件(烦人但对我的项目可行).他们还说必须删除所有鼠标移动代码.我的程序有点依赖于此,我需要它才能正常工作.我知道必须有一种方法可以在线使用所有 MouseListener 和 MouseMoveListener 代码,但也许有点不同.我不知道这是否与此有关,但我想我会指出它只是为了安全.

A few of my friends managed to get theirs working, however they said they had to remove all their .png files (annoying but doable for my project). They also said had to remove all their mouse movement code. My program is kind of dependent on that, I need that for it to work at all. I know there MUST be a way to use all the MouseListener and MouseMoveListener code online, maybe it's a little different though. I dunno if that has something to do with this, but I figured I'd point it out just to be safe.

这里的任何帮助将不胜感激.

Any help here would be greatly appreciated.

基本上你会问这样的问题:如何为当今的浏览器(applet、embed、object)部署一个java小程序?

Basically you're asking something like: How to deploy a java applet for today's browsers (applet, embed, object)?

基于此,我认为您想要的是:

Based on that, I think what you want is:

<object 
  classid="clsid:CAFEEFAC-0015-0000-0000-ABCDEFFEDCBA"
  style="height: 500px; width: 700px;">
  <param name="code" value="FinalProject.class">
    <comment>
      <embed code="FinalProject.class"
        type="application/x-java-applet"
        height="500" width="700">
        <noembed>
          This browser appears to lack support for Java Applets.
        </noembed>
      </embed>
    </comment>
  </object>

现在,您的代码中有一个 main.FinalProject.class 文件名.看起来 FinalProject.class 更有可能.但你的可能是对的.在任何情况下,此 html 文件都需要与 main.FinalProject.classFinalProject.class 以及可能需要的任何类位于同一文件夹中.

Now, you have a filename of main.FinalProject.class in your code. It seems like FinalProject.class would be more likely. But yours could be right. In any case, this html file needs to be in the same folder as main.FinalProject.class or FinalProject.class and whatever classes may also be required.

现在,您可能还需要确保您的浏览器实际上可以运行小程序.请参阅:如何在网络浏览器中启用 Java?

Now, you may also need to make sure your browsers can actually run an applet. See: How do I enable Java in my web browser?

更新

根据 Andrew Thompson 的反馈,首选的解决方案是使用来自 Oracle 的 JavaScript,如下所示:

Based on feedback from Andrew Thompson, the preferred solution is to use JavaScript from Oracle, like this:

<script src="http://www.java.com/js/deployJava.js"></script>
<script>
    var attributes = {
        code:'FinalProject.class',
    width:700, height:500} ;
    var parameters = {}; // does the Applet take parameters?
    var version = '1.6' ; // does the Applet require a minimum version of Java
    deployJava.runApplet(attributes, parameters, version);
</script>

这需要能够加载任意 JavaScript,但您也可以捕获该 deployJava.js 并将其设为本地.可能值得一看.

This requires the ability to load arbitrary JavaScript, but you could also capture that deployJava.js and have that be local as well. Might be worth a look.