简体   繁体   中英

Why am I receiving an Abstract Class error when I press the Applet button?

I am trying to create a button that once pressed erases the old screen and draws something new. I plan on drawing a room then adding a button onto a door. When the button on the door is pressed, it should erase the old screen and draw in a new room.

Why am I receiving this error:

Test is not abstract and does not override abstract method 
actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener

Relevant code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.JComponent.*;

public class Test extends JFrame implements ActionListener{

    private JLabel msg;
    private final String ButtonText = "Room";

    public void init()
    {

        Container contentHolder = getContentPane();

        contentHolder.setLayout(new BorderLayout(18,18));

        msg = new JLabel("");
        contentHolder.add(msg, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();

        JButton myButton = new JButton(ButtonText);

        myButton.addActionListener(this);

        buttonPanel.add(myButton);

        contentHolder.add(buttonPanel, BorderLayout.SOUTH);

    }

    public void actionPerformed(ActionEvent evt, Graphics g)
    {


        String command = evt.getActionCommand();

        if(ButtonText.equals(command))
        {
        Dimension d = getSize();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, d.width, d.height);
        house.drawRoom(g);
        }
    }
}

class house extends Test
{

    public static void drawRoom(Graphics g)
    {
        Expo.drawCircle(g,100,100,25);
    }

}

Your actionPerformed method's signature is

public void actionPerformed(ActionEvent evt, Graphics g)

You have to override:

public void actionPerformed(ActionEvent e)

The reason you are getting the error is because you do not have a method with the signature actionPerformed(java.awt.event.ActionEvent) . The abstract keyword means that the superclass does not define any sort of default implementation for the function, so it is up to you to define it. Otherwise, you need to declare your class abstract, telling any classes that extend your class that they need to implement it.

You do have a method public void actionPerformed(ActionEvent evt, Graphics g) , but that is not the same as public void actionPerformed(ActionEvent evt) . The libraries that incorporate the use of the actionEvent function need to be able to call it with just the event, not the event and some other parameter.

One way to avoid this sort of thing is to put the @Override annotation on each class method you believe should be overriding a superclass or template function. That way, it will tell you that actionPerformed(ActionEvent evt, Graphics g) isn't actually overriding anything.

The previous answers are spot on.

I'd just like to point out that in order to get that exception message at runtime you must have ignored some compilation error messages, and tried to run the Applet anyway.

That is a bad idea ... unless you know what you are doing.

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