使用TextFieldHandler和ButtonHandler的java出错

时间:2015-12-29 12:26:45

标签: java

当我编译我的代码时,我得到以下2个错误: 1)LabelFrame.TextFieldHandler不是抽象的,并且不会在ActionListener中覆盖抽象方法actionPerformed(ActionEvent) 2)方法不会覆盖或实现超类型@Override

的方法

同样的错误适用于ButtonHandler类。 你能解释我的错误吗?我该如何解决? 提前谢谢!

这是我的代码:

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.util.*;

public class LabelFrame extends JFrame {
    private final JTextField url;
    private final JButton backButton;
    private final JButton loadButton;
    private Stack urlStack = new Stack();

    public static void main(String[] args) {
        LabelFrame frame = new LabelFrame();
        frame.show();
    }


    public LabelFrame() {
        super("WebStalker");
        setLayout(new FlowLayout());

        url = new JTextField(30);
        add(url);
        loadButton = new JButton("Load");
        add(loadButton);
        backButton = new JButton("Back");
        add(backButton);

        TextFieldHandler tHandler = new TextFieldHandler();
        ButtonHandler bHandler = new ButtonHandler();
        url.addActionListener(tHandler);
        backButton.addActionListener(bHandler);
        loadButton.addActionListener(bHandler);
   }

    private class TextFieldHandler implements ActionListener {
        @Override
        public void actionPerfomed(ActionEvent event) {
             String urlString = "";
             urlString = event.getActionCommand();
        }
     }

     private class ButtonHandler implements ActionListener {
        @Override
         public void actionPerfomed(ActionEvent event) {
             if (event.getSource() == loadButton) {
                 try {
                     //remember url for back button
                     urlStack.push(url.getText());
                 } catch (Exception e) {
                     System.out.println("Unable to load page");
                 }
             } else if (event.getSource() == backButton) {
                 if (urlStack.size() <= 1) {
                     return;
                 }
                 try {
                     urlStack.pop();
                     String urlString = (String)urlStack.peek();
                 } catch (Exception e) {
                    System.out.println("Unable to load page");
                 }
             }
         }
     }
}

2 个答案:

答案 0 :(得分:0)

您在TextFieldHandlerButtonHandler类中重写的方法名称中有拼写错误

将方法名称从actionPerfomed更新为actionPerformed

答案 1 :(得分:0)

检查拼写:

您的代码:

private class TextFieldHandler implements ActionListener {
    @Override
    public void actionPerfomed(ActionEvent event) {
        //Your code
    }
}

应该是:

private class TextFieldHandler implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        //Your code
    }
}

您拼写actionPerfomed而不是actionPerformed。那应该解决它