如何解决抽象类错误的问题

时间:2014-01-25 01:20:06

标签: java class user-interface compiler-errors abstract

当我尝试编译以下代码时,它告诉我它不是抽象类,并且不会覆盖抽象方法actionPerformed。我该如何解决这个问题?

以下是我尝试编译的代码:

import java.awt.*;
import java.awt.event.*;
public class FinalProj1 extends Frame implements ActionListener
{
static FinalProj1 objFrame;
static Button objButton1;
static Button objButton2;
static TextField count = new TextField(20);
static TextField count2 = new TextField(20);
static Label objLabel;
static Label objLabel2;

FinalProj1()
{
    setTitle("Click Counter");
    setSize(400,400);
    show();
}
public static void main(String args[])
{   
    objFrame= new FinalProj1();
    objButton1= new Button("Agree");
    objButton2= new Button("Dissagree");
    objLabel= new Label();
    objLabel2= new Label();
    objLabel2.setText("Mexican Food Is Better Than Chineese Food");

    objButton1.setBounds(110,175,75,75);
    objButton2.setBounds(190,175,75,75);
    objLabel2.setBounds(80,95, 250,25);

    objFrame.add(objButton2);
    objFrame.add(objButton1);
    objFrame.add(objLabel2);
    objFrame.add(objLabel);

    objButton1.addActionListener(objFrame);
    objButton2.addActionListener(objFrame);

    int numClicks = 0;
    int numClicks2 = 0;
}
@Override
public void actionPerformed(ActionEvent e);
{
     objButton1.addActionListener(this);
     objButton2.addActionListener(this);
     if(e.getSource()==objButton1)
        {
            numClicks++;
        }
        else
        {
            numClicks2++;
        }
        count.setText("There are " + numClicks + " who agree");
        count2.setText("There are " + numClicks2 + " who dissagree");
    }
}

2 个答案:

答案 0 :(得分:4)

问题在于:

 actionPerformed(ActionEvent);
{
    objButton1.addActionListener(this);
    objButton2.addActionListener(this);
    if(e.getSource()==objButton1)
    {
        numClicks++;
    }
    else
    {
        numClicks2++;
    }
    count.setText("There are " + numClicks + " who agree");
    count2.setText("There are " + numClicks2 + " who dissagree");
}

actionPerformed是ActionListener界面中定义的方法,因为您要实现该接口,您的类必须实现该方法。问题是,您已将方法actionPerformed的主体放在主方法中,并且您尚未将其声明为方法。你应该做的是删除你当前拥有的actionPerformed部分,并在主方法之后将其添加为方法,如下所示:

public void actionPerformed(ActionEvent e)
{
    objButton1.addActionListener(this);
    objButton2.addActionListener(this);
    if(e.getSource()==objButton1)
    {
        numClicks++;
    }
    else
    {
        numClicks2++;
    }
    count.setText("There are " + numClicks + " who agree");
    count2.setText("There are " + numClicks2 + " who dissagree");
}

然后正如Takendarkk所提到的,你必须在主方法中添加这两行:

objButton1.addActionListener(objFrame);
objButton2.addActionListener(objFrame);

现在还有最后一个问题。由于actionPerformed是一个不同的方法,它无法访问main中的变量,所以你应该将它们移到main方法之外,并将它们变成静态变量,如下所示:(把它放在你班级的任何地方,最好是在顶部)

static FinalProj1 objFrame;
static Button objButton1;
static Button objButton2;
static TextField count = new TextField(20);
static TextField count2 = new TextField(20);
static Label objLabel;
static Label objLabel2;

并从main方法中删除这些声明。

答案 1 :(得分:1)

您需要实施此方法。

@Override
public void actionPerformed(ActionEvent e) {
    //Your code here will be performed when an action occurs
}

您还需要将这两行(以及其他对象实例化)移动到构造函数中。

objButton1.addActionListener(this);
objButton2.addActionListener(this);