有没有办法从小程序本身重新加载/刷新 Java 小程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1046691/
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
Is there a way to reload/refresh a java applet from within the applet itself?
提问by liminal
I have a button on my applet (contained in a browser) that I would like to make reload or refresh the entire applet one of two ways:
我的小程序上有一个按钮(包含在浏览器中),我想通过以下两种方式之一重新加载或刷新整个小程序:
- Refresh the applet itself without having to refresh the browser
- Refresh the entire browser
- 无需刷新浏览器即可刷新小程序本身
- 刷新整个浏览器
Is this possible from within the applet?
这可以从小程序内部实现吗?
回答by liminal
Use the AppletContext.showDocument(...) method:
使用 AppletContext.showDocument(...) 方法:
applet.getAppletContext().showDocument(applet.getDocumentBase(), "_self")
That will load the document containing the applet in the same window/frame the applet is already loaded in.
这将在小程序已加载到的同一窗口/框架中加载包含小程序的文档。
回答by Rob Whelan
For the first part of your question, there are a few approaches. You could make a call to JavaScript and remove/re-add the applet from there. Use LiveConnectto call from Java to JS, and be sure to use the "MAYSCRIPT" attribute in your applet tag.
对于您问题的第一部分,有几种方法。您可以调用 JavaScript 并从那里删除/重新添加小程序。使用LiveConnect从 Java 调用 JS,并确保在您的小程序标签中使用“MAYSCRIPT”属性。
In your JavaScript method, remove the applet from the DOM and add a fresh one in its place.
在您的 JavaScript 方法中,从 DOM 中删除小程序并在其位置添加一个新的小程序。
You might also put the applet in an iFrame, which gives you more options.
您也可以将小程序放在 iFrame 中,这为您提供了更多选择。
But what is your aim in reloading the applet? It would be a safer approach to solve this problem completely within the applet instead of relying on JavaScript (and your JAR will likely be cached anyway, so you're basically just restarting the same applet).
但是您重新加载小程序的目的是什么?在小程序中完全解决这个问题会是一种更安全的方法,而不是依赖于 JavaScript(而且你的 JAR 可能会被缓存,所以你基本上只是重新启动同一个小程序)。
I suggest either:
我建议:
- code your applet so that you can reset its GUI and data with a method call, or
- run the applet via a small wrapper applet which can drop/recreate the real applet it is displaying.
- 对您的小程序进行编码,以便您可以通过方法调用重置其 GUI 和数据,或者
- 通过一个小的包装小程序运行小程序,它可以删除/重新创建它正在显示的真实小程序。
For the wrapper applet approach: implement AppletStub, instantiate the real applet, and display it as the center of a BorderLayout, something like this (in your start() method):
对于包装小程序方法:实现 AppletStub,实例化真正的小程序,并将其显示为 BorderLayout 的中心,如下所示(在您的 start() 方法中):
Applet applet = new TheRealApplet();
applet.setStub(this);
this.setLayout( new BorderLayout() );
add( applet, BorderLayout.CENTER );
applet.init();
applet.start();
...then call applet.stop() in the wrapper's stop() method, and add a new method that restarts the applet -- by stopping it, removing it from the layout, and adding a fresh instance.
...然后在包装器的 stop() 方法中调用 applet.stop(),并添加一个新方法来重新启动小程序——通过停止它,将它从布局中删除,并添加一个新的实例。

