I hope this is posted in the correct forum, I have a question about changing my GUI. This is for school so I do not want anyone to write the code for me, but pointing me to a beginners tutorial on Java GUI's that I can view offline would be great as I only have limited online access (have to drive 20 minutes to get it). I currently have the following code for a mortgage calculator:
package mortgageCalc; import java.awt.*; import java.text.NumberFormat; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class MortgageCalc extends JFrame { //This creates all the buttons and windows JLabel firstNumLabel = new JLabel("Mortgage Principal:"); JTextField firstNumField = new JTextField("0"); JLabel secondNumLabel = new JLabel("Mortgage Terms (in years):"); JTextField secondNumField = new JTextField("0"); JLabel thirdNumLabel = new JLabel("Annual Percentage Rate (APR)"); JTextField thirdNumField = new JTextField("0"); JLabel resultLabel = new JLabel("Payment Amount:"); JTextField resultField = new JTextField("0"); JButton calcButton = new JButton("Calculate Payment"); JButton closeButton = new JButton("Close"); JButton resetButton = new JButton("Reset Calculator"); public int pymtAmt = 0; public int intRate = 0; public int monMorTerms = 0; public MortgageCalc() { //this creates the GUI super("Mortgage Calculator"); setLocation(500, 200); this.setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new GridLayout(6, 2, 10, 10)); add(firstNumLabel); add(firstNumField); add(secondNumLabel); add(secondNumField); add(thirdNumLabel); add(thirdNumField); add(resultLabel); add(resultField); add(calcButton); add(closeButton); add(resetButton); resultField.setEditable(false); closeButton.addActionListener( //defines what the close button does new ActionListener() { public void actionPerformed(ActionEvent evt) { System.exit(0); } }); calcButton.addActionListener( //defines what the calculate button does new ActionListener() { public void actionPerformed(ActionEvent evt) { String firstNumText = firstNumField.getText(); String secondNumText = secondNumField.getText(); String thirdNumText = thirdNumField.getText(); double firstNum = Double.parseDouble(firstNumText); double secondNum = Double.parseDouble(secondNumText); double thirdNum = Double.parseDouble(thirdNumText); double intRate = thirdNum / 100 / 12; double monMorTerms = secondNum * 12; double result = (firstNum * intRate) / (1 - Math.pow(1 + intRate, -monMorTerms)); NumberFormat nf = NumberFormat.getCurrencyInstance(); resultField.setText("" + nf.format(result)); } }); resetButton.addActionListener(new ActionListener() { //defines what the reset button does public void actionPerformed(ActionEvent e) { firstNumField.setText("0"); secondNumField.setText("0"); thirdNumField.setText("0"); resultField.setText(""); } }); pack(); setVisible(true); } public static void main(String[] args) { //creates a new instance of the MortgageCalc class MortgageCalc app = new MortgageCalc(); } }
Now I have to alter this to have a drop down box with choices of loans such as 7 years at 5.35%, 15 years at 5.5%, or 30 years at 5.75%, then use an array for the mortgage data for the different loans. Display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop back and enter a new amount and make a new selection or quit.
So far when trying to alter my program, I thought the following code would display a similar window, removing some of the previous buttons and adding a checkbox, but so far it has errors so I am not sure if I am headed in the right direction or not.
package weekThree; import java.awt.*; import java.text.NumberFormat; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; //This class creates a Mortgage Calculator in it's own window with buttons and text fields used to calculate mortgage payments public class MortgageCalc extends JFrame { //This creates all the buttons and windows JLabel firstNumLabel = new JLabel("Mortgage Principal:"); JTextField firstNumField = new JTextField("0"); JButton calcButton = new JButton("Calculate Payment"); JButton closeButton = new JButton("Close"); JButton resetButton = new JButton("Reset Calculator"); public int pymtAmt = 0; public int intRate = 0; public int monMorTerms = 0; JComboBox mortgageChoices; public JPanel createComboBox() { mortgageChoices = new JComboBox(); mortgageChoices.addItem("7 years at 5.35%"); mortgageChoices.addItem("15 years at 5.5%"); mortgageChoices.addItem("30 years at 5.75%"); mortgageChoices.setEditable(false); JPanel panel = new JPanel(); return panel; } public MortgageCalc() { //this creates the GUI super("Mortgage Calculator"); setLocation(500, 200); this.setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(new GridLayout(6, 2, 10, 10)); add(firstNumLabel); add(firstNumField); add(mortgageChoices); add(calcButton); add(closeButton); add(resetButton); closeButton.addActionListener( //defines what the close button does new ActionListener() { public void actionPerformed(ActionEvent evt) { System.exit(0); } }); calcButton.addActionListener( //defines what the calculate button does new ActionListener() { public void actionPerformed(ActionEvent evt) { String firstNumText = firstNumField.getText(); double firstNum = Double.parseDouble(firstNumText); NumberFormat nf = NumberFormat.getCurrencyInstance(); } }); resetButton.addActionListener(new ActionListener() { //defines what the reset button does public void actionPerformed(ActionEvent e) { firstNumField.setText("0"); } }); pack(); setVisible(true); } public static void main(String[] args) { //creates a new instance of the MortgageCalc class MortgageCalc app = new MortgageCalc(); } }
I am not sure if I am creating the combobox in the right place or if it should be somewhere else. Any suggestions as to downloadable video tutorials or pdfs would be great. I am currently reading Big Java and Beginning Java, but they seem to be a little over my head. I have also read Beginning Java Programming for Dummies but it did not really cover GUI's to this extent. Again I do not wish for anyone to write it for me, but suggestions as to how I should do it or better yet to learning materials I can grasp and read offline would be great. Thank you.