Results 1 to 3 of 3
  1. #1
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Problem with frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E)

    When transcribing this code I receive the following error message: "The method setDefaultCloseOperation(int) is undefined for the type LoanCalculator" I do not understand why I am receiving this message or how to get rid of it. The error is on line 80.

    Here is the code:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;  
    import javax.swing.*;
    import javax.swing.border.TitledBorder;
    
    //create LoanCalcualtor class that extends the JFrame class, thus making 
    //the LoanCalclator class a subclass of JFrame.
    public class LoanCalculator extends Frame {
    	//Create text fields for interest rate
    	//year, loan amount, monthly payment, and total payment
    	private JTextField jtfAnnualInterestRate = new JTextField();
    	private JTextField jtfNumberOfYears = new JTextField();
    	private JTextField jtfLoanAmount = new JTextField();
    	private JTextField jtfMonthlyPayment = new JTextField();
    	private JTextField jtfTotalPayment = new JTextField();
    	
    	//create a compute payment button
    	private JButton jbtComputeLoan = new JButton("Compute Payment");
    	
    	//construct LoanCalculator() constructor
    	public LoanCalculator() {
    		//create p1 JPanel object to hold labels and text fields
    		JPanel p1 = new JPanel(new GridLayout(5, 2));
    		//add labels and text fields to p1 panel
    		p1.add(new JLabel("Annual Interest Rate"));
    		p1.add(jtfAnnualInterestRate);
    		p1.add(new JLabel("Number of Years"));
    		p1.add(jtfNumberOfYears);
    		p1.add(new JLabel("Loan Amount"));
    		p1.add(jtfLoanAmount);
    		p1.add(new JLabel("Monthly Payment"));
    		p1.add(jtfMonthlyPayment);
    		p1.add(new JLabel("Total Payment"));
    		p1.add(jtfTotalPayment);
    		p1.setBorder(new
    				TitledBorder("Enter loan amount, interest rate, and year"));
    		
    		//create p2 JPanel object to hold button
    		JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    		//add button to p2 panel
    		p2.add(jbtComputeLoan);
    		
    		//add panels to the frame
    		add(p1, BorderLayout.CENTER);
    		add(p2, BorderLayout.SOUTH);
    		
    		//register listener for button
    		jbtComputeLoan.addActionListener(new ButtonListener());
    	}
    	
    	//create ButtonListener class to handle the compute payment button
    	public class ButtonListener implements ActionListener {
    		//create actionPerformed() method
    		public void actionPerformed(ActionEvent e) {
    			//Get the values from the text fields
    			double interest =
    					Double.parseDouble(jtfAnnualInterestRate.getText());
    			int year = 
    					Integer.parseInt(jtfNumberOfYears.getText());
    			double loanAmount = 
    					Double.parseDouble(jtfLoanAmount.getText());
    			
    			//create a new loan object
    			Loan loan = new Loan(interest, year, loanAmount);
    			
    			//display monthly payment and total payment
    			jtfMonthlyPayment.setText(String.format("%.2f",
    					loan.getMonthlyPayment()));
    			jtfTotalPayment.setText(String.format("%.2f",
    					loan.getTotalPayment()));	
    		}
    	}
    	
    	public static void main(String [] args) {
    		//create new frame object
    		LoanCalculator frame = new LoanCalculator();
    		frame.pack();
    		frame.setTitle("Loan Calculator");
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);	
    	}
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: Problem with frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E)

    Since LoanCalculator extends Frame, I want you to go to the Java API, look up the Frame class and show me where the setDefaultCloseOperation method is.

  3. #3
    jim01 is offline Member
    Join Date
    Sep 2010
    Location
    Southwest Missouri
    Posts
    97
    Rep Power
    0

    Default Re: Problem with frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E)

    That should have been JFrame, not Frame. I must have fat-fingered the J key. Thank you for your help.

Similar Threads

  1. Need help with setDefaultCloseOperation
    By noneofthem in forum New To Java
    Replies: 1
    Last Post: 02-23-2011, 12:03 PM
  2. Java slave Frame access to its owner main frame problem
    By cagdaseckin in forum New To Java
    Replies: 0
    Last Post: 12-10-2010, 10:40 AM
  3. Using frame.pack() for resizing JFrame
    By LianaN in forum AWT / Swing
    Replies: 0
    Last Post: 10-30-2010, 12:12 PM
  4. Frame problem
    By nikunj in forum AWT / Swing
    Replies: 3
    Last Post: 08-09-2010, 02:01 PM
  5. Replies: 3
    Last Post: 08-07-2010, 05:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •