问题
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 !!
回答1:
Since an Applet/JApple is a java.awt.Panel itself, then you can embed one into the other, for your specific case you can embed Display into Loader using a Panel in Loader to reload Display as you need.
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);
回答2:
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.
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.
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.
回答3:
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();
来源:https://stackoverflow.com/questions/15312058/launching-another-applet-from-another-applet