2017-06-13 42 views
0

我使用Eclipse編譯錯誤,但我需要此程序在Java Ready 1.4中工作。這是它表明Eclipse的錯誤:單擊按鈕時發生運行時錯誤,它顯示錯誤出現在Action Listener行

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(本);調用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(); 

} 
} 
+1

*「我使用了Eclipse的編譯錯誤」 *的事情是,你應該使用警告編譯器通過Eclipse提供**來解決**編譯錯誤,而不是忽略它們來產生..'java.lang.Error:未解決的編譯問題:'! –

+0

請參閱[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/q/9554636/418556) –

回答

1
public class MortgageCalculator extends JFrame implements ActionListener 

你爲什麼說你的類實現的ActionListener?

你不會在任何地方實現ActionListener接口的類,以便擺脫聲明:

public class MortgageCalculator extends JFrame 
+0

我有一個Action Listener,用於此類中的第二個JFrame。 –

+0

您的MortgageCalculator不執行ActionLIstener。閱讀您的教科書以獲取有關「實施」關鍵字的更多信息。理解這個概念是Java 101。 – camickr

相關問題