7 Oct 2014

Demonstrating opening of an Applet from an Applet on button click

Code for CmdDemoWithApplet.class file:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/*
<applet code="CmdDemoWithApplet.class" height=200 width=400>
</applet>
*/

public class CmdDemoWithApplet extends Applet implements ActionListener
{
Button b1,b2,b3;
    public void init()
    {
    setLayout(new GridLayout(3,1,35,35));
    Color c=new Color(187,224,148);
    Color c1=new Color(98,167,15);
    Color c2=new Color(225,0,0);
    setBackground(c);
    setForeground(c2);
    b1=new Button("Open Applet1");
    b2=new Button("Open Applet2");
    b3=new Button("Open Applet3");
    add(b1);
    b1.setBackground(c1);
    add(b2);
    b2.setBackground(c1);
    add(b3);
    b3.setBackground(c1);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    }
   
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==b1)
        {
            try
            {
            Process p1=Runtime.getRuntime().exec("appletviewer Applet1.java");
            }
            catch(Exception e)
            {
            }
        }
        else if(ae.getSource()==b2)
        {
            try
            {
            Process p2=Runtime.getRuntime().exec("appletviewer Applet2.java");
            }
            catch(Exception e)
            {
            }
        }
        else if(ae.getSource()==b3)
        {
            try
            {
            Process p3=Runtime.getRuntime().exec("appletviewer Applet3.java");
            }
            catch(Exception e)
            {
            }
        }
    }
}

Code for Applet1.class file:


import java.applet.*;
import java.awt.*;

/*
<applet code="Applet1.class" height=50 width=475>
</applet>
*/

public class Applet1 extends Applet
{
Label l1;
    public void init()
    {
    setLayout(new BorderLayout());
    Color c1=new Color(255,191,151);
    Color c2=new Color(230,84,0);
    setBackground(c1);
    setForeground(c2);
    Font f=new Font("Courier",Font.BOLD,50);
    l1=new Label("This is Applet1.");
    l1.setFont(f);
    add("Center",l1);
    }
}

Code for Applet2.class file:

import java.applet.*;
import java.awt.*;

/*
<applet code="Applet2.class" height=50 width=475>
</applet>
*/

public class Applet2 extends Applet
{
Label l1;
    public void init()
    {
    setLayout(new BorderLayout());
    Color c1=new Color(255,191,151);
    Color c2=new Color(230,84,0);
    setBackground(c1);
    setForeground(c2);
    Font f=new Font("Courier",Font.BOLD,50);
    l1=new Label("This is Applet2.");
    l1.setFont(f);
    add("Center",l1);
    }
}

Code for Applet3.class file:

import java.applet.*;
import java.awt.*;

/*
<applet code="Applet3.class" height=50 width=475>
</applet>
*/

public class Applet3 extends Applet
{
Label l1;
    public void init()
    {
    setLayout(new BorderLayout());
    Color c1=new Color(255,191,151);
    Color c2=new Color(230,84,0);
    setBackground(c1);
    setForeground(c2);
    Font f=new Font("Courier",Font.BOLD,50);
    l1=new Label("This is Applet3.");
    l1.setFont(f);
    add("Center",l1);
    }
}


The snapshot shows you the sequence of executing the commands in Command Prompt and shows the applets.

No comments:

Post a Comment