【问题标题】:Class is not abstract and does not override abstract method error when attempting to compile [closed]类不是抽象的,并且在尝试编译时不会覆盖抽象方法错误[关闭]
【发布时间】:2016-05-10 16:25:18
【问题描述】:

Java 新手,一直在创建 atm 程序,当我编译时,我得到 class is not an abstract 错误,并且不会覆盖 ActionListener 中的抽象方法 actionperformed(ActionEvent)

公共类 ATM 扩展 JFrame 实现 ActionListener

import java.util.Arrays;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;



  public class ATM extends JFrame implements ActionListener {

    private JLabel labelPassword = new JLabel("Enter password:");

    private JLabel labelConfirmPassword = new JLabel("Confirm password:");
    private JPasswordField passwordField1 = new JPasswordField(20);
    private JPasswordField passwordField2 = new JPasswordField(20);
    private JButton buttonOK = new JButton("OK");
    Label lab=new Label("                                                                                                                                                                 ");
    Label lab1=new Label("                                                                                                                                                                 ");
    TextField t[]=new TextField [4];
    Label l[]=new Label [4];
    Button but=new Button("Create Account");
    Button but1=new Button("Enter");
    BankAccount b;
    ATM()
    {
            addWindowListener(new NewWindowAdapter());
            setLayout(new GridLayout(2,0));
            Panel p=new Panel();
            Panel p1=new Panel();
            but.addActionListener(this);
            but1.addActionListener(this);
            p.setLayout(new GridLayout(5,2));
            p1.add(lab1);
            p1.add(lab);
            add(labelPassword);
            add(passwordField1);
            add(labelConfirmPassword);
            add(passwordField2);
            add(buttonOK);
            l[0]=new Label("Account Number");
            l[1]=new Label("Initial Balance");
            l[2]=new Label("Deposit Amount");
            l[3]=new Label("Withdraw Amount");
            for(int i=0;i<4;i++)
            {
                    t[i]=new TextField(10);
                    p.add(l[i]);
                    p.add(t[i]);
            }

            but1.setVisible(false);
            l[2].setVisible(false);
            l[3].setVisible(false);
            t[2].setVisible(false);
            t[3].setVisible(false);


            buttonOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
            buttonOKActionPerformed(event);
            }
            });
    }
    String testAccount(int d_amt,int w_amt)
    {
            String msg;
            b.deposit(d_amt);
            msg="Transaction Succesful";
            try
            {
                    b.withdraw(w_amt);
            }catch(FundsInsufficientException fe)
            {
                    fe=new FundsInsufficientException(b.amount,w_amt);
                    msg=String.valueOf(fe);
            }
            return msg;
    }
    private void buttonOKActionPerformed(ActionEvent event) {
    char[] password1 = passwordField1.getPassword();
    char[] password2 = passwordField2.getPassword();

    if (!Arrays.equals(password1, password2)) {
        JOptionPane.showMessageDialog(ATM.this, 
                "Passwords do not match!");
        return;
    }       

    char[] correctPass = new char[] {'J', 'a', 'm', 'e', 's'};
    if (Arrays.equals(password1, correctPass)) {
        b=new BankAccount(Integer.parseInt(t[0].getText()),Integer.parseInt(t[1].getText()));
                    but1.setVisible(true);
                    l[2].setVisible(true);
                    l[3].setVisible(true);
                    t[2].setVisible(true);
                    t[3].setVisible(true);
                    but.setVisible(false);
                    l[0].setVisible(false);
                    l[1].setVisible(false);
                    t[0].setVisible(false);
                    t[1].setVisible(false);
                    lab1.setText("Account : "+b.accnum+", Current Balance : "+b.amount);
                    return; 

    } else {
        JOptionPane.showMessageDialog(ATM.this, 
            "Wrong password!");         
    }
}
    public static void main(String arg[])
    {
        SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
        new ATM().setVisible(true);
        }
        });
            ATM use=new ATM();
            use.setTitle("James' Cash Machine");
            use.setSize(600,300);
            use.setVisible(true);
            }
   }
   class NewWindowAdapter extends WindowAdapter
   {
          public void windowClosing(WindowEvent we)
          {
                  System.exit(0);
          }
   }
   class BankAccount
   {
    int accnum;
    int amount;
    BankAccount(int num,int amt)
    {
            accnum=num;
            amount=amt;
    }
    public void deposit(int amt)
    {
            amount=amount+amt;
    }
    public void withdraw(int amt) throws FundsInsufficientException
    {
            if(amt>amount)
                    throw new FundsInsufficientException(amount,amt);
            else
                    amount=amount-amt;
    }
  }
  class FundsInsufficientException extends Exception
 {
           int balance;
           int withdraw_amount;
           FundsInsufficientException(int bal,int w_amt)
           {
            balance=bal;
            withdraw_amount=w_amt;
    }
    public String toString()
    {
            return "Your withdraw amount ("+withdraw_amount+") is less than            the balance ("+balance+"). No withdrawal was recorded.";
    }
 }

【问题讨论】:

  • actionperformed
  • 刚试了下,同样的错误,谢谢回复
  • 这根本不可能。错误是没有覆盖抽象方法actionperformed(ActionEvent)。所以要么你全部重命名,要么错误信息改变了。
  • 假设代码完整,我在ATM 类中没有看到方法public void actionPerformed(ActionEvent)。在 buttonOK 侦听器中有 一个 actionPerformed,但在 ATM 类中没有。如果不需要JFrame,也许删除implements ActionListener。或者,实现该方法。当然,我可能看不到这种方法。
  • 我收到这些错误 Tunaki:ATM 不是抽象的,并且不会覆盖 ActionListener 中的抽象方法 actionPerformed(ActionEvent) 公共类 ATM 扩展 JFrame 实现 ActionListener ^ 不是抽象的,不会覆盖ActionListener 中的抽象方法 actionPerformed(ActionEvent) buttonOK.addActionListener(new ActionListener() { ^ 方法不会覆盖或实现来自超类型@Override 的方法

标签: java swing


【解决方案1】:

根据当前发布的代码,问题在于ATM 类声明它为implements ActionListener。但是,在ATM 类中,没有为public void actionPerformed(ActionEvent) 提供方法。

ATM 类中的当前方法是:

  • testAccount(int, int)
  • buttonOKActionPerformed(ActionEvent)(从buttonOK.addActionListener(...) 行调用)
  • main 方法。

这个代码存在于ATM 类中这一事实可能会引起混淆:

            buttonOK.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent event) {
                buttonOKActionPerformed(event);
               }
              });

此代码确实实现了actionPerformed 方法,但它的作用域是buttonOK 对象。由于范围仅限于buttonOK,它没有ATM 类上实现。

要解决此问题,ATM 类必须实现该方法。如果 JFrame 本身不需要监听事件,那么另一种方法是从 ATM 类中移除 implements

【讨论】:

  • 感谢Kevin的帮助,我会努力解决的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-25
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
相关资源
最近更新 更多