Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Chapter 32: Financial Applets and Servlets 953


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("Maximum Regular Withdrawals");

JLabel orgPLab = new JLabel("Original Principal ");
JLabel periodLab = new JLabel("Years ");
JLabel rateLab = new JLabel("Rate of Return ");
JLabel numWDLab =
new JLabel("Number of Withdrawals per Year ");
JLabel maxWDLab = new JLabel("Maximum Withdrawal ");

maxWDText = new JTextField(10);
periodText = new JTextField(10);
orgPText = new JTextField(10);
rateText = new JTextField(10);
numWDText = new JTextField(10);

// Max withdrawal field for display only.
maxWDText.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);

// Anchor most components to the right.
gbc.anchor = GridBagConstraints.EAST;

gbc.gridwidth = GridBagConstraints.RELATIVE;
gbag.setConstraints(orgPLab, gbc);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbag.setConstraints(orgPText, gbc);
Free download pdf