从另一个小程序启动另一个小程序 [英] Launching another Applet from another Applet

查看:194
本文介绍了从另一个小程序启动另一个小程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了装载机的Applet其中问候用户,当用户点击该小程序显示的按​​钮,它然后启动主小应用程序和Loader小程序被破坏。

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 !

装载机小程序:

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");
        }
    }
}

显示小程序:

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());
    }
}

如何创建另一个小程序的实例,并销毁当前的Applet!

推荐答案

由于一个Applet / JApple是一个 java.awt.Panel 本身,那么你可以嵌入一个到另一个,为您的特定情况下,你可以嵌入的显示装载机在使用面板的加载以重新加载的显示,因为你需要。

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.

事情是这样的:

Panel container = new Panel();
container.setLayout(new GridLayout(1,0));
container.add(secondApplet); //Display Applet
add(container):

secondApplet.init();
secondApplet.start();

button.setVisible(false);

这篇关于从另一个小程序启动另一个小程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆