简体   繁体   中英

The type TextFieldDemo must implement the inherited abstract method ActionListener?

I almost have this code working. I can launch the GUI, however, when I press any of the buttons, I get the following error message:

The type TextFieldDemo must implement the inherited abstract method ActionListener

I have looked around online and can't see any issues with the code. Can someone help me please?

Thanks :)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

 public class TextFieldDemo implements ActionListener{

private JLabel lblName, lblAddress, lblPhone;
private JTextField txtName, txtAddress, txtPhone;
private JButton btnUpper, btnLower, btnExit;    
private JPanel panel;
private JFrame frame;

public static void main(String[] args){
    new TextFieldDemo();
}   

public TextFieldDemo(){
    createForm();
    addFields();
    addButtons();

    frame.add(panel); 
    frame.setVisible(true);
}

public void createForm(){
    frame = new JFrame();
    frame.setTitle("Student Form"); 
    frame.setSize(400,300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    panel = new JPanel();
    panel.setLayout(null);
}


public void addFields(){
    txtName = new JTextField("Fred Bloggs");
    txtName.setBounds(110, 50, 150, 20);
    panel.add(txtName);

    lblAddress = new JLabel ("Address");
    lblAddress.setBounds(10, 70, 100, 20);
    panel.add (lblAddress);

    txtAddress = new JTextField ("Ashley Road");
    txtAddress.setBounds(110, 70, 150, 20);
    panel.add (txtAddress);

    lblPhone = new JLabel ("Phone Number");
    lblPhone.setBounds(10, 90, 100, 20);
    panel.add (lblPhone);

    txtPhone= new JTextField("01202 191 3333");
    txtPhone.setBounds(110, 90, 150, 20);
    panel.add (txtPhone);

    lblName = new JLabel("Student Name");
    lblName.setBounds(10, 50, 100, 20);
    panel.add(lblName);

}

public void addButtons(){
    btnUpper = new JButton ("UpperCase");
    btnUpper.setBounds(50, 200, 100, 20);
    btnUpper.addActionListener(this);
    panel.add (btnUpper);


    btnLower = new JButton ("LowerCase");
    btnLower.setBounds(150, 200, 100, 20);
    btnLower.addActionListener(this);
    panel.add (btnLower);

    btnExit = new JButton ("Exit");
    btnExit.setBounds(250, 200, 100, 20);
    btnExit.addActionListener(this);
    panel.add (btnExit);

}

class UpperCaseHandler implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        txtName.setText(txtName.getText().toUpperCase());
        txtAddress.setText(txtAddress.getText().toUpperCase());
    }


    class LowerCaseHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            txtName.setText(txtName.getText().toLowerCase());
            txtAddress.setText(txtAddress.getText().toLowerCase());
        }

        class ExitHandler implements ActionListener{
            public void actionPerformed(ActionEvent e) {
                int n = JOptionPane.showConfirmDialog(frame, 
                        "Are you sure you want to exit?", 
                        "Exit?", 
                        JOptionPane.YES_NO_OPTION);
                if(n == JOptionPane.YES_OPTION){
                    System.exit(0);
                }
            }
        }


    }
}
 }

You say TextFieldDemo implements ActionListener , but it doesn't have an actionPerformed() method, so it's not actually implementing the interface.

Either implement the method or don't claim it implements the interface.

I didn't think it would compile like you have it, but there you go!

The error should make you check the documentation of ActionListener to find out what method(s) is missing. Sometimes eclipse will prompt you to add stubs for the missing methods.

The documentation shows the interface ActionListener has one method to be implemented, actionPerformed();

http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html

Strange, the compiler should throw an error on this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM