The calculator compiles and calculates the monthly payment but the radio buttons are not displaying the information in the fields. I also would like to show the the loan balance and the interest paid for each payment over the term of the loan and allow the user to clear the fields to enter the new information but the clear button doesn't work either. Can anyone help me please? Thanks!
Code:/* * Program: Mortgage Calculator with Graphical User Interface * Programs accepts user input in 3 text fields: Mortgage Amount, Mortgage Term, and Interest Rate. * User can calculate the monthly payment amount from a selection of the following: * 7 year loan at 5.35% * 15 year loan at 5.50% * 30 year loan at 5.75% * The user can enter the new available chice for te loan at the top of calulator by getting the * cursor on the desired field and backspace and enter new data. the user can quit by clicking on EXIT */ import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import java.text.*; import java.util.*; import java.lang.*; import javax.swing.border.*; import javax.swing.JOptionPane; // Create MortgageCalculator class public class MortgageCalc3 extends JFrame implements ActionListener { // Radio Button Selection JPanel rowOne = new JPanel(); JRadioButton rbOne = new JRadioButton("7 Years/5.35%",true); JRadioButton rbTwo = new JRadioButton("15 Years/5.5%",true); JRadioButton rbThree = new JRadioButton("30 Years/5.75%",true); // Text Amount for Mortgage JPanel rowTwo = new JPanel(); JLabel mortgageLabel = new JLabel("Mortgage Amount $", JLabel.LEFT); JTextField mortgageText = new JTextField(10); JLabel termLabel = new JLabel("Mortgage Term (Years)", JLabel.LEFT); JTextField termText = new JTextField(3); JLabel intRateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT); JTextField intRateText = new JTextField(5); JLabel mPaymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT); JTextField mPaymentText = new JTextField(10); // Create buttons to calculate payment, amortizaton, and clear fields JPanel rowThree = new JPanel(); JButton calculateButton = new JButton("CALCULATE"); JButton clearButton = new JButton("CLEAR"); JButton exitButton = new JButton("EXIT"); // Create Label for Text Box JPanel rowFour = new JPanel(); JLabel paymentLabel = new JLabel ("Payment #"); JLabel balLabel = new JLabel (" Balance"); JLabel ytdPrincLabel = new JLabel (" Principal"); JLabel ytdIntLabel = new JLabel (" Interest"); // Display area JPanel rowFive = new JPanel(new FlowLayout()); JTextArea textArea = new JTextArea(10, 31); JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); public MortgageCalc3(){ super("KARIN'S CALC"); setSize(400, 485); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container pane = getContentPane(); JScrollPane scroll = new JScrollPane (textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); FlowLayout layout = new FlowLayout (FlowLayout.LEFT); pane.setLayout(layout); // Menu Selection for Term & Rate JMenuItem mOne = new JMenuItem ("7 Years/5.35%"); JMenuItem mTwo = new JMenuItem ("15 Years/5.5%"); JMenuItem mThree = new JMenuItem ("30 Years/5.75%"); JMenu menu = new JMenu ("SELECT TERM & RATE"); menu.add(mOne); menu.addSeparator(); menu.add(mTwo); menu.addSeparator(); menu.add(mThree); JMenuBar mBar = new JMenuBar(); mBar.add(menu); setJMenuBar(mBar); // Listeners mOne.addActionListener(this); mTwo.addActionListener(this); mThree.addActionListener(this); // Add to the screen FlowLayout layout1 = new FlowLayout(); rowOne.setLayout(layout1); // Add Radio Button Group ButtonGroup btnGrp = new ButtonGroup(); btnGrp.add(rbOne); btnGrp.add(rbTwo); btnGrp.add(rbThree); rowOne.add(rbOne); rowOne.add(rbTwo); rowOne.add(rbThree); pane.add(rowOne); // Create a border around the radio buttons Border titledRadioBorder = BorderFactory.createTitledBorder("Make your selections"); rowOne.setBorder(titledRadioBorder); // Listeners rbOne.addActionListener(this); rbTwo.addActionListener(this); rbThree.addActionListener(this); // Add Labels & Text Boxes GridLayout layout2 = new GridLayout (4, 1); rowTwo.setLayout(layout2); rowTwo.add(mortgageLabel); rowTwo.add(mortgageText); mortgageText.setText("200000"); rowTwo.add(termLabel); rowTwo.add(termText); termText.setText("30"); rowTwo.add(intRateLabel); rowTwo.add(intRateText); intRateText.setText("5.75"); rowTwo.add(mPaymentLabel); rowTwo.add(mPaymentText); mPaymentText.setEnabled(false); pane.add(rowTwo); // Add Buttons FlowLayout layout3 = new FlowLayout (FlowLayout.CENTER, 10, 10); rowThree.setLayout (layout3); rowThree.add(calculateButton); calculateButton.setBackground(Color.green); rowThree.add(clearButton); clearButton.setBackground(Color.white); rowThree.add(exitButton); exitButton.setBackground(Color.red); pane.add(rowThree); // Listeners calculateButton.addActionListener(this); clearButton.addActionListener(this); exitButton.addActionListener(this); // Add Text Box Label FlowLayout layout4 = new FlowLayout (FlowLayout.LEFT, 10, 10); rowFour.setLayout (layout4); rowFour.add(paymentLabel); rowFour.add(balLabel); rowFour.add(ytdPrincLabel); rowFour.add(ytdIntLabel); pane.add(rowFour); // Add Text Box FlowLayout layout5 = new FlowLayout (FlowLayout.CENTER, 10, 10); rowFive.setLayout(layout5); rowFive.add(scroll); pane.add(rowFive); setContentPane(pane); setVisible(true); } public static void main(String[] args)throws ParseException { DecimalFormat dollarAmount = new DecimalFormat("#,###.00"); DecimalFormat percentAmount = new DecimalFormat("##.##"); MortgageCalc3 frame = new MortgageCalc3(); } public void setResultValue() { double amount = Double.parseDouble(mortgageText.getText()); double term = Double.parseDouble(termText.getText()); double rate = Double.parseDouble(intRateText.getText()) / 100.; double result = (amount * ( rate/12))/(1-( Math.pow (1/( 1 +(rate/12)), (term*12)))); mPaymentText.setText(Double.toString(result)); } public void actionPerformed(ActionEvent event) { if (event.getSource() == calculateButton) { setResultValue();} if (event.getSource() == clearButton) { setResultValue();} Object command = event.getSource(); if (command == exitButton){ System.exit(0); } } }


Reply With Quote



Bookmarks