home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / SAMPLES.BIN / LoanCalcFrame.java < prev    next >
Text File  |  1997-10-27  |  7KB  |  234 lines

  1. /*
  2.     A basic extension of the java.awt.Frame class
  3.  */
  4.  
  5. import java.awt.*;
  6.  
  7. import symantec.itools.awt.KeyPressManagerPanel;
  8. public class LoanCalcFrame extends Frame {
  9.  
  10.     void Frame1_WindowClosing(java.awt.event.WindowEvent event)
  11.     {
  12.         hide();         // hide the Frame
  13.     }
  14.  
  15.     public LoanCalcFrame()
  16.     {
  17.         // This code is automatically generated by Visual Cafe when you add
  18.         // components to the visual environment. It instantiates and initializes
  19.         // the components. To modify the code, only use code syntax that matches
  20.         // what Visual Cafe can generate, or Visual Cafe may be unable to back
  21.         // parse your Java file into its visual environment.
  22.         //{{INIT_CONTROLS
  23.         GridBagLayout gridBagLayout;
  24.         gridBagLayout = new GridBagLayout();
  25.         setLayout(gridBagLayout);
  26.         setVisible(false);
  27.         setSize(insets().left + insets().right + 419,insets().top + insets().bottom + 165);
  28.         PrincipleLabel = new java.awt.Label("Principle =");
  29.         PrincipleLabel.setBounds(insets().left + 65,insets().top + 9,72,23);
  30.         GridBagConstraints gbc;
  31.         gbc = new GridBagConstraints();
  32.         gbc.gridx = 0;
  33.         gbc.gridy = 0;
  34.         gbc.weightx = 1.0;
  35.         gbc.weighty = 1.0;
  36.         gbc.fill = GridBagConstraints.NONE;
  37.         gbc.insets = new Insets(0,0,0,0);
  38.         ((GridBagLayout)getLayout()).setConstraints(PrincipleLabel, gbc);
  39.         add(PrincipleLabel);
  40.         InterestLabel = new java.awt.Label("Interest =");
  41.         InterestLabel.setBounds(insets().left + 69,insets().top + 50,65,23);
  42.         gbc = new GridBagConstraints();
  43.         gbc.gridx = 0;
  44.         gbc.gridy = 1;
  45.         gbc.weightx = 1.0;
  46.         gbc.weighty = 1.0;
  47.         gbc.fill = GridBagConstraints.NONE;
  48.         gbc.insets = new Insets(0,0,0,0);
  49.         ((GridBagLayout)getLayout()).setConstraints(InterestLabel, gbc);
  50.         add(InterestLabel);
  51.         LengthLabel = new java.awt.Label("Length of loan(years) =");
  52.         LengthLabel.setBounds(insets().left + 31,insets().top + 91,140,23);
  53.         gbc = new GridBagConstraints();
  54.         gbc.gridx = 0;
  55.         gbc.gridy = 2;
  56.         gbc.weightx = 1.0;
  57.         gbc.weighty = 1.0;
  58.         gbc.fill = GridBagConstraints.NONE;
  59.         gbc.insets = new Insets(0,0,0,0);
  60.         ((GridBagLayout)getLayout()).setConstraints(LengthLabel, gbc);
  61.         add(LengthLabel);
  62.         PaymentsLabel = new java.awt.Label("Monthly payments =");
  63.         PaymentsLabel.setBounds(insets().left + 40,insets().top + 132,122,23);
  64.         gbc = new GridBagConstraints();
  65.         gbc.gridx = 0;
  66.         gbc.gridy = 3;
  67.         gbc.weightx = 1.0;
  68.         gbc.weighty = 1.0;
  69.         gbc.fill = GridBagConstraints.NONE;
  70.         gbc.insets = new Insets(0,0,0,0);
  71.         ((GridBagLayout)getLayout()).setConstraints(PaymentsLabel, gbc);
  72.         add(PaymentsLabel);
  73.         PrincipleTextField = new java.awt.TextField(15);
  74.         PrincipleTextField.setBounds(insets().left + 234,insets().top + 9,24,23);
  75.         gbc = new GridBagConstraints();
  76.         gbc.gridx = 1;
  77.         gbc.gridy = 0;
  78.         gbc.weightx = 1.0;
  79.         gbc.weighty = 1.0;
  80.         gbc.fill = GridBagConstraints.NONE;
  81.         gbc.insets = new Insets(0,0,0,0);
  82.         ((GridBagLayout)getLayout()).setConstraints(PrincipleTextField, gbc);
  83.         add(PrincipleTextField);
  84.         InterestTextField = new java.awt.TextField(15);
  85.         InterestTextField.setBounds(insets().left + 234,insets().top + 50,24,23);
  86.         gbc = new GridBagConstraints();
  87.         gbc.gridx = 1;
  88.         gbc.gridy = 1;
  89.         gbc.weightx = 1.0;
  90.         gbc.weighty = 1.0;
  91.         gbc.fill = GridBagConstraints.NONE;
  92.         gbc.insets = new Insets(0,0,0,0);
  93.         ((GridBagLayout)getLayout()).setConstraints(InterestTextField, gbc);
  94.         add(InterestTextField);
  95.         LengthTextField = new java.awt.TextField(15);
  96.         LengthTextField.setBounds(insets().left + 234,insets().top + 91,24,23);
  97.         gbc = new GridBagConstraints();
  98.         gbc.gridx = 1;
  99.         gbc.gridy = 2;
  100.         gbc.weightx = 1.0;
  101.         gbc.weighty = 1.0;
  102.         gbc.fill = GridBagConstraints.NONE;
  103.         gbc.insets = new Insets(0,0,0,0);
  104.         ((GridBagLayout)getLayout()).setConstraints(LengthTextField, gbc);
  105.         add(LengthTextField);
  106.         PaymentsTextField = new java.awt.TextField(15);
  107.         PaymentsTextField.setEditable(false);
  108.         PaymentsTextField.setBounds(insets().left + 234,insets().top + 132,24,23);
  109.         PaymentsTextField.setBackground(new Color(12632256));
  110.         gbc = new GridBagConstraints();
  111.         gbc.gridx = 1;
  112.         gbc.gridy = 3;
  113.         gbc.weightx = 1.0;
  114.         gbc.weighty = 1.0;
  115.         gbc.fill = GridBagConstraints.NONE;
  116.         gbc.insets = new Insets(0,0,0,0);
  117.         ((GridBagLayout)getLayout()).setConstraints(PaymentsTextField, gbc);
  118.         add(PaymentsTextField);
  119.         CalcButton = new java.awt.Button();
  120.         CalcButton.setActionCommand("button");
  121.         CalcButton.setLabel("Calculate");
  122.         CalcButton.setBounds(insets().left + 321,insets().top + 70,66,23);
  123.         gbc = new GridBagConstraints();
  124.         gbc.gridx = 2;
  125.         gbc.gridy = 0;
  126.         gbc.gridheight = 4;
  127.         gbc.weightx = 1.0;
  128.         gbc.weighty = 1.0;
  129.         gbc.fill = GridBagConstraints.NONE;
  130.         gbc.insets = new Insets(0,0,0,0);
  131.         ((GridBagLayout)getLayout()).setConstraints(CalcButton, gbc);
  132.         add(CalcButton);
  133.         setTitle("Loan Calculation");
  134.         //}}
  135.  
  136.         //{{INIT_MENUS
  137.         //}}
  138.  
  139.         //{{REGISTER_LISTENERS
  140.         Window lWindow = new Window();
  141.         addWindowListener(lWindow);
  142.         Action lAction = new Action();
  143.         CalcButton.addActionListener(lAction);
  144.         //}}
  145.     }
  146.  
  147.     public LoanCalcFrame(String title)
  148.     {
  149.         this();
  150.         setTitle(title);
  151.     }
  152.  
  153.     public synchronized void show()
  154.     {
  155.         move(50, 50);
  156.         super.show();
  157.     }
  158.  
  159.     public void addNotify()
  160.     {
  161.         super.addNotify();
  162.  
  163.         if (fComponentsAdjusted)
  164.             return;
  165.  
  166.         // Adjust components according to the insets
  167.         setSize(insets().left + insets().right + getSize().width, insets().top + insets().bottom + getSize().height);
  168.         Component components[] = getComponents();
  169.         for (int i = 0; i < components.length; i++)
  170.         {
  171.             Point p = components[i].getLocation();
  172.             p.translate(insets().left, insets().top);
  173.             components[i].setLocation(p);
  174.         }
  175.         fComponentsAdjusted = true;
  176.     }
  177.  
  178.     boolean fComponentsAdjusted = false;
  179.  
  180.     //{{DECLARE_CONTROLS
  181.     java.awt.Label PrincipleLabel;
  182.     java.awt.Label InterestLabel;
  183.     java.awt.Label LengthLabel;
  184.     java.awt.Label PaymentsLabel;
  185.     java.awt.TextField PrincipleTextField;
  186.     java.awt.TextField InterestTextField;
  187.     java.awt.TextField LengthTextField;
  188.     java.awt.TextField PaymentsTextField;
  189.     java.awt.Button CalcButton;
  190.     //}}
  191.  
  192.     //{{DECLARE_MENUS
  193.     //}}
  194.  
  195.     class Window extends java.awt.event.WindowAdapter
  196.     {
  197.         public void windowClosing(java.awt.event.WindowEvent event)
  198.         {
  199.             Object object = event.getSource();
  200.             if (object == LoanCalcFrame.this)
  201.                 Frame1_WindowClosing(event);
  202.         }
  203.     }
  204.  
  205.     class Action implements java.awt.event.ActionListener
  206.     {
  207.         public void actionPerformed(java.awt.event.ActionEvent event)
  208.         {
  209.             Object object = event.getSource();
  210.             if (object == CalcButton)
  211.                 CalcButton_Action(event);
  212.         }
  213.     }
  214.  
  215.     void CalcButton_Action(java.awt.event.ActionEvent event)
  216.     {
  217.         double Principal, InterestRate, LengthLoan, MonthlyPay, IntDec, NumMonths;
  218.  
  219.         try {
  220.             Principal    = ( Double.valueOf(PrincipleTextField.getText()   ) ).doubleValue();
  221.             InterestRate = ( Double.valueOf(InterestTextField.getText()) ).doubleValue();
  222.             LengthLoan   = ( Double.valueOf(LengthTextField.getText()  ) ).doubleValue();
  223.         } catch (NumberFormatException e) {return;}
  224.  
  225.         if (Principal < 0 || InterestRate < 0 || LengthLoan < 0) return;
  226.  
  227.         IntDec     = InterestRate / (12.0 * 100.0);
  228.         NumMonths  = LengthLoan * 12.0;
  229.         MonthlyPay = Principal * (IntDec / (1.0 - Math.pow((1.0 + IntDec), -NumMonths)));
  230.  
  231.         PaymentsTextField.setText(String.valueOf(MonthlyPay));
  232.     }
  233. }
  234.