Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Chapter 32: Financial Applets and Servlets 941


double principal; // original principal
double rateOfRet; // rate of return
double numYears; // length of investment in years
int compPerYear; // number of compoundings per year


NumberFormat nf;


public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable () {
public void run() {
makeGUI(); // initialize the GUI
}
});
} catch(Exception exc) {
System.out.println("Can't create because of "+ exc);
}
}


// Set up and initialize the GUI.
private void makeGUI() {


// Use a grid bag layout.
GridBagLayout gbag = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbag);

JLabel heading = new
JLabel("Future Value of an Investment");

JLabel amountLab = new JLabel("Principal ");
JLabel periodLab = new JLabel("Years ");
JLabel rateLab = new JLabel("Rate of Return ");
JLabel futvalLab =
new JLabel("Future Value of Investment ");
JLabel compLab =
new JLabel("Compounding Periods per Year ");

amountText = new JTextField(10);
periodText = new JTextField(10);
futvalText = new JTextField(10);
rateText = new JTextField(10);
compText = new JTextField(10);

// Future value field for display only.
futvalText.setEditable(false);

doIt = new JButton("Compute");

// Define the grid bag.
gbc.weighty = 1.0; // use a row weight of 1
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.NORTH;
gbag.setConstraints(heading, gbc);
Free download pdf