如何在 GWT 页面中显示 Java 小程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/406963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display java applet inside GWT page?
提问by user9945
I'm probably missing something simple here, but I can't find the answer elsewhere. I just want to display an applet in my GWT code.
我可能在这里遗漏了一些简单的东西,但我在其他地方找不到答案。我只想在我的 GWT 代码中显示一个小程序。
OS: Windows XP Java: JDK 1.6.0_10 Other: GWT, GWT-Ext 2.0.5
操作系统:Windows XP Java:JDK 1.6.0_10 其他:GWT、GWT-Ext 2.0.5
Here is the applet (obviously simplified for testing):
这是小程序(显然简化了测试):
package foo.applet;
import javax.swing.JApplet;
import java.awt.Graphics;
public class HelloApplet extends JApplet
{
public void paint(Graphics g)
{
g.drawRect(0, 0,
getSize().width - 1,
getSize().height - 1);
g.drawString("Hello world!", 5, 15);
}
}
Here is the code calling it:
这是调用它的代码:
package foo.applet;
import com.google.gwt.user.client.ui.HTML;
import com.gwtext.client.widgets.Panel;
public class AppletPanel extends Panel
{
public AppletPanel()
{
HTML applet = new HTML();
applet.setHTML("<applet name=\"HelloApplet\" code=\"HelloApplet.class\" width=\"300\" height=\"300\"" );
this.add(applet);
}
}
}
When I launch the app in hosted mode, the jvm crashes (filed incident 1425130 with Sun).
当我以托管模式启动应用程序时,jvm 崩溃(向 Sun 提交事件 1425130)。
When I try to compile the GWT code for running in a browser, I get this:
当我尝试编译 GWT 代码以在浏览器中运行时,我得到以下信息:
[ERROR] Errors in 'file:/C:/<blah>/applet/HelloApplet.java'
[ERROR] Line 3: The import javax.swing cannot be resolved
[ERROR] Line 4: The import java.awt cannot be resolved
[ERROR] Line 6: JApplet cannot be resolved to a type
[ERROR] Line 8: Graphics cannot be resolved to a type
[ERROR] Line 11: The method getSize() is undefined for the type HelloApplet
[ERROR] Line 12: The method getSize() is undefined for the type HelloApplet
Obviously I'm missing some applet library, but I've grepped through all the jars in the jdk and tried including all of the ones that list JApplet or awt (plugin.jar, resources.jar, rt.jar, deploy.jar, javaws.jar).
显然我缺少一些小程序库,但我已经浏览了 jdk 中的所有 jar 并尝试包括所有列出 JApplet 或 awt 的那些(plugin.jar、resources.jar、rt.jar、deploy.jar, javaws.jar)。
Also, I'm pretty sure once I solve this problem there's another one lurking right after it, but I'll save that for another question.
另外,我很确定一旦我解决了这个问题,就会有另一个问题紧随其后,但我会把它留到另一个问题中去。
Thanks!
谢谢!
The legacy app is not an applet - it is a thick-client Swing application. We've hacked it to run as an applet because our customers want a browser client and this is the fastest way to get that done.
遗留应用程序不是小程序——它是一个胖客户端 Swing 应用程序。我们已经将它改成小程序运行,因为我们的客户需要浏览器客户端,这是完成任务的最快方法。
I don't know if GWT would accept the JPanel solution - the app is not written in any way that GWT can parse - i.e. it's not using the GWT API, it's using the Swing API. AFAIK, the only way to mix Swing with GWT would be in an applet fashion.
我不知道 GWT 是否会接受 JPanel 解决方案 - 该应用程序不是以 GWT 可以解析的任何方式编写的 - 即它不使用 GWT API,它使用的是 Swing API。AFAIK,将 Swing 与 GWT 混合的唯一方法是以小程序方式。
Am I missing something?
我错过了什么吗?
回答by Yoni Roit
Are you trying to GWT-compile your applet?
你想用 GWT 编译你的小程序吗?
This won't work, as GWT compilation (which is just translation from Java to Javascript) supports only handful of Java libraries and certainly not applets.
这是行不通的,因为 GWT 编译(只是从 Java 到 Javascript 的转换)仅支持少数 Java 库,当然不支持小程序。
Make sure that your applet is not on GWT source path (move it to another package).
确保您的小程序不在 GWT 源路径上(将其移动到另一个包)。
参考:http: //code.google.com/docreader/#p=google-web-toolkit-doc-1-5&s=google-web-toolkit-doc-1-5&t=RefJreEmulation
回答by Andrew Newdigate
Don't use the GWTCompiler to compile your applet code. I would recommend creating a second module (or project) that contains only the applet code. Compile this to a separate JAR using the standard Javac compiler (or your IDE/ant)
不要使用 GWTCompiler 编译您的小程序代码。我建议创建仅包含小程序代码的第二个模块(或项目)。使用标准 Javac 编译器(或您的 IDE/ant)将其编译为单独的 JAR
The GWTCompiler uses a subset of the Java libraries, and should only be used to generate the code which is going to eventually run as Javascript.
GWTCompiler 使用 Java 库的一个子集,并且应该只用于生成最终将作为 Javascript 运行的代码。
回答by duffymo
Google found this. One of the responses says: "The previous poster is right, the shell can not handle embedded things like Flash or Applets. There are some restrictions in the SWT component used to run the browser inside of the shell. A bug report has been associated with this issue, you may want to keep an eye on it for future updates."
谷歌找到了这个。其中一个回应说:“之前的海报是对的,shell 无法处理 Flash 或 Applets 之类的嵌入式内容。用于在 shell 内部运行浏览器的 SWT 组件存在一些限制。错误报告已与这个问题,您可能需要关注它以备将来更新。”
Looks like it can't be done.
好像做不到啊
回答by Josh
I found this during research for a gwtapp, and even though this is an old thread thought i would post a method to run an applet inside of gwt.
我在研究gwt应用程序时发现了这一点,尽管这是一个旧线程,但我认为我会发布一种在 gwt 中运行小程序的方法。
First create two distinct projects, one for your applet and one for gwt.
make your applet as normal.
首先创建两个不同的项目,一个用于您的小程序,一个用于gwt. 使您的小程序正常运行。
jar your applet.
jar 你的小程序。
then SIGN your .jarwith jarsigner.
然后签上你.jar用jarsigner。
create your gwtmodules as normal.
gwt像往常一样创建你的模块。
to embed the applet I use a gwtHTML object with an applet tag like this:
为了嵌入小程序,我使用了一个gwt带有小程序标记的HTML 对象,如下所示:
(applet MAYSCRIPT code = 'com.myapplet.MyApplet' id ='myApplet' jnlp_href = '/spplets/MyApplet.jnlp', width=500, height=400)(/applet)
(applet MAYSCRIPT code = 'com.myapplet.MyApplet' id ='myApplet' jnlp_href = '/spplets/MyApplet.jnlp', width=500, height=400)(/applet)
Simply add the HTML widget to a contentPanel and the gui part is done.
只需将 HTML 小部件添加到 contentPanel 中,gui 部分就完成了。
The applet .jar will need to go in the /war for your gwt project.
小程序 .jar 将需要进入您的 gwt 项目的 /war 中。
Along with the .jaryou will need to write a .jnlpfile to launch the applet.
与 一起,.jar您将需要编写一个.jnlp文件来启动小程序。
This will embed an applet in gwt and run it in hosted or production mode. The key is to SIGN your .JARand launch it with .JNLP
这将在 gwt 中嵌入一个小程序并在托管或生产模式下运行它。关键是签名.JAR并启动它.JNLP
回答by Bob Futrelle
A light-heavyweight app might mix GWT and JNLP. Then we could get the bigger jars onto people's machines rather transparently. As an example, I'd like to use the Batik toolkit or other SVG-related goodies to have SVG in my GWT app, rather than being forced to use only png or other raster formats.
轻量级应用程序可能会混合使用 GWT 和 JNLP。然后我们可以相当透明地将更大的罐子放到人们的机器上。例如,我想使用 Batik 工具包或其他与 SVG 相关的好东西在我的 GWT 应用程序中包含 SVG,而不是被迫仅使用 png 或其他光栅格式。
- Tyro - you can also mail your thoughts on this to me at [email protected]
- Tyro - 您也可以通过 [email protected] 将您的想法发送给我
回答by duffymo
"The import javax.swing cannot be resolved" - sorry, I'm not a GWT maven, but this error is classpath-esque. Sounds like GWT can't find the rt.jar for your JVM.
“无法解析导入 javax.swing” - 抱歉,我不是 GWT 专家,但此错误是类路径式的。听起来 GWT 找不到适用于您的 JVM 的 rt.jar。

