单击按钮打开另一个小程序

Click Button to Open Another Applet

我想通过点击一个按钮调用另一个小程序;然后旧小程序将关闭或重新加载到新小程序。

我的 Action Listener 还没有任何东西。

public class ConImage extends JApplet implements ActionListener {
    Button btn;
    Applet second;

    public void init()
    {
        setSize(1600,900);
        setLayout(null);
        btn=new Button("Replace with other applet");

        add(btn);
        btn.addActionListener(this);
    }


    public void paint(Graphics g)
    {
        super.paint(g);

        btn.setLocation(100, 100);
        btn.setSize(100, 50);            
    }

    public void actionPerformed(ActionEvent e)
    {   second=null;
        second= getAppletContext().getApplet("SecondClass");
        if (second!=null)
        {
            if(e.getSource()==Time)
            {
                SecondClass ma= (SecondClass) second;

            }
        }
    }
}

我很确定这是不可能的,因为 Java 的安全系统。最好的方法是拥有一个 master class,它有一个 JApplet 的数组。在那个主小程序上,我将创建一个方法,从数组中设置可见小程序,调用 init() 并在请求渲染时调用该小程序的 paint()

像这样:

public class MasterApplet extends JApplet {

private int index = 0;
private JApplet[] applets;

public void init(){
    JApplet appletA = new AppletA();
    JApplet appletB = new AppletB();
    applets = new JApplet[]{appletA, appletB};
    setViewing(index);
}

public void paint(Graphics g){
    applets[index].paint(g);
}

public void setViewing(int idex){
    index = idex;
    applets[idex].init();
    revalidate();
    repaint();
}

几乎如果你想改变小程序,将它添加到小程序数组,然后用那个小程序的索引调用setViewing()