2016-05-10 14 views
-3

Javaに触発され、コンパイル時にatmプログラムを作成していますgetクラスは抽象的なエラーではなく、抽象メソッドをオーバーライドしません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."; 
    } 
} 
+1

'actionperformed' < - 大文字を確認してください。 – Tunaki

+0

ちょうど試してみましたが、同じエラーが表示されました。返信いただきありがとうございます。 – TheNoviceProgrammer

+1

これは簡単ではありません。このエラーは*抽象メソッドactionperformed(ActionEvent)*をオーバーライドしません。そのため、すべての名前を変更するか、エラーメッセージが変更されました。 – Tunaki

答えて

0

を実装して拡張したActionListener

パブリッククラスでは、問題がATMクラスはimplements ActionListenerことを宣言されていることです。ただし、ATMクラスでは、public void actionPerformed(ActionEvent)の方法はありません。 ATMクラス

現在の方法は、次のとおり

  • testAccount(int, int)buttonOK.addActionListener(...)ラインから呼び出された)
  • buttonOKActionPerformed(ActionEvent)
  • main方法。

このコードはATMクラスに存在するという事実についての混乱がある可能です:

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

このコードactionPerformedメソッドを実装しないが、それはbuttonOKオブジェクトにスコープされます。スコープはbuttonOKに限定されているため、ATMクラスで実装されたではありません。

この問題を解決するには、ATMクラスでこのメソッドを実装する必要があります。 JFrame自体がイベントをリッスンする必要がない場合は、別の方法として、ATMクラスからimplementsを削除します。

+0

ケビンに助けてくれてありがとう、私はそれを整理しようとします。 – TheNoviceProgrammer

関連する問題