Tuesday, May 26, 2009

modity fresh code

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

Label lblproductName = new Label("Product Name");
Label lblproductPrice = new Label("Product Price");
Label lblnumberUnits = new Label ("Number of Units");
Label lbltotalPrice = new Label ("Total Price");

Button calculateTotal = new Button("Calculate Total");
Button clear = new Button("Clear");

public void init()
{
setLayout(new FlowLayout());

add(lblproductName);
add(productName);
add(lblproductPrice);
add(productPrice);
add(lblnumberUnits);
add(numberUnits);
add(lbltotalPrice);
add(totalPrice);
add(calculateTotal);
add(clear);

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

Calculate me = new Calculate();
}

public void actionPerformed (ActionEvent e) {

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);
}
if (e.getSource()==clear)
{
productName.setText("");
productPrice.setText("");
numberUnits.setText("");
totalPrice.setText("");
}
}
}

No comments: