简体   繁体   English

从另一个Applet启动另一个Applet

[英]Launching another Applet from another Applet

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!

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. 由于一个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);

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 不用两个小程序,而是在CardLayout中交换两个面板
  2. Instead of having both applets in one page, call getAppletContext().showDocument(secondAppletURL); 而不是将两个小程序都放在一个页面中,而是调用getAppletContext().showDocument(secondAppletURL); . Presumably secondAppletURL is different to the URL of the page that hosts the first applet. 大概secondAppletURL与承载第一个applet的页面的URL不同。

OK - the problems with the first applet: OK-第一个applet的问题:

  1. Don't try to set the size of an applet. 不要尝试设置小程序的大小。 It is set by the HTML. 它由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. 应该将start()方法中的所有方法调用移至init()方法,因为start()方法可能会重复调用。 Then there is no reason to override start() at all. 这样就完全没有理由重写start()了。
  3. Don't mix Swing (eg JApplet ) & AWT (eg Button ) components without good cause. 不要将Swing(例如JApplet )和AWT(例如Button )组件JApplet In this case, use JButton instead of Button . 在这种情况下,请使用JButton而不是Button
  4. As a stylistic point, it is typically better to create an anonymous inner ActionListener than implement it on the parent class. 从风格上讲,创建匿名内部ActionListener通常比在父类上实现它更好。
  5. Overriding paint() with an empty implementation is not a good idea. 用空的实现覆盖paint()不是一个好主意。 The original paint() draws the applet and components, so now it would do nothing. 原始的paint()绘制了applet和组件,因此现在它什么也不做。
  6. The methods in the actionPerformed() are equally nonsensical. actionPerformed()中的方法同样是荒谬的。 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. 在调用init()start()之后,applet不会包含在AppletContext ,这意味着显式调用这些方法将导致第二次调用它们。 While the start() method is meant to be called multiple times, the init() method should only be called once. 尽管start()方法应被多次调用,但init()方法仅应被调用一次。

2nd Applet. 第二小程序。

  1. Ditto point 1. re the 1st applet. 同上点1.重新成为第一小程序。
  2. The overridden paint() method ..would paint the BG color (or the FG color - not sure) but nothing else. 覆盖的paint()方法将绘制BG颜色(或FG颜色-不确定),但仅涂其他颜色。 Again, don't override it. 同样,不要覆盖它。

Try this method to load another applet. 尝试使用此方法加载另一个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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM