Launching another Applet from another Applet

为君一笑 提交于 2019-12-08 03:40:02

问题


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.

  1. Instead of having two applets, have two panels that are swapped in a CardLayout
  2. Instead of having both applets in one page, call getAppletContext().showDocument(secondAppletURL);. Presumably secondAppletURL is different to the URL of the page that hosts the first applet.

OK - the problems with the first applet:

  1. Don't try to set the size of an applet. It is set by the HTML.
  2. All the methods calls in the start() method should be moved to the init() method, since the start() method might be called repeatedly. Then there is no reason to override start() at all.
  3. Don't mix Swing (e.g. JApplet) & AWT (e.g. Button) components without good cause. In this case, use JButton instead of Button.
  4. As a stylistic point, it is typically better to create an anonymous inner ActionListener than implement it on the parent class.
  5. Overriding paint() with an empty implementation is not a good idea. The original paint() draws the applet and components, so now it would do nothing.
  6. The methods in the actionPerformed() are equally nonsensical. An applet does not get included into the AppletContext until after init() & start() have been called, which would mean that calling those methods explicitly causes them to be invoked a second time. While the start() method is meant to be called multiple times, the init() method should only be called once.

2nd Applet.

  1. Ditto point 1. re the 1st applet.
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!