单击按钮时出现运行时错误,它显示错误发生在操作侦听器行中



我使用Eclipse来解决编译错误,但我需要这个程序在Java Ready 1.4中工作。这是它为 Eclipse 显示的错误:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
at test.MortgageCalculator$1.actionPerformed(MortgageCalculator.java:97)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我尝试使用:nextButton.addActionListener(this(;调用Action Listener,它仍然给出相同的错误。

package test;
import java.awt.*; //for Dimension
import javax.swing.*; //for GUI components
import java.awt.event.*; //for MessageListener
public class MortgageCalculator extends JFrame implements ActionListener
{
public String nameField, ageField, incomeField, priceField, downPayField, interestRateField, paymentField, amortField;

public MortgageCalculator ()
{ //Frame
    JFrame frame = new JFrame ();
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.setSize (new Dimension (300, 250));
    frame.setTitle ("Mortgage Calculator");
    frame.getContentPane ().setLayout (new BorderLayout ());
    //User Information (Input) - Center Panel
    JPanel centerPanel = new JPanel (new GridLayout (16, 1));
    //Personal Information
    centerPanel.add (new JLabel ("Personal Information", SwingConstants.CENTER));
    centerPanel.add (new JLabel ("Full Name:"));
    final JTextField nameField = new JTextField ();
    centerPanel.add (nameField);
    centerPanel.add (new JLabel ("Your Age:"));
    final JTextField ageField = new JTextField ();
    centerPanel.add (ageField);
    centerPanel.add (new JLabel ("Yearly Income:"));
    final JTextField incomeField = new JTextField ();
    centerPanel.add (incomeField);
    centerPanel.add (new JLabel ());
    //Mortgage Information
    centerPanel.add (new JLabel ("Mortgage Information" , SwingConstants.CENTER));
    centerPanel.add (new JLabel ("Price of Property:"));
    final JTextField priceField = new JTextField ();
    centerPanel.add (priceField);
    centerPanel.add (new JLabel ("Down Payment:"));
    final JTextField downPayField = new JTextField ();
    centerPanel.add (downPayField);
    centerPanel.add (new JLabel ("Interest Rate (%):"));
    final JTextField interestRateField = new JTextField ();
    centerPanel.add (interestRateField);
    centerPanel.add (new JLabel ("Amortization Period (Years):"));
    frame.getContentPane ().add (centerPanel, BorderLayout.CENTER);
    //User Options (Input) - South Panel
    JPanel southPanel = new JPanel (new GridLayout (8, 3));
    //Payment Option
    southPanel.add (new JLabel ("Payment:"));
    southPanel.add (new JLabel ("Type in 1, 2 or 3"));
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ("1. Weekly"));
    southPanel.add (new JLabel ("2. Biweekly"));
    southPanel.add (new JLabel ("3. Monthly"));
    JTextField paymentField = new JTextField ();
    southPanel.add (paymentField);
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ());
    //Amortization Period Option
    southPanel.add (new JLabel ("Amortization Period:"));
    southPanel.add (new JLabel ("Type in 1, 2 or 3"));
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ("1. 20 years"));
    southPanel.add (new JLabel ("2. 25 years"));
    southPanel.add (new JLabel ("3. 30 years"));
    JTextField amortField = new JTextField ();
    southPanel.add (amortField);
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ());
    //To add a blank line in frame, added three blank JLabels because it is 3 horiontal for grid layout
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ());
    southPanel.add (new JButton ("Clear Information"));
    southPanel.add (new JLabel ());
    JButton nextButton = new JButton ("Next");
    southPanel.add (nextButton);
    frame.getContentPane ().add (southPanel, BorderLayout.SOUTH);

    frame.pack ();
    frame.setVisible (true);
    nextButton.addActionListener(new ActionListener () {
        public void actionPerformed (ActionEvent e) {
            //Gets the personal information from the text fields
            String name;
            int age, payment, amort;
            double income, price, downPay, interestRate;
            name = nameField.getText();
            age = Integer.parseInt (ageField.getText ());
            income = Double.parseDouble (incomeField.getText ());
            price = Double.parseDouble (priceField.getText ());
            downPay = Double.parseDouble (downPayField.getText ());
            interestRate = Double.parseDouble (interestRateField.getText ());
            payment = Integer.parseInt (paymentField.getText ());
            amort = Integer.parseInt (amortField.getText ());
            interestRate = interestRate / 100;      

            //Output frame
            JFrame frame2 = new JFrame ();
            frame2.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame2.setSize (new Dimension (300, 250));
            frame2.setTitle ("Mortgage Calculator");
            frame2.getContentPane ().setLayout (new BorderLayout ());
            JPanel westPanel2 = new JPanel (new GridLayout (3, 1));
            JLabel nameLabel = new JLabel ("Name: " + name);
            westPanel2.add (nameLabel);
            JLabel ageLabel = new JLabel ("Age: " + age);
            westPanel2.add (ageLabel);
            JLabel incomeLabel = new JLabel ("Yearly Income: $" + income);
            westPanel2.add (incomeLabel);
            frame2.getContentPane ().add (westPanel2, BorderLayout.WEST);
            JPanel centerPanel2 = new JPanel (new GridLayout (3, 1));
            JLabel priceLabel = new JLabel ("    Price of property: $" + price);
            centerPanel2.add (priceLabel);
            JLabel downPayLabel = new JLabel ("    Down Payment: $" + downPay);
            centerPanel2.add (downPayLabel);
            JLabel interestRateLabel = new JLabel ("    Interest Rate: " + income);
            centerPanel2.add (interestRateLabel);
            frame2.getContentPane ().add (centerPanel2, BorderLayout.CENTER);

            double interest, numerator, l, denominator, payPerPeriod, initialP, mortgageYearly;
            if (payment == 1)
            {
                interest = 1 + interestRate / 52;
                if (amort == 1)
                {
                    l = Math.pow (interest, 20 * 52);
                    numerator = l * interest;
                    denominator = l - 1;
                    initialP = price - downPay;
                    payPerPeriod = (numerator / denominator) * initialP;
                }
                else if (amort == 2)
                {
                    l = Math.pow (interest, 25 * 52);
                    numerator = l * interest;
                    denominator = l - 1;
                    initialP = price - downPay;
                    payPerPeriod = (numerator / denominator) * initialP;
                }
                else // 3. amort30
                {
                    l = Math.pow (interest, 30 * 52);
                    numerator = l * interest;
                    denominator = l - 1;
                    initialP = price - downPay;
                    payPerPeriod = (numerator / denominator) * initialP;
                }
                mortgageYearly = payPerPeriod * 52;
            }
            else if (payment == 2)
            {
                interest = 1 + interestRate / 26;
                if (amort == 1)
                {
                    l = Math.pow (interest, 20 * 26);
                    numerator = l * interest;
                    denominator = l - 1;
                    initialP = price - downPay;
                    payPerPeriod = (numerator / denominator) * initialP;
                }
                else if (amort == 2)
                {
                    l = Math.pow (interest, 25 * 26);
                    numerator = l * interest;
                    denominator = l - 1;
                    initialP = price - downPay;
                    payPerPeriod = (numerator / denominator) * initialP;
                }
                else // 3. amort30
                {
                    l = Math.pow (interest, 30 * 26);
                    numerator = l * interest;
                    denominator = l - 1;
                    initialP = price - downPay;
                    payPerPeriod = (numerator / denominator) * initialP;
                }
                mortgageYearly = payPerPeriod * 26;

            }
            else //3. monthly payment
            {
                interest = 1 + interestRate / 12;
                if (amort == 1)
                {
                    l = Math.pow (interest, 20 * 12);
                    numerator = l * interest;
                    denominator = l - 1;
                    initialP = price - downPay;
                    payPerPeriod = (numerator / denominator) * initialP;
                }
                else if (amort == 2)
                {
                    l = Math.pow (interest, 25 * 12);
                    numerator = l * interest;
                    denominator = l - 1;
                    initialP = price - downPay;
                    payPerPeriod = (numerator / denominator) * initialP;
                }
                else // 2. amort30
                {
                    l = Math.pow (interest, 30 * 12);
                    numerator = l * interest;
                    denominator = l - 1;
                    initialP = price - downPay;
                    payPerPeriod = (numerator / denominator) * initialP;
                }
                mortgageYearly = payPerPeriod * 12;
            }

            JPanel eastPanel2 = new JPanel (new GridLayout (3, 1));
            JLabel payTotalLabel = new JLabel ("    Payment Total: $" + initialP);
            eastPanel2.add (priceLabel);
            JLabel payPerPeriodLabel = new JLabel ("    Payment per period: $" + payPerPeriod);
            eastPanel2.add (payPerPeriodLabel);
            frame2.getContentPane ().add (eastPanel2, BorderLayout.EAST);

            JPanel southPanel2 = new JPanel (new GridLayout (2, 1));
            double minimumIncome = mortgageYearly / 0.28;
            JLabel minimumLabel = new JLabel ("    Minimum Yearly Income: $" + minimumIncome);
            eastPanel2.add (priceLabel);
            if (minimumIncome > income)
            {
                JLabel qualifiedLabel = new JLabel ("You are not qualified for the mortgage");
                eastPanel2.add (qualifiedLabel);
            }
            else
            {
                JLabel qualifiedLabel = new JLabel ("You are qualified for the mortgage");
                eastPanel2.add (qualifiedLabel);
            }

            frame2.getContentPane ().add (eastPanel2, BorderLayout.EAST);

            frame2.pack ();
            frame2.setVisible (true);

        }
    });
}

public static void main (String[] args)
{
    MortgageCalculator mainApp = new MortgageCalculator ();
}
}
public class MortgageCalculator extends JFrame implements ActionListener

为什么你说你的类实现了ActionListener?

您没有在类中的任何位置实现 ActionListener 接口,因此请删除该声明:

public class MortgageCalculator extends JFrame 

相关内容

  • 没有找到相关文章