Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0

    Help with separating into multiple classes


    Can someone help me figure out how I would break this program into multiple different classes? Like one for each button off of the mainframe window at the beginning of the application?? Thanks for any help. Here is my code so far. I know it's not complete yet:

    Code:
    package financialCalc;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import java.text.DecimalFormat;
    import java.awt.*;
    import java.awt.event.*;
    
    public class FinancialCalc extends JFrame
    {
    	double balance, minimumPayment;
    	String payment, fixedPayment;
    	private JPanel welcomePanel, greetingPanel, buttonPanel, minPmtButtonPanel, minPmtPanel;
    	private JLabel message, Amt, Term, Rate, minPmt, fixedPmt;
    	//private JLabel autoAmtArray[], autoRateArray[], autoTermArray[];
    	private JTextField AmtF, TermF, RateF, minPmtF, fixedPmtF;
    	private JButton autoPmt, mortPmt, homeAfford, costOfMin, exit, minPmtButton, calcPmts;
    	private JFrame mainFrame, autoPmtFrame, mortPmtFrame, minPmtFrame;
    	private Dimension d = new Dimension(400,400);
    	private DecimalFormat dlr = new DecimalFormat("#,##0.00");
    	private DecimalFormat pct = new DecimalFormat("#0");
    	//private JCheckBox add;
    	
    	public FinancialCalc()
    	{
    		
    		mainFrame = new JFrame("Financial Calculator Application");		
    		
    		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
    		
    		mainFrame.setPreferredSize(d);
    		mainFrame.setLayout(new BorderLayout());
    		mainFrame.setLocationRelativeTo(null);
    		
    		buildMenuBar();
    		buildWelcomeMenu();
    		
    		mainFrame.add(greetingPanel, BorderLayout.NORTH);
    		mainFrame.add(welcomePanel, BorderLayout.CENTER);
    		mainFrame.add(buttonPanel, BorderLayout.SOUTH);		
    		
    		mainFrame.pack();
    		mainFrame.setVisible(true);
    	}
    	
    	
    	public void buildMenuBar()
    	{
    		JMenuBar menuBar;
    		JMenu fileMenu;
    		JMenu editMenu;
    		JMenuItem exit;
    		JMenuItem cut;
    		JMenuItem copy;
    		JMenuItem paste;
    		
    		menuBar = new JMenuBar();
    		
    		fileMenu = new JMenu("File");
    		fileMenu.setMnemonic(KeyEvent.VK_F);
    		
    		exit = new JMenuItem("Exit");
    		exit.setMnemonic(KeyEvent.VK_E);
    		exit.addActionListener(new ExitButtonListener());
    	
    		fileMenu.add(exit);
    		
    		
    		editMenu = new JMenu("Edit");
    		editMenu.setMnemonic(KeyEvent.VK_E);
    		
    		cut = new JMenuItem("Cut");
    		cut.setMnemonic(KeyEvent.VK_X);
    		
    		copy = new JMenuItem("Copy");
    		copy.setMnemonic(KeyEvent.VK_C);
    		
    		paste = new JMenuItem("Paste");
    		paste.setMnemonic(KeyEvent.VK_P);
    		
    		editMenu.add(cut);
    		editMenu.add(copy);
    		editMenu.add(paste);
    		
    		
    		menuBar.add(fileMenu);
    		menuBar.add(editMenu);
    		
    		setJMenuBar(menuBar);
    	}
    		
    	
    	public void buildWelcomeMenu()
    	{
    		welcomePanel = new JPanel();
    		welcomePanel.setLayout(new GridLayout(4,1));
    		welcomePanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));	
    		
    		autoPmt = new JButton("Auto loan payment calculator");
    		autoPmt.setMnemonic(KeyEvent.VK_A);
    		autoPmt.addActionListener(new AutoButtonListener());
    		
    		mortPmt = new JButton("Mortgage payment calculator");
    		mortPmt.setMnemonic(KeyEvent.VK_M);
    		mortPmt.addActionListener(new MortButtonListener());
    		
    		homeAfford = new JButton("How much home can you afford?");
    		homeAfford.setMnemonic(KeyEvent.VK_H);
    		homeAfford.addActionListener(new HomeButtonListener());
    		
    		costOfMin = new JButton("True cost of paying the minimum payment");
    		costOfMin.setMnemonic(KeyEvent.VK_C);
    		costOfMin.addActionListener(new CostMinButtonListener());
    		
    		buttonPanel = new JPanel();
    		exit = new JButton("Exit");
    		exit.setMnemonic(KeyEvent.VK_E);
    		exit.addActionListener(new ExitButtonListener());
    		buttonPanel.add(exit);
    		
    		welcomePanel.add(autoPmt);
    		welcomePanel.add(mortPmt);
    		welcomePanel.add(homeAfford);
    		welcomePanel.add(costOfMin);
    	
    		
    		greetingPanel = new JPanel();
    		message = new JLabel("Welcome to the Financial Calculations Application");
    		greetingPanel.add(message);
    		
    		
    	}
    	
    	
    	private class AutoButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			JPanel autoPmtPanel, autoButtonPanel;
    			autoPmtFrame = new JFrame();
    			autoPmtFrame = new JFrame("Auto Payment Calculator");			
    			autoPmtFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			autoPmtFrame.setLayout(new GridLayout(2,1));
    			autoPmtFrame.setPreferredSize(d);
    			autoPmtFrame.setLocationRelativeTo(null);
    			
    			autoPmtPanel = new JPanel();
    			autoPmtPanel.setLayout(new GridLayout(4,2));
    			autoPmtPanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));
    			
    			
    			autoButtonPanel = new JPanel();
    			//add = new JCheckBox("Compare another loan");
    			//add.addActionListener(new AddButtonListener());
    			
    						
    			JButton calculate = new JButton("Calculate");
    			JButton exit = new JButton("Exit");
    			calculate.addActionListener(new AutoCalcButtonListener());
    			exit.addActionListener(new ExitButtonListener());
    			
    			Amt = new JLabel("Loan Amount");
    			Term = new JLabel("Term in months");
    			Rate = new JLabel("Interest Rate");
    			
    			
    			AmtF = new JTextField(5);
    			TermF = new JTextField(5);
    			RateF = new JTextField(5);
    			
    			autoPmtPanel.add(Amt);
    			autoPmtPanel.add(AmtF);
    			autoPmtPanel.add(Term);
    			autoPmtPanel.add(TermF);
    			autoPmtPanel.add(Rate);
    			autoPmtPanel.add(RateF);
    			
    			autoButtonPanel.add(calculate);
    			autoButtonPanel.add(exit);
    
    			autoPmtFrame.add(autoPmtPanel);
    			autoPmtFrame.add(autoButtonPanel);
    			
    			autoPmtFrame.pack();
    			autoPmtFrame.setVisible(true);
    			
    		}
    	
    	}
    	
    	private class AutoCalcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			
    			String str;
    			double amt, n, rate, denominator;
    			double monthlyPmt, totalInt, totalAmtPaid;
    			
    			str = AmtF.getText();
    			amt = Double.parseDouble(str);
    			
    			str = TermF.getText();
    			n = Double.parseDouble(str);
    			
    			str = RateF.getText();
    			rate = Double.parseDouble(str);
    			
    			rate = rate / 100 / 12;
    			denominator = 1- Math.pow(1 + rate, -n);
    			
    			monthlyPmt = amt * (rate / denominator);
    			
    			totalAmtPaid = monthlyPmt * n;
    			
    			totalInt = totalAmtPaid - amt;
    			
    			JOptionPane.showMessageDialog(null, "Monthly Payment is $" + dlr.format(monthlyPmt) + "\n\n" + 
    					"Total Amount Paid over " + pct.format(n) + " months: $" + dlr.format(totalAmtPaid) + "\n\n" +
    					"Total Interest Paid: $" + dlr.format(totalInt), 
    					"Total", JOptionPane.PLAIN_MESSAGE);
    			
    			
    		}
    	}
    	
    	private class MortButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			mortPmtFrame = new JFrame("Mortgage Payment Calculator");
    			mortPmtFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			mortPmtFrame.setLayout(new GridLayout(2,1));
    			mortPmtFrame.setPreferredSize(d);
    			mortPmtFrame.setLocationRelativeTo(null);
    						
    			JPanel mortPmtPanel, mortButtonPanel;
    			
    			mortPmtPanel = new JPanel();
    			mortButtonPanel = new JPanel();
    			
    			mortPmtPanel.setLayout(new GridLayout(4,2));
    			mortPmtPanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));
    			
    			
    			JButton calculate = new JButton("Calculate");
    			JButton exit = new JButton("Exit");
    			calculate.addActionListener(new MortCalcButtonListener());
    			exit.addActionListener(new ExitButtonListener());
    			
    			Amt = new JLabel("Loan Amount");
    			Term = new JLabel("Term in months");
    			Rate = new JLabel("Interest Rate");
    			
    			
    			AmtF = new JTextField(5);
    			TermF = new JTextField(5);
    			RateF = new JTextField(5);
    			
    			mortPmtPanel.add(Amt);
    			mortPmtPanel.add(AmtF);
    			mortPmtPanel.add(Term);
    			mortPmtPanel.add(TermF);
    			mortPmtPanel.add(Rate);
    			mortPmtPanel.add(RateF);
    			
    			mortButtonPanel.add(calculate);
    			mortButtonPanel.add(exit);
    
    			mortPmtFrame.add(mortPmtPanel);
    			mortPmtFrame.add(mortButtonPanel);
    			
    			mortPmtFrame.pack();
    			mortPmtFrame.setVisible(true);
    			
    		}
    	
    	}
    	
    	
    	private class MortCalcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			
    			String str;
    			double amt, n, rate, denominator;
    			double monthlyPmt, totalInt, totalAmtPaid;
    			
    			str = AmtF.getText();
    			amt = Double.parseDouble(str);
    			
    			str = TermF.getText();
    			n = Double.parseDouble(str);
    			
    			str = RateF.getText();
    			rate = Double.parseDouble(str);
    			
    			rate = rate / 100 / 12;
    			denominator = 1- Math.pow(1 + rate, -n);
    			
    			monthlyPmt = amt * (rate / denominator);
    			
    			totalAmtPaid = monthlyPmt * n;
    			
    			totalInt = totalAmtPaid - amt;
    			
    			n = n / 12;
    			
    			JOptionPane.showMessageDialog(null, "Monthly Payment is $" + dlr.format(monthlyPmt) + "\n\n" + 
    					"Total Amount Paid over " + pct.format(n) + " years: $" + dlr.format(totalAmtPaid) + "\n\n" +
    					"Total Interest Paid: $" + dlr.format(totalInt), 
    					"Total", JOptionPane.PLAIN_MESSAGE);
    			
    			
    		}
    	}
    	
    	
    	
    	private class CostMinButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			minPmtFrame = new JFrame("True Cost of Paying the Minimum Payment");
    			minPmtFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			minPmtFrame.setLayout(new GridLayout(2,1));
    			minPmtFrame.setPreferredSize(d);
    			minPmtFrame.setLocationRelativeTo(null);
    			
    			minPmtPanel = new JPanel();
    			minPmtButtonPanel = new JPanel();
    			
    			minPmtPanel.setLayout(new GridLayout(4,2));
    			minPmtPanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));
    			
    			
    			minPmtButton = new JButton("Get Minimum Payment");
    		
    			minPmtButton.addActionListener(new MinPmtButtonListener());
    			
    			
    			Amt = new JLabel(" Balance ");
    			Rate = new JLabel(" Interest Rate ");
    			minPmt = new JLabel(" Minimum Payment ");
    			fixedPmt = new JLabel(" Fixed Payment ");
    			
    			AmtF = new JTextField(10);
    			RateF = new JTextField(10);
    			minPmtF = new JTextField(null);
    			fixedPmtF = new JTextField(null);
    			
    			minPmtPanel.add(Amt);
    			minPmtPanel.add(AmtF);
    			minPmtPanel.add(Rate);
    			minPmtPanel.add(RateF);
    			minPmtPanel.add(minPmt);
    			minPmtPanel.add(minPmtButton);
    			
    			minPmtFrame.add(minPmtPanel);
    			
    			minPmtFrame.pack();
    			minPmtFrame.setVisible(true);
    			
    		}
    	
    	}
    	
    	private class MinPmtButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			balance = Double.parseDouble(AmtF.getText());
    			
    			double fPayment;
    			double pmtPct = .02;
    			calcPmts = new JButton("Calculate");
    			calcPmts.addActionListener(new MinPmtsCalcListener());
    			
    			minimumPayment = balance * pmtPct;
    			fPayment = minimumPayment * 2;
    			dlr.format(minimumPayment);
    			dlr.format(fPayment);
    						
    			payment = Double.toString(minimumPayment);
    			fixedPayment = Double.toString(fPayment);
    			
    			minPmtF.setText(payment);
    			fixedPmtF.setText(fixedPayment);
    			
    			minPmtPanel.removeAll();			
    			minPmtPanel.add(Amt);
    			minPmtPanel.add(AmtF);
    			minPmtPanel.add(Rate);
    			minPmtPanel.add(RateF);
    			minPmtPanel.add(minPmt);
    			minPmtPanel.add(minPmtF);
    			minPmtPanel.add(fixedPmt);
    			minPmtPanel.add(fixedPmtF);			
    			
    			minPmtButtonPanel.add(calcPmts);
    			minPmtFrame.add(minPmtPanel);
    			minPmtFrame.add(minPmtButtonPanel);
    			
    			minPmtFrame.pack();
    			minPmtFrame.setVisible(true);
    			
    		}
    	
    	}
    	
    	private class MinPmtsCalcListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			
    			
    			
    			
    		}
    	
    	}
    	
    	
    	
    	private class HomeButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			
    			
    			
    		}
    	
    	}
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	private class ExitButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);			
    		}
    	
    	}
    	
    	public static void main(String[] args) 
    	{
    		new FinancialCalc();
    
    	}
    
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    The code currently has many classes. Why do you want to make more?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    The code currently has many classes. Why do you want to make more?
    Ok what I meant I guess was that I want to create a new class file so this one file doesn't have everything but that there are a few class files to make it sort of more organized
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Have you tried cutting out the classes in the posted code and making them external classes in their own files?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Have you tried cutting out the classes in the posted code and making them external classes in their own files?
    No but I know that part. What I'm wondering is how to call the buttonListeners when they are in another class file?
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    how to call the buttonListeners
    You use the new statement the same as always to create ("call"???) another class.

    Or are you asking how one class (the listener) can access values in another class? Usually listeners are inner classes so that they can have access to the contents of the class that they are in.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    You use the new statement the same as always to create ("call"???) another class.

    Or are you asking how one class (the listener) can access values in another class? Usually listeners are inner classes so that they can have access to the contents of the class that they are in.
    Ya I just don't know how to do that call? How do I call it so that when the button is clicked, it calls the buttonListener method from the other class file?
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    how to do that call
    By "do call" do you mean create an instance of a class?
    Or do you mean to call a method in a class?
    Your code shouldn't call listener methods. That's done by the java program when an event happens that is being listened for.

    How do I call it so that when the button is clicked
    Use the new statement to create an instance of the Listener class. Pass that reference to the addListener() method.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    By "do call" do you mean create an instance of a class?
    Or do you mean to call a method in a class?
    Your code shouldn't call listener methods. That's done by the java program when an event happens that is being listened for.


    Use the new statement to create an instance of the Listener class. Pass that reference to the addListener() method.
    Any chance you could show me an example from one of my buttons to call one of my listeners pretending it's in a different class file?
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    example from one of my buttons to call one of my listeners
    You do NOT call a listener. The java program calls the listener when an event happens.

    What have you tried so far? Do you get any errors?

    There is the following statement in the code:
    Code:
       costOfMin.addActionListener(new CostMinButtonListener());
    That line should compile with CostMinButtonListener defined in its own class file. There could be a problem compiling that class if that class expects to be an inner class and have access to the enclosing class's values.
    Last edited by NormR; December 8th, 2012 at 11:16 AM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    You do NOT call a listener. The java program calls the listener when an event happens.

    What have you tried so far? Do you get any errors?

    There is the following statement in the code:
    Code:
       costOfMin.addActionListener(new CostMinButtonListener());
    That line should compile with CostMinButtonListener defined in its own class file. There could be a problem compiling that class if that class expects to be an inner class and have access to the enclosing class's values.
    Ok but if costMinButtonListener is in a different class file, will it still be able to access it?
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Yes. That is how most classes are defined. Each class can be defined in its own file.
    If the class file is on the classpath, the compiler will find it.

    Try it and see what happens.
    The problems will be with the inner class expectation of getting at the enclosing class's values.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by NormR
    Yes. That is how most classes are defined. Each class can be defined in its own file.
    If the class file is on the classpath, the compiler will find it.

    Try it and see what happens.
    The problems will be with the inner class expectation of getting at the enclosing class's values.
    Okay thanks. I will let you know what happens! Thanks for all your help
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    28
    Rep Power
    0
    Originally Posted by cgodfrey987
    Okay thanks. I will let you know what happens! Thanks for all your help
    Ok, here is what I have but for example my "autoPmt" button in "buildWelcomeMenu" isn't linking to the "AutoButtonListener" in my other class file (AutoCalc). Do you know how I write the code to link them?

    Code:
    public class FinancialCalc extends JFrame
    {
    	double balance, minimumPayment;
    	String payment, fixedPayment;
    	private JPanel welcomePanel, greetingPanel, buttonPanel, minPmtButtonPanel, minPmtPanel;
    	private JLabel message, Amt, Term, Rate, minPmt, fixedPmt;
    	//private JLabel autoAmtArray[], autoRateArray[], autoTermArray[];
    	private JTextField AmtF, TermF, RateF, minPmtF, fixedPmtF;
    	private JButton autoPmt, mortPmt, homeAfford, costOfMin, exit, minPmtButton, calcPmts;
    	private JFrame mainFrame, mortPmtFrame, minPmtFrame;//, autoPmtFrame;
    	private Dimension d = new Dimension(400,400);
    	private DecimalFormat dlr = new DecimalFormat("#0.00");
    	private DecimalFormat pct = new DecimalFormat("#0");
    	AutoCalc autoPanel = new AutoCalc();
    	//private JCheckBox add;
    	
    	public FinancialCalc()
    	{
    		
    		mainFrame = new JFrame("Financial Calculator Application");		
    		
    		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
    		
    		mainFrame.setPreferredSize(d);
    		mainFrame.setLayout(new BorderLayout());
    		mainFrame.setLocationRelativeTo(null);
    		
    		//buildMenuBar();
    		buildWelcomeMenu();
    		
    		mainFrame.add(greetingPanel, BorderLayout.NORTH);
    		mainFrame.add(welcomePanel, BorderLayout.CENTER);
    		mainFrame.add(buttonPanel, BorderLayout.SOUTH);		
    		
    		mainFrame.pack();
    		mainFrame.setVisible(true);
    	}
    		
    	
    	public void buildWelcomeMenu()
    	{
    		welcomePanel = new JPanel();
    		welcomePanel.setLayout(new GridLayout(4,1));
    		welcomePanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));	
    		
    		autoPmt = new JButton("Auto loan payment calculator");
    		autoPmt.setMnemonic(KeyEvent.VK_A);
    		autoPmt.addActionListener(new AutoButtonListener());
    		
    		
    		mortPmt = new JButton("Mortgage payment calculator");
    		mortPmt.setMnemonic(KeyEvent.VK_M);
    		mortPmt.addActionListener(new MortButtonListener());
    		
    		homeAfford = new JButton("How much home can you afford?");
    		homeAfford.setMnemonic(KeyEvent.VK_H);
    		homeAfford.addActionListener(new HomeButtonListener());
    		
    		costOfMin = new JButton("True cost of paying the minimum payment");
    		costOfMin.setMnemonic(KeyEvent.VK_C);
    		costOfMin.addActionListener(new CostMinButtonListener());
    		
    		buttonPanel = new JPanel();
    		exit = new JButton("Exit");
    		exit.setMnemonic(KeyEvent.VK_E);
    		exit.addActionListener(new ExitButtonListener());
    		buttonPanel.add(exit);
    		
    		welcomePanel.add(autoPmt);
    		welcomePanel.add(mortPmt);
    		welcomePanel.add(homeAfford);
    		welcomePanel.add(costOfMin);
    	
    		
    		greetingPanel = new JPanel();
    		message = new JLabel("Welcome to the Financial Calculations Application");
    		greetingPanel.add(message);
    		
    		
    	}
    
    
    // THE BEGINNING OF MY AUTOCALC CLASS FILE
    
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    
    public class AutoCalc
    {
    	private JLabel Amt, Term, Rate;
    	private JTextField AmtF, TermF, RateF;//, minPmtF, fixedPmtF;
    	private JFrame autoPmtFrame;
    	private DecimalFormat dlr = new DecimalFormat("#0.00");
    	private DecimalFormat pct = new DecimalFormat("#0");
    	private Dimension d = new Dimension(400,400);
    
    	
    
    	private class AutoButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			JPanel autoPmtPanel, autoButtonPanel;
    			autoPmtFrame = new JFrame();
    			autoPmtFrame = new JFrame("Auto Payment Calculator");			
    			autoPmtFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			autoPmtFrame.setLayout(new GridLayout(2,1));
    			autoPmtFrame.setPreferredSize(d);
    			autoPmtFrame.setLocationRelativeTo(null);
    			
    			autoPmtPanel = new JPanel();
    			autoPmtPanel.setLayout(new GridLayout(4,2));
    			autoPmtPanel.setBorder(new EmptyBorder(new Insets(40, 40, 40, 40)));
    			
    			
    			autoButtonPanel = new JPanel();
    			//add = new JCheckBox("Compare another loan");
    			//add.addActionListener(new AddButtonListener());
    			
    						
    			JButton calculate = new JButton("Calculate");
    			JButton exit = new JButton("Exit");
    			calculate.addActionListener(new AutoCalcButtonListener());
    			exit.addActionListener(new ExitButtonListener());
    			
    			Amt = new JLabel("Loan Amount");
    			Term = new JLabel("Term in months");
    			Rate = new JLabel("Interest Rate");
    			
    			
    			AmtF = new JTextField(5);
    			TermF = new JTextField(5);
    			RateF = new JTextField(5);
    			
    			autoPmtPanel.add(Amt);
    			autoPmtPanel.add(AmtF);
    			autoPmtPanel.add(Term);
    			autoPmtPanel.add(TermF);
    			autoPmtPanel.add(Rate);
    			autoPmtPanel.add(RateF);
    			
    			autoButtonPanel.add(calculate);
    			autoButtonPanel.add(exit);
    
    			autoPmtFrame.add(autoPmtPanel);
    			autoPmtFrame.add(autoButtonPanel);
    			
    			autoPmtFrame.pack();
    			autoPmtFrame.setVisible(true);
    			
    		}
    	
    	}
    	
    	private class AutoCalcButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			
    			String str;
    			double amt, n, rate, denominator;
    			double monthlyPmt, totalInt, totalAmtPaid;
    			
    			str = AmtF.getText();
    			amt = Double.parseDouble(str);
    			
    			str = TermF.getText();
    			n = Double.parseDouble(str);
    			
    			str = RateF.getText();
    			rate = Double.parseDouble(str);
    			
    			rate = rate / 100 / 12;
    			denominator = 1- Math.pow(1 + rate, -n);
    			
    			monthlyPmt = amt * (rate / denominator);
    			
    			totalAmtPaid = monthlyPmt * n;
    			
    			totalInt = totalAmtPaid - amt;
    			
    			JOptionPane.showMessageDialog(null, "Monthly Payment is $" + dlr.format(monthlyPmt) + "\n\n" + 
    					"Total Amount Paid over " + pct.format(n) + " months: $" + dlr.format(totalAmtPaid) + "\n\n" +
    					"Total Interest Paid: $" + dlr.format(totalInt), 
    					"Total", JOptionPane.PLAIN_MESSAGE);
    			
    			
    		}
    	}
    	
    	
    	private class ExitButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);			
    		}
    	
    	}
    	
    	
    	
    	
    }
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    linking to the "AutoButtonListener" in my other class file (AutoCalc).
    Is the AutoButtonListener class public and external? If so the new statement should be create an instance of it.

    The posted code shows it private and internal.
    You could write a method that creates an instance of the class and returns it.


    Please post the full text of the error messages.
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo