Simple Budget Calculator

Using Java Swing Applet


Java Applet Code

// Swing applet calculator demo
// copyright 2011, Michael A. Stratton

import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JOptionPane;

public class budgetCalculator extends JApplet
{
private double sum; // sum of values

// initialize applet
public void init()
{
// input total income from user
String incomeNumber = JOptionPane.showInputDialog(
"Enter your total income");

//inupt total expenses from user
String expenseNumber = JOptionPane.showInputDialog(
"Enter your total expenses");

// convert number from string to double
double inNumber = Double.parseDouble(incomeNumber);
double exNumber = Double.parseDouble(expenseNumber);

sum = inNumber - exNumber; // balance
} // end method init

// draw results in a rectangle in background
public void paint(Graphics g)
{
super.paint(g); // call superclass version of method paint


//draw rectangle starting from (15,10) that is 270 pixels wide and 20 pixels tall
g.drawString("Your remaning balance is " + sum, 25, 25);
} // end method paint


} // end class budgetCalculator

HTML Code (Enter into the <body> section </body>

<applet code = "budgetCalculator.class" width = "300" height = "50">
</applet>