Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类不是抽象的,并且不重写抽象方法actionPerformed(ActionEvent)_Java_Actionlistener_Connectivity - Fatal编程技术网

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

Java 类不是抽象的,并且不重写抽象方法actionPerformed(ActionEvent),java,actionlistener,connectivity,Java,Actionlistener,Connectivity,我正在尝试将oracle sql与java连接起来,并希望执行一些查询,但我发现了一个错误 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 j

我正在尝试将oracle sql与java连接起来,并希望执行一些查询,但我发现了一个错误

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 
编译器:NetBeans

如何解决此错误,我不想删除
implements ActionListener

。请任何人在不删除“implements ActionListener”的情况下更正此错误。

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

public void actionListener(ActionEvent e)
但这实际上应该是:

public void actionPerformed(ActionEvent e)

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

因为您的类实现了接口

public interface ActionListener extends EventListener {
    void actionPerformed(ActionEvent var1);
}
您需要实现它的方法:

@Override
public void actionPerformed(ActionEvent actionEvent) {...}
我假设此方法的名称错误:

public void actionListener(ActionEvent e){...}

因此,将其重命名为
actionPerformed
,它应该可以工作。

从类中删除ActionListener实现:

class Conn extends JFrame
删除actionListener方法并添加它,而不是sbmt.addActionListener(this)

编辑:

我想知道您不想删除ActionListener实现,所以其他人已经说过您必须更改您的方法:

public void actionListener(ActionEvent e)
{

进入:


如果要保留接口ActionListener,必须实现该方法。 只需将actionListener()的名称切换为actionPerformed()。否则,您也可以保留actionListener()并实现actionPerformed(),这样他就什么也不做了

@Override
public void actionPerformed(ActionEvent e) {}

此外,请始终添加
@Override
注释。除了使代码更具可读性外,如果方法名称不正确,它还会给您带来错误。是的,我在编写方法时犯了错误。谢谢。@helloword如果您的问题得到回答,您能否将其中一个答案标记为已接受?这样其他人就会知道它是一个错误回答:OP要求提供“不删除ActionListener”的解决方案。我完全没有从智能手机中找到这个。既然已经给出了答案,我将把这个留给其他人
@Override
public void actionPerformed(ActionEvent e){
}
@Override
public void actionPerformed(ActionEvent e) {}