import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.border.Border; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeEvent; import java.text.*; public class newMortgageCalculator1 extends JFrame implements PropertyChangeListener, ActionListener { /** * */ private static final long serialVersionUID = 1L; public class String { } private static final Object[] String = null; //*input for each field private double amount = 200000; private double rate = .0575; //5.75% private int numPeriods = 30; //*Lets you know what the fields are private JLabel amountLabel; private JLabel rateLabel; private JLabel numPeriodsLabel; private JLabel paymentLabel; //*Strings for each input private static java.lang.String amountString = "Loan Amount: "; private static java.lang.String rateString = "APR (%): "; private static java.lang.String numPeriodsString = "Years: "; private static java.lang.String paymentString = "Monthly Payment: "; private static newMortgageCalculator1 newMortgageCalculator; //*input fields private JFormattedTextField amountField; private JFormattedTextField rateField; private JFormattedTextField numPeriodsField; private JFormattedTextField paymentField; //*Formats for the calculation private NumberFormat amountFormat; private NumberFormat percentFormat; private NumberFormat paymentFormat; //*Adds buttons to the field public JButton CalculateButton; public JButton ClearButton; public JButton ExitButton; private Container content; public JLabel temp; public newMortgageCalculator1() { super(); setSize(500, 400); setUpFormats(); double payment = calculatePayment(amount, rate, numPeriods); add(CalculateButton); add(ClearButton); add(ExitButton); CalculateButton = new JButton("Calculate"); ClearButton = new JButton("Clear"); ExitButton = new JButton("Exit"); CalculateButton.addActionListener(this); ClearButton.addActionListener(this); ExitButton.addActionListener(this); //*Makes the labels. amountLabel = new JLabel(amountString); rateLabel = new JLabel(rateString); numPeriodsLabel = new JLabel(numPeriodsString); paymentLabel = new JLabel(paymentString); //*Makes text fields. amountField = new JFormattedTextField(amountFormat); amountField.setValue(new Double(amount)); amountField.setColumns(10); amountField.addPropertyChangeListener("value", (PropertyChangeListener) this); rateField = new JFormattedTextField(percentFormat); rateField.setValue(new Double(rate)); rateField.setColumns(10); rateField.addPropertyChangeListener("value", this); numPeriodsField = new JFormattedTextField(); numPeriodsField.setValue(new Integer(numPeriods)); numPeriodsField.setColumns(10); numPeriodsField.addPropertyChangeListener("value", this); paymentField = new JFormattedTextField(paymentFormat); paymentField.setValue(new Double(payment)); paymentField.setColumns(10); paymentField.setEditable(false); paymentField.setForeground(Color.red); //*Information on the labels amountLabel.setLabelFor(amountField); rateLabel.setLabelFor(rateField); numPeriodsLabel.setLabelFor(numPeriodsField); paymentLabel.setLabelFor(paymentField); //*arranges labels in the panel. JPanel labelPane = new JPanel(new GridLayout(0,1)); labelPane.add(amountLabel); labelPane.add(rateLabel); labelPane.add(numPeriodsLabel); labelPane.add(paymentLabel); //*arranges the text labels in the panel . JPanel fieldPane = new JPanel(new GridLayout(0,1)); fieldPane.add(amountField); fieldPane.add(rateField); fieldPane.add(numPeriodsField); fieldPane.add(paymentField); //*Put the labels on left. //*text fields on right. setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); add(labelPane, BorderLayout.CENTER); add(fieldPane, BorderLayout.LINE_END); //* places content in frame. content.add(CalculateButton); content.add(ClearButton); content.add(ExitButton); //* controls the calculation button actions CalculateHandler handlerC = new CalculateHandler(); CalculateButton.addActionListener((ActionListener) handlerC); //*controls the clear button actions ClearHandler handlerD = new ClearHandler(); ClearButton.addActionListener((ActionListener) handlerD); } private double calculatePayment(double amount2, double rate2, int numPeriods2) { // TODO Auto-generated method stub return 0; } private void add(JPanel labelPane, java.lang.String center) { // TODO Auto-generated method stub } private void setUpFormats() { // TODO Auto-generated method stub } private void setBorder(Border createEmptyBorder) { // TODO Auto-generated method stub } private void add(JButton calculate2) { // TODO Auto-generated method stub } //*Makes button perform public void actionPerformed(ActionEvent e){ if (e.getSource() == CalculateButton){ } else if (e.getSource() == ClearButton){ } else if (e.getSource() == ExitButton){ System.exit(0); } } /** Used when a field's "value" property changes. */ public void propertyChange(PropertyChangeEvent e) { Object source = e.getSource(); if (source == amountField) { amount = ((Number)amountField.getValue()).doubleValue(); } else if (source == rateField) { rate = ((Number)rateField.getValue()).doubleValue(); } else if (source == numPeriodsField) { numPeriods = ((Number)numPeriodsField.getValue()).intValue(); } intValue();} private void intValue() { // TODO Auto-generated method stub } int args() { return 0; } newMortgageCalculator1 (String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { }}); } //*Calculates the monthly payment based on the loan amount, //*Interest rate, and length of loan. public static double calculatePayment1(double principle, double annRate, int years){ double monthlyInt = annRate / 12; double monthlyPayment = (principle * monthlyInt) / (1 - Math.pow(1/ (1 + monthlyInt), years * 12)); //*Calculates 1 monthly payment times by 12 for yearly cost. return format(monthlyPayment, 2); } public static double format(double amount, int mortgage) { double temp = amount; temp = temp * Math.pow(10, mortgage); temp = Math.round(temp); temp = temp/Math.pow(10, mortgage); return temp; } @SuppressWarnings("unused") private void newMortgageCalculator11(){ amountFormat = NumberFormat.getNumberInstance(); percentFormat = NumberFormat.getNumberInstance(); percentFormat.setMinimumFractionDigits(3); paymentFormat = NumberFormat.getCurrencyInstance(); } public void mortcal(String[] args){ newMortgageCalculator = new newMortgageCalculator1(); newMortgageCalculator.setVisible1(true); } private void setVisible1(boolean b) { // TODO Auto-generated method stub } public void setVisible(boolean b) { // TODO Auto-generated method stub } public static Object[] getString() { return String; } }