159 lines
4.7 KiB
Java
159 lines
4.7 KiB
Java
|
|
import java.awt.*;
|
|
import java.awt.event.*;
|
|
import javax.swing.*;
|
|
import java.util.*;
|
|
|
|
/*
|
|
* The main work of the application is done in this JPanel-extending class.
|
|
* This allows the java class to be run as a stand-alone application or
|
|
* as an applet within a web browser.
|
|
*/
|
|
class RetirementCalculatorPanel extends JPanel implements ActionListener
|
|
{
|
|
private RetirementCalculatorModel myModel;
|
|
|
|
public RetirementCalculatorPanel()
|
|
{
|
|
super(new BorderLayout());
|
|
JPanel inner = new JPanel(new GridBagLayout());
|
|
// inner.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
|
|
JComponent field;
|
|
GridBagConstraints c = new GridBagConstraints();
|
|
c.fill = GridBagConstraints.HORIZONTAL;
|
|
c.ipadx = 2;
|
|
c.gridwidth = 1;
|
|
c.gridheight = 1;
|
|
c.gridx = 0;
|
|
c.gridy = 0;
|
|
|
|
c.weightx = 0.0;
|
|
inner.add(new JLabel("How much annual income will you want in retirement?", SwingConstants.RIGHT), c);
|
|
c.gridx++;
|
|
c.weightx = 1.0;
|
|
field = new JTextField();
|
|
field.setMinimumSize(new Dimension(1, 1));
|
|
inner.add(field, c);
|
|
c.gridx--;
|
|
c.gridy++;
|
|
|
|
c.weightx = 0.0;
|
|
inner.add(new JLabel("What is your current annual income?", SwingConstants.RIGHT), c);
|
|
c.gridx++;
|
|
c.weightx = 1.0;
|
|
field = new JTextField();
|
|
inner.add(field, c);
|
|
c.gridx--;
|
|
c.gridy++;
|
|
|
|
c.gridwidth = 2;
|
|
inner.add(new JLabel("What is your expected annual income after retirement -"), c);
|
|
c.gridwidth = 1;
|
|
c.gridy++;
|
|
|
|
c.weightx = 0.0;
|
|
inner.add(new JLabel(" - from an employer pension plan?", SwingConstants.RIGHT), c);
|
|
c.gridx++;
|
|
c.weightx = 1.0;
|
|
field = new JTextField();
|
|
inner.add(field, c);
|
|
c.gridx--;
|
|
c.gridy++;
|
|
|
|
c.weightx = 0.0;
|
|
inner.add(new JLabel(" - from a part-time job?", SwingConstants.RIGHT), c);
|
|
c.gridx++;
|
|
c.weightx = 1.0;
|
|
field = new JTextField();
|
|
inner.add(field, c);
|
|
c.gridx--;
|
|
c.gridy++;
|
|
|
|
c.weightx = 0.0;
|
|
inner.add(new JLabel(" - other?", SwingConstants.RIGHT), c);
|
|
c.gridx++;
|
|
c.weightx = 1.0;
|
|
field = new JTextField();
|
|
inner.add(field, c);
|
|
c.gridx--;
|
|
c.gridy++;
|
|
|
|
c.weightx = 0.0;
|
|
inner.add(new JLabel("At what age do you expect to retire?", SwingConstants.RIGHT), c);
|
|
c.gridx++;
|
|
c.weightx = 1.0;
|
|
field = new JSpinner(new SpinnerNumberModel(65, 0, 150, 1));
|
|
inner.add(field, c);
|
|
c.gridx--;
|
|
c.gridy++;
|
|
|
|
JPanel p = new JPanel(new BorderLayout());
|
|
p.setAlignmentX(Component.RIGHT_ALIGNMENT);
|
|
p.add(new JLabel("What is your life expectancy?", SwingConstants.RIGHT), BorderLayout.CENTER);
|
|
String[] choices = {
|
|
"82 (Male; 50th percentile)",
|
|
"86 (Female; 50th percentile)",
|
|
"89 (Male; 75th percentile)",
|
|
"92 (Female; 75th percentile)",
|
|
"94 (Male; 90th percentile)",
|
|
"97 (Female; 90th percentile)"
|
|
};
|
|
field = new JComboBox(choices);
|
|
p.add(field, BorderLayout.EAST);
|
|
c.gridwidth = 2;
|
|
inner.add(p, c);
|
|
c.gridwidth = 1;
|
|
c.gridy++;
|
|
|
|
JScrollPane scrollpane = new JScrollPane(inner,
|
|
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
|
|
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
|
|
add(scrollpane);
|
|
}
|
|
|
|
public void actionPerformed(ActionEvent e)
|
|
{
|
|
System.out.println("Event:");
|
|
System.out.println(e.getActionCommand());
|
|
System.out.println(e.paramString());
|
|
}
|
|
}
|
|
|
|
/*
|
|
* The "main" class of this file allows the app to be run as an applet
|
|
* but also provides a "main" method to run as an application.
|
|
*/
|
|
public class RetirementCalculator extends JApplet
|
|
{
|
|
final static private String PROG_NAME = "Josh's Retirement Calculator";
|
|
|
|
RetirementCalculatorPanel panel;
|
|
|
|
public void init()
|
|
{
|
|
setLayout(new BorderLayout());
|
|
panel = new RetirementCalculatorPanel();
|
|
add(panel, BorderLayout.CENTER);
|
|
}
|
|
|
|
public static void main(String[] args)
|
|
{
|
|
javax.swing.SwingUtilities.invokeLater(new Runnable() {
|
|
public void run()
|
|
{
|
|
JFrame frame = new JFrame(PROG_NAME);
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
RetirementCalculatorPanel panel =
|
|
new RetirementCalculatorPanel();
|
|
frame.getContentPane().add(panel);
|
|
|
|
frame.setPreferredSize(new Dimension(600, 450));
|
|
|
|
frame.pack();
|
|
frame.setVisible(true);
|
|
}
|
|
});
|
|
}
|
|
}
|