Credits

Tuesday, March 16, 2010

Another Java Applet

When an applet is run, it actually executes the initialization method, then goes to start method until one  minimizes/restores, then the applet is stopped, then goes back to start method again. But, when clicks on exit button, then the applet is destroyed and freed from PC's memory.


So, even if we don't write our own applet methods, Java itself calls the default methods and run them. 


See the program below for another applet example.

[+/-] show/hide



/*
* adopted from another java source
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AppletPartyPlanner extends Applet implements ActionListener {
JLabel companyName = new JLabel ("STI Programs");
JButton calcButton =new JButton("Calculate");
JLabel prompt =new JLabel ("Enter the number of guests at your event: ");
JTextField numGuests = new JTextField(5);
JLabel perPersonResult = new JLabel ("Plan with us.");
JLabel totalResult = new JLabel("The more, the merrier.");
Font bigFont = new Font("Helvetica",Font.BOLD,24);
public void init()
{
companyName.setFont(bigFont);
add(companyName);
add(prompt);
add(numGuests);
add(calcButton);
calcButton.addActionListener(this);
numGuests.addActionListener(this);
add(totalResult);
}
public void start()
{
perPersonResult.setText("Plan with us.");
numGuests.setText("0");
totalResult.setText("The more, the merrier.");
invalidate();
validate();

}

public void actionPerformed(ActionEvent e)
{ int[] guestLimit ={0,25,50,100,200,500,1000};
int[] ratePerGuest ={27,25,22,19,17,14,11};
int guests = Integer.parseInt(numGuests.getText());
int individualFee=0,eventFee=0;
int x=0,a=0;
for (x=5;x>=0;--x)
if(guests>=guestLimit[x])
{ individualFee=ratePerGuest[x];
eventFee=guests * individualFee;
x=0;}
perPersonResult.setText(" $ "+individualFee +" per person");
totalResult.setText("Event cost $"+eventFee);
}

}

AddThis