1 Replies - 1540 Views - Last Post: 02 May 2009 - 10:28 PM Rate Topic: -----

#1 lupo66maniac   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-April 09

Creating an applet out of an application with multiple frames

Post icon  Posted 02 May 2009 - 07:08 PM

Alright, I have a financial calculator that I coded that has multiple frames. I would like to turn this into an applet that I can put in my website. What is the easiest way to go about this? Is it even possible or do I need to start from scratch?
/*--------------------------------------------------------------------------*
* File:		P09GUI.java													 *
* Author:	Jason French													*
*--------------------------------------------------------------------------*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;


public class P09
{ public static P09Frame1 Frame1;
  public static P09Frame2 Frame2;
  public static P09Frame3 Frame3;
  public static P09Frame4 Frame4;

/*----------------------- Method: Main ------------------------------------*/
public static void main(String Args[])
{ Frame1 = new P09Frame1("Financial Calculator");
  Frame2 = new P09Frame2("Compound Interest Calculator");
  Frame3 = new P09Frame3("Amortization Calculator");
  Frame4 = new P09Frame4("Annuity Calculator");
  ShowFrame1();
} //main
/*----------------------- Method: ShowFrame1()-----------------------------*/
public static void ShowFrame1()
{ Frame1.setVisible(true);
  Frame2.setVisible(false);
  Frame3.setVisible(false);
  Frame4.setVisible(false);
}
/*----------------------- Method: ShowFrame2()-----------------------------*/
public static void ShowFrame2()
{ Frame1.setVisible(false);
  Frame2.setVisible(true);
  Frame3.setVisible(false);
  Frame4.setVisible(false);
  Frame2.Reset();
}
/*----------------------- Method: ShowFrame3()-----------------------------*/
public static void ShowFrame3()
{ Frame1.setVisible(false);
  Frame2.setVisible(false);
  Frame3.setVisible(true);
  Frame4.setVisible(false);
  Frame3.Reset();
}
/*----------------------- Method: ShowFrame4()-----------------------------*/
public static void ShowFrame4()
{ Frame1.setVisible(false);
  Frame2.setVisible(false);
  Frame3.setVisible(false);
  Frame4.setVisible(true);
  Frame4.Reset();
}
} //class
/*----------------------- End of File:  P08GUI.java -----------------------*/



* File:		P09Frame1.java												  *
* Author:	Jason French													*
*--------------------------------------------------------------------------*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class P09Frame1 extends Frame implements ActionListener
{ private JPanel pnlHead	  = new JPanel(new GridLayout(2,1));
  private JPanel pnlButton	= new JPanel(new GridLayout(4,1,50,50));
  private JPanel pnlBlank2	= new JPanel(new GridLayout(1,1));
  private JPanel pnlBlank3	= new JPanel(new GridLayout(1,1));
  private JPanel pnlBlank1	= new JPanel(new GridLayout(1,1));
  private JLabel lblMain	  = new JLabel("Main Menu", JLabel.CENTER);
  private JLabel lblBlank	 = new JLabel("	  ");
  private JLabel lblBlank2	= new JLabel("					   ");
  private JLabel lblBlank3	= new JLabel("  ");
  private JButton btnCompound = new JButton("Compound Interest");
  private JButton btnAmort	= new JButton("Amortization");
  private JButton btnAnnuity  = new JButton("Annuity");
  private JButton btnExit	 = new JButton("Exit");

public P09Frame1(String Title)
{ super(Title);

  Toolkit tk = Toolkit.getDefaultToolkit();
  Dimension screenSize = tk.getScreenSize();
  int screenHeight = screenSize.height;
  int screenWidth = screenSize.width;
  int centerHeight, centerWidth;

  centerHeight= screenHeight-600;
  centerWidth = screenWidth-600;

  this.setSize(600,600);
  this.setLocation(centerWidth/2, centerHeight/2);
  this.setBackground(Color.gray);
  this.setForeground(Color.black);
  this.setLayout(new BorderLayout());
  this.add("North", pnlHead);
  this.add("Center", pnlButton);
  this.add("East", pnlBlank2);
  this.add("West", pnlBlank3);
  this.add("South", pnlBlank1);

  //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  //lblMain.setOpaque(true);
  lblMain.setBackground(Color.gray);
  lblMain.setForeground(Color.blue);
  lblMain.setFont(new Font("Serif",Font.BOLD,45));
  lblBlank.setBackground(Color.gray);
  lblBlank.setForeground(Color.blue);
  lblBlank.setFont(new Font("Serif",Font.BOLD,45));
  pnlHead.add(lblMain);
  pnlHead.add(lblBlank);
  pnlBlank2.add(lblBlank);
  pnlBlank3.add(lblBlank2);
  pnlBlank1.add(lblBlank3);


  btnCompound.setFont(new Font("Serif", Font.BOLD, 30));
  btnCompound.setSize(10,10);
  pnlButton.add(btnCompound);
  btnAmort.setFont(new Font("Serif", Font.BOLD, 30));
  pnlButton.add(btnAmort);
  btnAnnuity.setFont(new Font("Serif", Font.BOLD, 30));
  pnlButton.add(btnAnnuity);
  btnExit.setFont(new Font("Serif", Font.BOLD, 30));
  pnlButton.add(btnExit);

  btnCompound.addActionListener(this);
  btnAmort.addActionListener(this);
  btnAnnuity.addActionListener(this);
  btnExit.addActionListener(this);


}


public void actionPerformed(ActionEvent Ae)
{ String cmd = Ae.getActionCommand();
  if (Ae.getSource() instanceof JButton)
  { if (cmd=="Compound Interest") P09.ShowFrame2();
	else if (cmd=="Amortization") P09.ShowFrame3();
	else if (cmd=="Annuity") P09.ShowFrame4();
	else ExitProgram();
  }
} //actionperformed

private void ExitProgram()
{ P09.Frame1.setVisible(false);
  dispose();
  System.exit(99);
} //exitprogram

class WinListener extends WindowAdapter
{ public void windowClosing(WindowEvent We)
  { ExitProgram(); }
} //winlistener
} //class
/*----------------------- End of File:  P09Frame1.java --------------------*/


/*--------------------------------------------------------------------------*
* File:		P09Frame2.java												  *
* Author:	Jason French													*
*--------------------------------------------------------------------------*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class P09Frame2 extends Frame implements ActionListener
{ private JPanel pnlHead	 = new JPanel(new GridLayout(2,1));
  private JPanel pnlButton   = new JPanel(new GridLayout(1,3));
  private JPanel pnlArea	 = new JPanel(new GridLayout(5,2,50,50));
  private JPanel pnlBlank2   = new JPanel(new GridLayout(1,1));
  private JPanel pnlBlank3   = new JPanel(new GridLayout(1,1));
  private JLabel lblMain	 = new JLabel("Compound Interest Calculator", JLabel.CENTER);
  private JLabel lblBlank	= new JLabel("	");
  private JLabel lblBlank2	= new JLabel("	 ");
  private JLabel lblAmount   = new JLabel("  Enter Amount Invested:");
  private JLabel lblInterest = new JLabel("  Enter Annual Interest Rate:");
  private JLabel lblPeriod   = new JLabel("  Enter Compounding Periods per Year:");
  private JLabel lblYears	= new JLabel("  Enter Number of Years:");
  private JLabel lblTotal	= new JLabel("  Compounded Amount:");
  private JTextField txtAmount  = new JTextField("");
  private JTextField txtInterest= new JTextField("");
  private JTextField txtPeriod  = new JTextField("");
  private JTextField txtYears   = new JTextField("");
  private JTextField txtTotal   = new JTextField("");
  private JButton btnCalculate= new JButton("Calculate");
  private JButton btnReset	= new JButton("Reset");
  private JButton btnMain	 = new JButton("Main");

public P09Frame2(String Title)
{ super(Title);

  Toolkit tk = Toolkit.getDefaultToolkit();
  Dimension screenSize = tk.getScreenSize();
  int screenHeight = screenSize.height;
  int screenWidth = screenSize.width;
  int centerHeight, centerWidth;

  centerHeight= screenHeight-600;
  centerWidth = screenWidth-800;

  this.setSize(800,600);
  this.setLocation(centerWidth/2, centerHeight/2);
  this.setBackground(Color.gray);
  this.setForeground(Color.black);
  this.setLayout(new BorderLayout());
  this.add("North", pnlHead);
  this.add("Center", pnlArea);
  this.add("South", pnlButton);
  this.add("East", pnlBlank2);
  this.add("West", pnlBlank3);

  //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  //lblMain.setOpaque(true);
  lblMain.setBackground(Color.gray);
  lblMain.setForeground(Color.blue);
  lblMain.setFont(new Font("Serif",Font.BOLD,45));
  lblBlank.setBackground(Color.gray);
  lblBlank.setForeground(Color.blue);
  lblBlank.setFont(new Font("Serif",Font.BOLD,45));
  lblAmount.setBackground(Color.gray);
  lblAmount.setForeground(Color.black);
  lblAmount.setFont(new Font("Serif",Font.BOLD,20));
  lblInterest.setBackground(Color.gray);
  lblInterest.setForeground(Color.black);
  lblInterest.setFont(new Font("Serif",Font.BOLD,20));
  lblPeriod.setBackground(Color.gray);
  lblPeriod.setForeground(Color.black);
  lblPeriod.setFont(new Font("Serif",Font.BOLD,20));
  lblYears.setBackground(Color.gray);
  lblYears.setForeground(Color.black);
  lblYears.setFont(new Font("Serif",Font.BOLD,20));
  lblTotal.setBackground(Color.gray);
  lblTotal.setForeground(Color.black);
  lblTotal.setFont(new Font("Serif",Font.BOLD,20));
  txtAmount.setBackground(Color.white);
  txtAmount.setForeground(Color.black);
  txtAmount.setFont(new Font("Serif",Font.BOLD,24));
  txtInterest.setBackground(Color.white);
  txtInterest.setForeground(Color.black);
  txtInterest.setFont(new Font("Serif",Font.BOLD,24));
  txtPeriod.setBackground(Color.white);
  txtPeriod.setForeground(Color.black);
  txtPeriod.setFont(new Font("Serif",Font.BOLD,24));
  txtYears.setBackground(Color.white);
  txtYears.setForeground(Color.black);
  txtYears.setFont(new Font("Serif",Font.BOLD,24));
  txtTotal.setBackground(Color.blue);
  txtTotal.setForeground(Color.white);
  txtTotal.setFont(new Font("Serif",Font.BOLD,27));
  lblTotal.setVisible(false);
  txtTotal.setVisible(false);
  txtTotal.setEditable(false);


  pnlHead.add(lblMain);
  pnlHead.add(lblBlank);
  pnlArea.add(lblAmount);
  pnlArea.add(txtAmount);
  pnlArea.add(lblInterest);
  pnlArea.add(txtInterest);
  pnlArea.add(lblPeriod);
  pnlArea.add(txtPeriod);
  pnlArea.add(lblYears);
  pnlArea.add(txtYears);
  pnlArea.add(lblTotal);
  pnlArea.add(txtTotal);
  pnlBlank2.add(lblBlank);
  pnlBlank3.add(lblBlank2);

  btnCalculate.setFont(new Font("Serif", Font.BOLD, 30));
  pnlButton.add(btnCalculate);
  btnReset.setFont(new Font("Serif", Font.BOLD, 30));
  pnlButton.add(btnReset);
  btnMain.setFont(new Font("Serif", Font.BOLD, 30));
  pnlButton.add(btnMain);

  btnCalculate.addActionListener(this);
  btnReset.addActionListener(this);
  btnMain.addActionListener(this);


}

public void CalculateTotal() throws IOException
{ double amount=0, interest=0, total, r, x, y, q;
  int nt, period, years;
  String outAmount, outInterest, outPeriod, outYears, outTotal;

  amount   = Double.parseDouble(txtAmount.getText());
  interest = Double.parseDouble(txtInterest.getText());
  period   = Integer.parseInt(txtPeriod.getText());
  years	= Integer.parseInt(txtYears.getText());

  r = interest / (100*period);
  nt = period*years;
  x = 1+r;
  y = Math.pow(x,nt);
  total = amount*y;

  outAmount   = String.format("%10.2f", amount);
  outInterest = String.format("%10.2f", interest);
  outPeriod   = String.format("%10d", period);
  outYears	= String.format("%10d", years);
  outTotal	= String.format("%10.2f", total);

  lblTotal.setVisible(true);
  txtTotal.setVisible(true);

  txtAmount.setText("$" + outAmount);
  txtInterest.setText(outInterest + "%");
  txtPeriod.setText(outPeriod + " per year");
  txtYears.setText(outYears + " years");
  txtTotal.setText("$" + outTotal);

}

public void Reset()
{ txtAmount.setText("");
  txtInterest.setText("");
  txtPeriod.setText("");
  txtYears.setText("");
  txtTotal.setText("");
  txtTotal.setVisible(false);
  lblTotal.setVisible(false);
  btnCalculate.setEnabled(true);
}

public void actionPerformed(ActionEvent Ae)
{ String cmd = Ae.getActionCommand();
  if (Ae.getSource() instanceof JButton)
  { if (cmd=="Calculate")
	 { try
	   { CalculateTotal(); }
	catch (IOException Error)
	   { String Err = "IO Exception error...";
		 System.err.print(Err + Error.getMessage());
		 txtTotal.setText(Err + Error.getMessage());
	   }
	 btnCalculate.setEnabled(false);
   }
	else if (cmd=="Reset") Reset();
	else P09.ShowFrame1();
  }
} //actionperformed

private void ExitProgram()
{ P09.Frame1.setVisible(false);
  dispose();
  System.exit(99);
} //exitprogram

class WinListener extends WindowAdapter
   { public void windowClosing(WindowEvent We)
	 { ExitProgram(); }
   } //winlistener
} //class
/*----------------------- End of File:  P09Frame2.java --------------------*/


/*--------------------------------------------------------------------------*
* File:		P09Frame3.java												  *
* Author:	Jason French													*
*--------------------------------------------------------------------------*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class P09Frame3 extends Frame implements ActionListener
{ private JPanel pnlHead	 = new JPanel(new GridLayout(2,1));
  private JPanel pnlButton   = new JPanel(new GridLayout(1,3));
  private JPanel pnlArea	 = new JPanel(new GridLayout(6,2,40,40));
  private JPanel pnlBlank2   = new JPanel(new GridLayout(1,1));
  private JPanel pnlBlank3   = new JPanel(new GridLayout(1,1));
  private JLabel lblMain	 = new JLabel("Amortization Calculator", JLabel.CENTER);
  private JLabel lblBlank	= new JLabel("	");
  private JLabel lblBlank2	= new JLabel("	 ");
  private JLabel lblAmount   = new JLabel("  Enter Loan Amount:");
  private JLabel lblInterest = new JLabel("  Enter Annual Interest Rate:");
  private JLabel lblPeriod   = new JLabel("  Enter Number of Payments per Year:");
  private JLabel lblYears	= new JLabel("  Enter Duration of Loan in Years:");
  private JLabel lblTotal	= new JLabel("  Installment Amount:");
  private JLabel lblTotalI   = new JLabel("  Total Interest:");
  private JTextField txtAmount  = new JTextField("");
  private JTextField txtInterest= new JTextField("");
  private JTextField txtPeriod  = new JTextField("");
  private JTextField txtYears   = new JTextField("");
  private JTextField txtTotal   = new JTextField("");
  private JTextField txtTotalI  = new JTextField("");
  private JButton btnCalculate= new JButton("Calculate");
  private JButton btnReset	= new JButton("Reset");
  private JButton btnMain	 = new JButton("Main");

public P09Frame3(String Title)
{ super(Title);

  Toolkit tk = Toolkit.getDefaultToolkit();
  Dimension screenSize = tk.getScreenSize();
  int screenHeight = screenSize.height;
  int screenWidth = screenSize.width;
  int centerHeight, centerWidth;

  centerHeight= screenHeight-600;
  centerWidth = screenWidth-800;

  this.setSize(800,600);
  this.setLocation(centerWidth/2, centerHeight/2);
  this.setBackground(Color.gray);
  this.setForeground(Color.black);
  this.setLayout(new BorderLayout());
  this.add("North", pnlHead);
  this.add("Center", pnlArea);
  this.add("South", pnlButton);
  this.add("East", pnlBlank2);
  this.add("West", pnlBlank3);

  //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  //lblMain.setOpaque(true);
  lblMain.setBackground(Color.gray);
  lblMain.setForeground(Color.blue);
  lblMain.setFont(new Font("Serif",Font.BOLD,45));
  lblBlank.setBackground(Color.gray);
  lblBlank.setForeground(Color.blue);
  lblBlank.setFont(new Font("Serif",Font.BOLD,45));
  lblAmount.setBackground(Color.gray);
  lblAmount.setForeground(Color.black);
  lblAmount.setFont(new Font("Serif",Font.BOLD,20));
  lblInterest.setBackground(Color.gray);
  lblInterest.setForeground(Color.black);
  lblInterest.setFont(new Font("Serif",Font.BOLD,20));
  lblPeriod.setBackground(Color.gray);
  lblPeriod.setForeground(Color.black);
  lblPeriod.setFont(new Font("Serif",Font.BOLD,20));
  lblYears.setBackground(Color.gray);
  lblYears.setForeground(Color.black);
  lblYears.setFont(new Font("Serif",Font.BOLD,20));
  lblTotal.setBackground(Color.gray);
  lblTotal.setForeground(Color.black);
  lblTotal.setFont(new Font("Serif",Font.BOLD,20));
  lblTotalI.setBackground(Color.gray);
  lblTotalI.setForeground(Color.black);
  lblTotalI.setFont(new Font("Serif",Font.BOLD,20));
  txtAmount.setBackground(Color.white);
  txtAmount.setForeground(Color.black);
  txtAmount.setFont(new Font("Serif",Font.BOLD,24));
  txtInterest.setBackground(Color.white);
  txtInterest.setForeground(Color.black);
  txtInterest.setFont(new Font("Serif",Font.BOLD,24));
  txtPeriod.setBackground(Color.white);
  txtPeriod.setForeground(Color.black);
  txtPeriod.setFont(new Font("Serif",Font.BOLD,24));
  txtYears.setBackground(Color.white);
  txtYears.setForeground(Color.black);
  txtYears.setFont(new Font("Serif",Font.BOLD,24));
  txtTotal.setBackground(Color.blue);
  txtTotal.setForeground(Color.white);
  txtTotal.setFont(new Font("Serif",Font.BOLD,27));
  txtTotalI.setBackground(Color.blue);
  txtTotalI.setForeground(Color.white);
  txtTotalI.setFont(new Font("Serif",Font.BOLD,27));
  lblTotal.setVisible(false);
  txtTotal.setVisible(false);
  txtTotal.setEditable(false);
  lblTotalI.setVisible(false);
  txtTotalI.setVisible(false);
  txtTotalI.setEditable(false);


  pnlHead.add(lblMain);
  pnlHead.add(lblBlank);
  pnlArea.add(lblAmount);
  pnlArea.add(txtAmount);
  pnlArea.add(lblInterest);
  pnlArea.add(txtInterest);
  pnlArea.add(lblPeriod);
  pnlArea.add(txtPeriod);
  pnlArea.add(lblYears);
  pnlArea.add(txtYears);
  pnlArea.add(lblTotal);
  pnlArea.add(txtTotal);
  pnlArea.add(lblTotalI);
  pnlArea.add(txtTotalI);
  pnlBlank2.add(lblBlank);
  pnlBlank3.add(lblBlank2);

  btnCalculate.setFont(new Font("Serif", Font.BOLD, 30));
  pnlButton.add(btnCalculate);
  btnReset.setFont(new Font("Serif", Font.BOLD, 30));
  pnlButton.add(btnReset);
  btnMain.setFont(new Font("Serif", Font.BOLD, 30));
  pnlButton.add(btnMain);

  btnCalculate.addActionListener(this);
  btnReset.addActionListener(this);
  btnMain.addActionListener(this);


}

public void CalculateTotal() throws IOException
{ double amount=0, interest=0, total, totalI, r, x, y, z, a, b, c;
  int nt, period, years;
  String outAmount, outInterest, outPeriod, outYears, outTotal, outTotalI, cogs;

  amount   = Double.parseDouble(txtAmount.getText());
  interest = Double.parseDouble(txtInterest.getText());
  period   = Integer.parseInt(txtPeriod.getText());
  years	= Integer.parseInt(txtYears.getText());

  r = interest / (100*period);
  nt = period*years;
  x = amount*r;
  a = 1+r;
  y = Math.pow(a,nt);
  z = x*y;
  c = y-1;
  b = z/c;
  total = Math.floor(b*100+0.05)/100;
  totalI = total*period*years-amount;

  if (period==12) cogs=" per month";
  else if (period==4) cogs=" per quarter";
  else if (period==2) cogs=" per half";
  else if (period==1) cogs=" per year";
  else cogs=" per period";

  outAmount   = String.format("%10.2f", amount);
  outInterest = String.format("%10.2f", interest);
  outPeriod   = String.format("%10d", period);
  outYears	= String.format("%10d", years);
  outTotal	= String.format("%10.2f", total);
  outTotalI   = String.format("%10.2f", totalI);

  lblTotal.setVisible(true);
  txtTotal.setVisible(true);
  lblTotalI.setVisible(true);
  txtTotalI.setVisible(true);


  txtAmount.setText("$" + outAmount);
  txtInterest.setText(outInterest + "%");
  txtPeriod.setText(outPeriod + " per year");
  txtYears.setText(outYears + " years");
  txtTotal.setText("$" + outTotal + cogs);
  txtTotalI.setText("$" + outTotalI);

}

public void Reset()
{ txtAmount.setText("");
  txtInterest.setText("");
  txtPeriod.setText("");
  txtYears.setText("");
  txtTotal.setText("");
  txtTotalI.setText("");
  txtTotal.setVisible(false);
  lblTotal.setVisible(false);
  txtTotalI.setVisible(false);
  lblTotalI.setVisible(false);

  btnCalculate.setEnabled(true);
}


public void actionPerformed(ActionEvent Ae)
{ String cmd = Ae.getActionCommand();
  if (Ae.getSource() instanceof JButton)
  { if (cmd=="Calculate")
	 { try
	   { CalculateTotal(); }
	catch (IOException Error)
	   { String Err = "IO Exception error...";
		 System.err.print(Err + Error.getMessage());
		 txtTotal.setText(Err + Error.getMessage());
	   }
	 btnCalculate.setEnabled(false);
   }
	else if (cmd=="Reset") Reset();
	else P09.ShowFrame1();
  }
} //actionperformed

private void ExitProgram()
{ P09.Frame1.setVisible(false);
  dispose();
  System.exit(99);
} //exitprogram

class WinListener extends WindowAdapter
   { public void windowClosing(WindowEvent We)
	 { ExitProgram(); }
   } //winlistener
} //class
/*----------------------- End of File:  P09Frame3.java --------------------*/


/*--------------------------------------------------------------------------*
* File:		P09Frame4.java												  *
* Author:	Jason French													*
*--------------------------------------------------------------------------*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class P09Frame4 extends Frame implements ActionListener
{ private JPanel pnlHead	 = new JPanel(new GridLayout(2,1));
  private JPanel pnlButton   = new JPanel(new GridLayout(1,3));
  private JPanel pnlArea	 = new JPanel(new GridLayout(7,2,40,40));
  private JPanel pnlBlank2   = new JPanel(new GridLayout(1,1));
  private JPanel pnlBlank3   = new JPanel(new GridLayout(1,1));
  private JLabel lblMain	 = new JLabel("Annuity Calculator", JLabel.CENTER);
  private JLabel lblBlank	= new JLabel("	");
  private JLabel lblBlank2	= new JLabel("	 ");
  private JLabel lblAmount   = new JLabel("  Enter Amount Invested:");
  private JLabel lblInterest = new JLabel("  Enter Annual Interest Rate:");
  private JLabel lblPeriod   = new JLabel("  Enter Investment Periods per Year");
  private JLabel lblCompound = new JLabel("  Enter Compounding Periods per Year:");
  private JLabel lblYears	= new JLabel("  Enter Annuity Term:");
  private JLabel lblTotal	= new JLabel("  Total Annuity Amount:");
  private JLabel lblTotalI   = new JLabel("  Total Amount Invested:");
  private JTextField txtAmount  = new JTextField("");
  private JTextField txtInterest= new JTextField("");
  private JTextField txtPeriod  = new JTextField("");
  private JTextField txtCompound= new JTextField("");
  private JTextField txtYears   = new JTextField("");
  private JTextField txtTotal   = new JTextField("");
  private JTextField txtTotalI  = new JTextField("");
  private JButton btnCalculate= new JButton("Calculate");
  private JButton btnReset	= new JButton("Reset");
  private JButton btnMain	 = new JButton("Main");

public P09Frame4(String Title)
{ super(Title);

  Toolkit tk = Toolkit.getDefaultToolkit();
  Dimension screenSize = tk.getScreenSize();
  int screenHeight = screenSize.height;
  int screenWidth = screenSize.width;
  int centerHeight, centerWidth;

  centerHeight= screenHeight-600;
  centerWidth = screenWidth-800;

  this.setSize(800,600);
  this.setLocation(centerWidth/2, centerHeight/2);
  this.setBackground(Color.gray);
  this.setForeground(Color.black);
  this.setLayout(new BorderLayout());
  this.add("North", pnlHead);
  this.add("Center", pnlArea);
  this.add("South", pnlButton);
  this.add("East", pnlBlank2);
  this.add("West", pnlBlank3);

  //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  //lblMain.setOpaque(true);
  lblMain.setBackground(Color.gray);
  lblMain.setForeground(Color.blue);
  lblMain.setFont(new Font("Serif",Font.BOLD,45));
  lblBlank.setBackground(Color.gray);
  lblBlank.setForeground(Color.blue);
  lblBlank.setFont(new Font("Serif",Font.BOLD,45));
  lblAmount.setBackground(Color.gray);
  lblAmount.setForeground(Color.black);
  lblAmount.setFont(new Font("Serif",Font.BOLD,20));
  lblInterest.setBackground(Color.gray);
  lblInterest.setForeground(Color.black);
  lblInterest.setFont(new Font("Serif",Font.BOLD,20));
  lblPeriod.setBackground(Color.gray);
  lblPeriod.setForeground(Color.black);
  lblPeriod.setFont(new Font("Serif",Font.BOLD,20));
  lblCompound.setBackground(Color.gray);
  lblCompound.setForeground(Color.black);
  lblCompound.setFont(new Font("Serif",Font.BOLD,20));
  lblYears.setBackground(Color.gray);
  lblYears.setForeground(Color.black);
  lblYears.setFont(new Font("Serif",Font.BOLD,20));
  lblTotal.setBackground(Color.gray);
  lblTotal.setForeground(Color.black);
  lblTotal.setFont(new Font("Serif",Font.BOLD,20));
  lblTotalI.setBackground(Color.gray);
  lblTotalI.setForeground(Color.black);
  lblTotalI.setFont(new Font("Serif",Font.BOLD,20));
  txtAmount.setBackground(Color.white);
  txtAmount.setForeground(Color.black);
  txtAmount.setFont(new Font("Serif",Font.BOLD,20));
  txtInterest.setBackground(Color.white);
  txtInterest.setForeground(Color.black);
  txtInterest.setFont(new Font("Serif",Font.BOLD,20));
  txtPeriod.setBackground(Color.white);
  txtPeriod.setForeground(Color.black);
  txtPeriod.setFont(new Font("Serif",Font.BOLD,20));
  txtCompound.setBackground(Color.white);
  txtCompound.setForeground(Color.black);
  txtCompound.setFont(new Font("Serif",Font.BOLD,20));
  txtYears.setBackground(Color.white);
  txtYears.setForeground(Color.black);
  txtYears.setFont(new Font("Serif",Font.BOLD,20));
  txtTotal.setBackground(Color.blue);
  txtTotal.setForeground(Color.white);
  txtTotal.setFont(new Font("Serif",Font.BOLD,24));
  lblTotal.setVisible(false);
  txtTotal.setVisible(false);
  txtTotal.setEditable(false);
  txtTotalI.setBackground(Color.blue);
  txtTotalI.setForeground(Color.white);
  txtTotalI.setFont(new Font("Serif",Font.BOLD,24));
  lblTotal.setVisible(false);
  txtTotal.setVisible(false);
  txtTotal.setEditable(false);
  lblTotalI.setVisible(false);
  txtTotalI.setVisible(false);
  txtTotalI.setEditable(false);


  pnlHead.add(lblMain);
  pnlHead.add(lblBlank);
  pnlArea.add(lblAmount);
  pnlArea.add(txtAmount);
  pnlArea.add(lblInterest);
  pnlArea.add(txtInterest);
  pnlArea.add(lblPeriod);
  pnlArea.add(txtPeriod);
  pnlArea.add(lblCompound);
  pnlArea.add(txtCompound);
  pnlArea.add(lblYears);
  pnlArea.add(txtYears);
  pnlArea.add(lblTotalI);
  pnlArea.add(txtTotalI);
  pnlArea.add(lblTotal);
  pnlArea.add(txtTotal);
  pnlBlank2.add(lblBlank);
  pnlBlank3.add(lblBlank2);

  btnCalculate.setFont(new Font("Serif", Font.BOLD, 30));
  pnlButton.add(btnCalculate);
  btnReset.setFont(new Font("Serif", Font.BOLD, 30));
  pnlButton.add(btnReset);
  btnMain.setFont(new Font("Serif", Font.BOLD, 30));
  pnlButton.add(btnMain);

  btnCalculate.addActionListener(this);
  btnReset.addActionListener(this);
  btnMain.addActionListener(this);


}

public void CalculateTotal() throws IOException
{ double amount=0, interest=0, total, totalI, r, a, b, c, x, y, z;
  int nt, period, years, compound;
  String outAmount, outInterest, outPeriod, outCompound, outYears, outTotal, outTotalI, cogs;

  amount   = Double.parseDouble(txtAmount.getText());
  interest = Double.parseDouble(txtInterest.getText());
  period   = Integer.parseInt(txtPeriod.getText());
  compound = Integer.parseInt(txtCompound.getText());
  years	= Integer.parseInt(txtYears.getText());

  r = interest / (100*compound);
  nt = years*compound;
  a = compound/period; // n/m
  //y = Math.floor(a);
  b = 1+r;
  c = Math.pow(b,a); // 1+r^n/m
  x = Math.pow(b,nt); // 1+r^nt
  total = amount*c*(x-1) / (c-1);
  totalI = amount*period*years;

  if (period==12) cogs=" per month";
  else if (period==4) cogs=" per quarter";
  else if (period==2) cogs=" per half";
  else if (period==1) cogs=" per year";
  else cogs=" per period";

  outAmount   = String.format("%10.2f", amount);
  outInterest = String.format("%10.2f", interest);
  outPeriod   = String.format("%10d", period);
  outCompound = String.format("%10d", compound);
  outYears	= String.format("%10d", years);
  outTotal	= String.format("%10.2f", total);
  outTotalI   = String.format("%10.2f", totalI);

  lblTotal.setVisible(true);
  txtTotal.setVisible(true);
  lblTotalI.setVisible(true);
  txtTotalI.setVisible(true);

  txtAmount.setText("$" + outAmount + cogs);
  txtInterest.setText(outInterest + "%");
  txtPeriod.setText(outPeriod + " per year");
  txtCompound.setText(outCompound + " per year");
  txtYears.setText(outYears + " years");
  txtTotalI.setText("$" + outTotalI);
  txtTotal.setText("$" + outTotal);

}

public void Reset()
{ txtAmount.setText("");
  txtInterest.setText("");
  txtPeriod.setText("");
  txtCompound.setText("");
  txtYears.setText("");
  txtTotal.setText("");
  txtTotalI.setText("");
  txtTotal.setVisible(false);
  lblTotal.setVisible(false);
  txtTotalI.setVisible(false);
  lblTotalI.setVisible(false);

  btnCalculate.setEnabled(true);
}

public void actionPerformed(ActionEvent Ae)
{ String cmd = Ae.getActionCommand();
  if (Ae.getSource() instanceof JButton)
  { if (cmd=="Calculate")
	 { try
	   { CalculateTotal(); }
	catch (IOException Error)
	   { String Err = "IO Exception error...";
		 System.err.print(Err + Error.getMessage());
		 txtTotal.setText(Err + Error.getMessage());
	   }
	 btnCalculate.setEnabled(false);
   }
	else if (cmd=="Reset") Reset();
	else P09.ShowFrame1();
  }
} //actionperformed

private void ExitProgram()
{ P09.Frame1.setVisible(false);
  dispose();
  System.exit(99);
} //exitprogram

class WinListener extends WindowAdapter
   { public void windowClosing(WindowEvent We)
	 { ExitProgram(); }
   } //winlistener
} //class
/*----------------------- End of File:  P09Frame4.java --------------------*/


Is This A Good Question/Topic? 0
  • +

Replies To: Creating an applet out of an application with multiple frames

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Creating an applet out of an application with multiple frames

Posted 02 May 2009 - 10:28 PM

You can always create an Applet of 1 pixel by 1 pixel and call the plublic static void main() method of your application from it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1