Method issue Java GUI - must implement the inherited abstract method

Theblazomatic2 :

I have been following videos to build gui's and i used the code in the video for practice, however the videos are from 2009. It is now 2020.

The videos are on youtube, for reference you can look it up.

https://www.youtube.com/watch?v=qhYook53olE

The issue i'm having is the class private thehandler throws an error.

it says: GUI.thehandler must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)

in the video the code runs fine the way it currently is when it is finished.

Did the language get updated so that this no longer works maybe?

Any help is appreciated.

package GUI;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import java.util.Scanner;

public class GUI extends JFrame{

private JTextField item1;
private JTextField item2;
private JTextField item3;
private JPasswordField passwordField;

public GUI() {
    super("Find The King");
    setLayout(new FlowLayout());



    item1 = new JTextField(10);
    add(item1);

    item2 = new JTextField("Enter text here.");
    add(item2);

    item3 = new JTextField("uneditable",20);
    item3.setEditable(false);
    add(item3);

    passwordField = new JPasswordField("Mypass");
    add(passwordField);

    thehandler handler = new thehandler();
    item1.addActionListener(handler);
    item2.addActionListener(handler);
    item3.addActionListener(handler);
    passwordField.addActionListener(handler);
}

    private class thehandler implements ActionListener{

        public void actionpPerformed(ActionEvent event) {
        String  string = "";
            if (event.getSource()==item1) 
                string=String.format("field 1 : %s", event.getActionCommand());
            else if (event.getSource()==item1)
                string=String.format("field 1 : %s", event.getActionCommand());
            else if (event.getSource()==item2)
                string=String.format("field 2 : %s", event.getActionCommand());
            else if (event.getSource()==item3)
                string=String.format("field 3 : %s", event.getActionCommand());
            string=String.format("password field is : %s", event.getActionCommand());
    }


   }
 }
D M :

The class should look like this:

private class thehandler implements ActionListener{

        public void actionPerformed(ActionEvent event) {
        String  string = "";
            if (event.getSource()==item1) 
                string=String.format("field 1 : %s", event.getActionCommand());
            else if (event.getSource()==item1)
                string=String.format("field 1 : %s", event.getActionCommand());
            else if (event.getSource()==item2)
                string=String.format("field 2 : %s", event.getActionCommand());
            else if (event.getSource()==item3)
                string=String.format("field 3 : %s", event.getActionCommand());
            string=String.format("password field is : %s", event.getActionCommand());
    }

If you have an interface that you want to implement from, you have to follow some instructions. One is to implement methods that are mendatory in that interface. Methods that are needed are given a specifc signature, that means they have to have a very specific name and very specific number and sequence of parameters .. Means

actionPerformed(ActionEvent event, String aString) {
    ...
}

would have been wrong, too

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=28184&siteId=1