有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java类型AbstractButton中的方法addActionListener(ActionListener)不适用于参数(对象)

package in.ac.iitb.cfilt.hwnbrowser;

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;


public class WordListBox extends JPanel
    implements ActionListener, WindowListener
{

    public static void showList(String type, String list[], String labelText, String title)
    {
        frame = new JFrame(title);
        frame.setDefaultCloseOperation(2);
        JComponent newContentPane = new WordListBox(type, list, labelText);
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        frame.addWindowListener(new  Object(type)   
    class _anm1 {}

);
        frame.pack();
        frame.setVisible(true);
    }

    public void windowOpened(WindowEvent windowevent)
    {
    }

    public void windowClosing(WindowEvent windowevent)
    {
    }

    public void windowIconified(WindowEvent windowevent)
    {
    }

    public void windowDeiconified(WindowEvent windowevent)
    {
    }

    public void windowActivated(WindowEvent windowevent)
    {
    }

    public void windowDeactivated(WindowEvent windowevent)
    {
    }

    public void windowClosed(WindowEvent windowevent)
    {
    }


}

 /*
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (Object)
    Syntax error, insert ")" to complete MethodInvocation
    Syntax error, insert ";" to complete Statement
    Syntax error on token ")", delete this token
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (Object)
    Syntax error, insert ")" to complete MethodInvocation
    Syntax error, insert ";" to complete Statement
    Syntax error on token ")", delete this token
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (Object)
    Syntax error, insert ";" to complete Statement
    Syntax error, insert ")" to complete MethodInvocation
    Syntax error on token ")", delete this token
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (Object)
    Syntax error, insert ")" to complete MethodInvocation
    Syntax error, insert ";" to complete Statement
    Syntax error on token ")", delete this token
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (Object)
    Syntax error, insert ";" to complete Statement
    Syntax error, insert ")" to complete MethodInvocation
    Syntax error on token ")", delete this token
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (Object)
    Syntax error, insert ";" to complete Statement
    Syntax error, insert ")" to complete MethodInvocation
    Syntax error on token ")", delete this token
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (Object)
    Syntax error, insert ")" to complete MethodInvocation
    Syntax error, insert ";" to complete Statement
    Syntax error on token ")", delete this token
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (Object)
    Syntax error, insert ";" to complete Statement
    Syntax error, insert ")" to complete MethodInvocation
    Syntax error on token ")", delete this token
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (Object)
    Syntax error, insert ")" to complete MethodInvocation
    Syntax error, insert ";" to complete Statement
    Syntax error on token ")", delete this token
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (Object)
    Syntax error, insert ";" to complete Statement
    Syntax error, insert ")" to complete MethodInvocation
    Syntax error on token ")", delete this token
    The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (Object)
    Syntax error, insert ";" to complete Statement
    Syntax error, insert ")" to complete MethodInvocation
    Syntax error on token ")", delete this token

    at in.ac.iitb.cfilt.hwnbrowser.WNBrowserMain.createMenuBar(WNBrowserMain.java:43)
    at in.ac.iitb.cfilt.hwnbrowser.WNBrowserMain.createAndShowGUI(WNBrowserMain.java:571)
    at in.ac.iitb.cfilt.hwnbrowser.WNBrowserMain.main(WNBrowserMain.java:928)*/

共 (1) 个答案

  1. # 1 楼答案

    frame.addWindowListener(new  Object(type)
    

    使用this而不是new Object(),因为addWindowListener需要一个WindowListener参数,并且this引用WindowListene实现的类

    frame.addWindowListener(this);
    

    • 另外,在代码中没有对ActionListener actionPerformed的引用,这是需要的。甚至您将ActionListener注册到任何组件。你没给我们看什么

    • 而且我完全不知道这个class _anm1 {}想做什么。摆脱它,只需像上面指出的那样通过this

    • 您还必须去掉方法签名中的static,因为方法(this)中引用的类不是static。如果您的类中有一堆不必要的static字段和static方法,那么您就有问题了,前面还有一个大的重构(取决于它有多糟糕)

    • difference between instance field and static field in Java上进行谷歌搜索,并阅读出现的一些结果

    • 任何其他问题,我们将不得不看到更多的代码