Tuesday, May 26, 2009

Fresh Applet

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

public class Calculate extends JApplet implements ActionListener
{
// Declare and allocate Components

TextField productName = new TextField(9);
TextField productPrice = new TextField(9);
TextField numberUnits = new TextField(9);
TextField totalPrice = new TextField(9);

Button calculateTotal = new Button("click here");
Button clear = new Button("dun click here");

public void init()
{

setSize(120,320);
setLocation(100,100);
setLayout(new FlowLayout());
// addWindowListener(new WindowDestroyer());

// Arrange Component layout
add(new Label("Your name sir"));
add(productName);
add(new Label("The ticket price sir:"));
add(productPrice);
add(new Label("Destination:"));
add(numberUnits);
add(new Label("nah total ah"));
add(totalPrice);
add(calculateTotal);
add(clear);
//add(totalPrice);


// Register Component Listeners
productName.addActionListener(this);
productPrice.addActionListener(this);
numberUnits.addActionListener(this);
calculateTotal.addActionListener(this);
clear.addActionListener(this);

setVisible(true);

Calculate me = new Calculate();
}

public void actionPerformed (ActionEvent e) {

// Respond to Action Events:
// 1. tFahr TextField
// 2. tCent TextField
double price, units, total;

if (e.getSource()==calculateTotal)
{
price = new Double(productPrice.getText()).doubleValue();
units = new Double(numberUnits.getText()).doubleValue();
total=price*units;
totalPrice.setText(Double.toString(total));
JOptionPane.showMessageDialog(null, "Total Price: " + total);

// totalPrice.setText(total);

}
if (e.getSource()==clear)
{
productName.setText("");
productPrice.setText("");
numberUnits.setText("");
totalPrice.setText("");
}



}
}

No comments: