java 从另一个 Applet 启动另一个 Applet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15312058/
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
Launching another Applet from another Applet
提问by Snehasish
I have made a Loader Applet which greets the user and when user clicks the button displayed on that Applet it then launches the main applet and the Loader Applet is destroyed.
我制作了一个加载程序小程序来迎接用户,当用户单击该小程序上显示的按钮时,它会启动主小程序,加载程序小程序将被销毁。
But on clicking Another applet is not launched !
但是点击另一个小程序不会启动!
Loader Applet:
加载程序小程序:
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
public class Loader extends JApplet implements ActionListener{
Display secondApplet;
Button button;
@Override
public void init() {
setSize(800,600);
}
@Override
public void start() {
setLayout(new FlowLayout());
button = new Button ("Click me !!");
add(button);
button.addActionListener(this);
}
@Override
public void paint(Graphics g) {
}
@Override
public void actionPerformed(ActionEvent e) {
secondApplet = (Display)getAppletContext().getApplet("Display");
if (secondApplet != null) {
secondApplet.init();
secondApplet.start();
}
else {
System.out.println("Not Running\n");
}
}
}
Display Applet:
显示小程序:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
public class Display extends JApplet {
@Override
public void init() {
setSize(600,400);
}
@Override
public void paint(Graphics g) {
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
}
How can I create an instance of the other Applet and destroy the current Applet !!
如何创建另一个 Applet 的实例并销毁当前 Applet !!
回答by emecas
Since an Applet/JApple is a java.awt.Panelitself, then you can embed one into the other, for your specific case you can embed Displayinto Loaderusing a Panel in Loaderto reload Displayas you need.
由于一个Applet / JApple是java.awt.Panel本身,那么你可以嵌入到彼此,为您的特定情况下,你可以嵌入显示到装载机在使用面板加载到重装显示,因为你需要。
Something like this:
像这样的东西:
Panel container = new Panel();
container.setLayout(new GridLayout(1,0));
container.add(secondApplet); //Display Applet
add(container):
secondApplet.init();
secondApplet.start();
button.setVisible(false);
回答by Andrew Thompson
There are so many things wrong with the applets it is hard to know where to begin. But let us concentrate 1st on a more sensible strategy to cause the change beteween one view and another.
小程序有很多问题,很难知道从哪里开始。但是,让我们首先关注一个更明智的策略,以引起一种观点和另一种观点之间的变化。
- Instead of having two applets, have two panels that are swapped in a
CardLayout
- Instead of having both applets in one page, call
getAppletContext().showDocument(secondAppletURL);
. PresumablysecondAppletURL
is different to the URL of the page that hosts the first applet.
- 不是有两个小程序,而是有两个面板,它们在一个
CardLayout
- 不要将两个小程序放在一页中,而是调用
getAppletContext().showDocument(secondAppletURL);
. 大概secondAppletURL
与承载第一个小程序的页面的 URL 不同。
OK - the problems with the first applet:
好的 - 第一个小程序的问题:
- Don't try to set the size of an applet. It is set by the HTML.
- All the methods calls in the
start()
method should be moved to theinit()
method, since thestart()
method might be called repeatedly. Then there is no reason to overridestart()
at all. - Don't mix Swing (e.g.
JApplet
) & AWT (e.g.Button
) components without good cause. In this case, useJButton
instead ofButton
. - As a stylistic point, it is typically better to create an anonymous inner
ActionListener
than implement it on the parent class. - Overriding
paint()
with an empty implementation is not a good idea. The originalpaint()
draws the applet and components, so now it would do nothing. - The methods in the
actionPerformed()
are equally nonsensical. An applet does not get included into theAppletContext
until afterinit()
&start()
have been called, which would mean that calling those methods explicitly causes them to be invoked a second time. While thestart()
method is meant to be called multiple times, theinit()
method should only be called once.
- 不要试图设置小程序的大小。它由 HTML 设置。
- 方法中的所有方法调用
start()
都应该移动到init()
方法中,因为start()
方法可能会被重复调用。那么根本没有理由覆盖start()
。 - 不要在没有
JApplet
正当Button
理由的情况下混合 Swing(例如)和 AWT(例如)组件。在这种情况下,请使用JButton
代替Button
。 - 作为一个风格点,创建匿名内部通常
ActionListener
比在父类上实现它更好。 paint()
用空实现覆盖不是一个好主意。原来是paint()
绘制小程序和组件,所以现在什么都不做。- 中的方法
actionPerformed()
同样荒谬。小程序不会被包含到调用AppletContext
之后,直到init()
&start()
被调用,这意味着显式调用这些方法会导致它们被第二次调用。虽然该start()
方法旨在被多次调用,但该init()
方法只能被调用一次。
2nd Applet.
第二个小程序。
- Ditto point 1. re the 1st applet.
- The overridden
paint()
method ..would paint the BG color (or the FG color - not sure) but nothing else. Again, don't override it.
- 同上点 1. 是第一个小程序。
- 覆盖的
paint()
方法..将绘制 BG 颜色(或 FG 颜色 - 不确定),但不会绘制其他颜色。再次,不要覆盖它。
回答by SudoRahul
Try this method to load another applet. See if it works.
试试这个方法来加载另一个小程序。看看它是否有效。
Class applet2 = Class.forName("Applet2");
Applet appletToLoad = (Applet)applet2.newInstance();
appletToLoad.setStub(this);
setLayout(new GridLayout(1,0));
add(appletToLoad);
appletToLoad.init();
appletToLoad.start();