当前位置: 首页 > 知识库问答 >
问题:

类不是抽象的,并且不重写抽象方法actionPerformed(ActionEvent)

越正阳
2023-03-14
error: Conn is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
class Conn extends JFrame implements ActionListener 
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;

class Conn extends JFrame implements ActionListener 
{
        JFrame frame = new JFrame("login page");
        JLabel fname,lname,pn;
        JTextField fname1,lname1,pn1;
        JButton sbmt,updt,dlt,slct;

        static Connection conn;

    Conn()
    {
        fname = new JLabel("First name");
        fname.setBounds(5,50,100,20);

        fname1 = new JTextField(10);
        fname1.setBounds(100,50,100,20);

        lname = new JLabel("Last Name");
        lname.setBounds(5, 100, 100,20);

        lname1 = new JTextField(10);
        lname.setBounds(100,100,100,20);

        pn = new JLabel("Phone No.");
       pn.setBounds(50, 50, 50, 50);

        pn1 =  new JTextField(10);
        pn1.setBounds(50,50,50,50);

        sbmt = new JButton("Insert");
        sbmt.setBounds(50,50,50,50);


        add(fname);
        add(fname1);

        add(lname);
        add(lname1);

        add(pn);
        add(pn1);

        add(sbmt);

        sbmt.addActionListener(this);

    }

  public void actionListener(ActionEvent e)
  {
      if (e.getSource()==sbmt)
      {
          System.out.println("sbmt button clicked");
      }
  }

    public static void main(String[] args) {

        try
        {
            Class.forName("oracle.jdbc.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","password");
             conn.close();
        }
        catch(ClassNotFoundException | SQLException e)
                {
                    System.out.println(e);

                }


        Conn con = new Conn();

        con.setSize(400,400);
        con.setLayout(null);
        con.setVisible(true);
    }    
}
/home/jayu/NetBeansProjects/projects/conn/src/conn/Conn.java:5: error: Conn is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
class Conn extends JFrame implements ActionListener 

如何解决此错误,我不想删除implements actionlistener。请任何人在不删除`implements actionlistener的情况下更正此错误。

共有1个答案

柯乐池
2023-03-14

实际上,错误信息说明了一切。在您的示例中,添加了以下方法

public void actionListener(ActionEvent e)

但这实际上应该是:

public void actionPerformed(ActionEvent e)

因为这是您正在实现的接口中的实际方法。

 类似资料: