如何在GWT页面中显示java小程序?
我可能在这里遗漏了一些简单的东西,但我在其他地方找不到答案。 我只想在我的 GWT 代码中显示一个小程序。
操作系统:Windows XP Java:JDK 1.6.0_10 其他:GWT、GWT-Ext 2.0.5
这是小程序(显然是为了测试而简化的):
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);
}
}
这是调用它的代码:
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);
}
}
当我以托管模式启动应用程序时,jvm 崩溃(向 Sun 提交了事件 1425130)。
当我尝试编译 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
显然我缺少一些小程序库,但我已经对 jdk 中的所有 jar 进行了 grep 并尝试包括所有列出 JApplet 或awt(plugin.jar、resources.jar、rt.jar、deploy.jar、javaws.jar)。
另外,我很确定一旦解决了这个问题,后面就会潜伏着另一个问题,但我会把它留到另一个问题。
谢谢!
旧应用程序不是小程序 - 它是胖客户端 Swing 应用程序。 我们将其破解为小程序运行,因为我们的客户需要浏览器客户端,而这是完成此任务的最快方法。
我不知道 GWT 是否会接受 JPanel 解决方案 - 该应用程序不是以 GWT 可以解析的任何方式编写的 - 即它不使用 GWT API,而是使用 Swing API。 AFAIK,将 Swing 与 GWT 混合的唯一方法是采用小程序方式。
我错过了什么吗?
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.
OS: Windows XP
Java: JDK 1.6.0_10
Other: 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).
When I try to compile the GWT code for running in a browser, I get this:
[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).
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.
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.
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我在研究
gwt
应用程序时发现了这一点,尽管这是一个旧线程,但我认为我会发布一种在 gwt 内运行小程序的方法。首先创建两个不同的项目,一个用于您的小程序,另一个用于
gwt
。使您的小程序正常运行。
罐装你的小程序。
然后使用
jarsigner
签署您的.jar
。正常创建您的
gwt
模块。要嵌入小程序,我使用带有小程序标记的
gwt
HTML 对象,如下所示:(applet MAYSCRIPT code = 'com.myapplet.MyApplet' id ='myApplet' jnlp_href = '/spplets/ MyApplet.jnlp', width=500, height=400)(/applet)
只需将 HTML 小部件添加到 contentPanel 中,GUI 部分就完成了。
小程序 .jar 需要放入您的 gwt 项目的 /war 中。
除了
.jar
之外,您还需要编写一个.jnlp
文件来启动小程序。这将在 gwt 中嵌入一个小程序并以托管或生产模式运行它。 关键是签署您的
.JAR
并使用.JNLP
启动它I found this during research for a
gwt
app, and even though this is an old thread thought i would post a method to run an applet inside of gwt.First create two distinct projects, one for your applet and one for
gwt
.make your applet as normal.
jar your applet.
then SIGN your
.jar
withjarsigner
.create your
gwt
modules as normal.to embed the applet I use a
gwt
HTML object with an applet tag like this:(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.
The applet .jar will need to go in the /war for your gwt project.
Along with the
.jar
you will need to write a.jnlp
file to launch the applet.This will embed an applet in gwt and run it in hosted or production mode. The key is to SIGN your
.JAR
and launch it with.JNLP
轻量级应用程序可能会混合使用 GWT 和 JNLP。
然后我们就可以相当透明地将更大的罐子放到人们的机器上。
例如,我想使用 Batik 工具包或其他与 SVG 相关的工具在我的 GWT 应用程序中使用 SVG,而不是被迫仅使用 png 或其他光栅格式。
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.
“导入 javax.swing 无法解析” - 抱歉,我不是 GWT maven,但这个错误是类路径式的。 听起来 GWT 找不到适用于您的 JVM 的 rt.jar。
"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.
您是否正在尝试 GWT 编译您的小程序?
这是行不通的,因为 GWT 编译(只是从 Java 到 Javascript 的翻译)仅支持少数 Java 库,当然不支持小程序。
确保您的小程序不在 GWT 源路径上(将其移至另一个包)。
参考:http://code.google.com/docreader/#p=google-web-toolkit-doc-1-5&s=google-web-toolkit-doc-1-5&t =RefJreEmulation
Are you trying to GWT-compile your applet?
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.
Make sure that your applet is not on GWT source path (move it to another package).
Reference: http://code.google.com/docreader/#p=google-web-toolkit-doc-1-5&s=google-web-toolkit-doc-1-5&t=RefJreEmulation
不要使用 GWTCompiler 来编译您的小程序代码。 我建议创建第二个仅包含小程序代码的模块(或项目)。 使用标准 Javac 编译器(或您的 IDE/ant)将其编译为单独的 JAR
GWTCompiler 使用 Java 库的子集,并且只应用于生成最终将作为 Javascript 运行的代码。
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)
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.
Google 发现了此内容。 其中一个回复说:“上一张海报是正确的,shell 无法处理 Flash 或 Applet 等嵌入式内容。用于在 shell 内运行浏览器的 SWT 组件存在一些限制。错误报告已与这个问题,您可能需要关注以获取未来的更新。”
看来是做不到了。
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."
Looks like it can't be done.