2

我的小程序(包含在浏览器中)上有一个按钮,我想通过以下两种方式之一重新加载或刷新整个小程序:

  1. 无需刷新浏览器即可刷新小程序本身
  2. 刷新整个浏览器

这可以从小程序内部实现吗?

4

2 回答 2

3

使用 AppletContext.showDocument(...) 方法:

applet.getAppletContext().showDocument(applet.getDocumentBase(), "_self")

这将在已加载小程序的同一窗口/框架中加载包含小程序的文档。

于 2009-06-26T00:45:33.820 回答
0

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 LiveConnect to call from Java to JS, and be sure to use the "MAYSCRIPT" attribute in your applet tag.

In your JavaScript method, remove the applet from the DOM and add a fresh one in its place.

You might also put the applet in an iFrame, which gives you more options.

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).

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.

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):

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.

于 2010-06-01T22:39:42.420 回答