2014-01-25 118 views
0

当我尝试编译下面的代码时,它告诉我它不是抽象类,并且不重写抽象方法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"); 
    } 
} 
+1

'不重写抽象方法actionPerformed' ... –

+1

如果你不打算实现它的方法,你为什么让你的类实现'ActionListener'? –

+0

'actionPerformed(ActionEvent);'现在你正试图调用这个方法,然后你只需要一段代码。 2不像你认为的那样链接。 (我认为 :))。 – csmckelvey

回答

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的主体放入你的main方法中,并且你没有将它声明为方法。你应该做的就是删除您目前拥有的actionPerformed一部分,它的主要方法之后添加的方法,像这样:

​​

,然后作为Takendarkk提到的,你必须将这两种线在你的主要方法中添加:

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

现在还有最后一个问题。由于actionPerformed是一种不同的方法,因此它将无法访问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; 

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

+0

太棒了,非常感谢! – user3233707

+0

没问题,很高兴我能帮到:) –

+0

这个答案好多了。我在我的手机上,不得不简短:) – csmckelvey

1

你需要实现这个方法。

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

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

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

我知道这不是一个完整的解决方案,但它应该有所帮助。整个给男人一条鱼的东西... – csmckelvey

+1

'这'在一个静态的方法?真? –