0

我的程序现在遇到了很大的麻烦。我不明白这条错误消息“MyEventA 不是抽象的,并且不会覆盖 java.awt.event.ActionListener 中的抽象方法 actionPerformed(java.awt.event.actionEvent)。” 我曾尝试在网上和教科书中查找,但仍然无法正常工作。

如果有人能帮助我找出我的代码有什么问题,我将不胜感激。我已经为此工作了一个半小时,但仍然没有解决它。:( 提前致谢!

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

public class MyEventA extends JFrame implements ActionListener
{
    private JButton plus;
    private JButton minus;
    private JButton reset;
    private JButton quit;

    public MyEventA()
{
    add(new JLabel("Current Value", JLabel.LEFT), BorderLayout.NORTH);
    JTextField jtfCurrent = new JTextField("0");
    add(jtfCurrent, BorderLayout.EAST);
    JPanel jpSouth = new JPanel();
    plus = new JButton("+");
    minus = new JButton("-");
    reset = new JButton("Reset");
    quit = new JButton("Quit");
    ButtonListener b1 = new ButtonListener(jtfCurrent);
    jtfCurrent.addActionListener(b1);
    plus.addActionListener(this);
    minus.addActionListener(this);
    reset.addActionListener(this);
    quit.addActionListener(this);
    jpSouth.add(plus);
    jpSouth.add(minus);
    jpSouth.add(reset);
    jpSouth.add(quit);
    add(jpSouth, BorderLayout.SOUTH);
}

class ButtonListener implements ActionListener {
    private JTextField writeInto;   // text field reference
    private int count = 0;

    public ButtonListener(JTextField tf) {
        writeInto = tf;
        count = 0;
    }

    public void actionPerformed(ActionEvent ae){
        if(ae.getActionCommand().equals("+")){
            count++;
            writeInto.setText("" + count);
        }
        else if(ae.getActionCommand().equals("-")){
            count--;
            writeInto.setText("" + count);
        }
        else if(ae.getActionCommand().equals("Reset")){
            count = 0;
            writeInto.setText("" + count);
        }
        else {
            System.exit(0);    
        }
    }

    public static void main(String[] args){
        MyEventA events = new MyEventA();
        events.setTitle("Part 2 Using getSource");
        events.pack();
        events.setDefaultCloseOperation(EXIT_ON_CLOSE);
        events.setSize(300,100);
        events.setLocation(200,200);
        events.setVisible(true);
    }
}
}
4

4 回答 4

2

一般来说,当一个(非抽象)类implements是一个接口时,会制定一个约定,建议这个类将实现接口中提供的所有方法。

你的类MyEventA implements接口ActionListener,它需要一个方法被覆盖(void actionPerformed(ActionEvent))。由于MyEventA不是抽象的,编译器告诉您需要在此处实现该方法。

如果您想知道如何实现该方法,那么您在实现的子类 ( ButtonListener) 中有一个很好的示例。在这里您可以看到ButtonListener实现了相同的接口ActionListener,并实现了它的方法。

于 2013-04-08T03:52:29.393 回答
1

因为MyEventA已经实现ActionListener并且因为它不是抽象类,所以它必须actionPeformed (ActionEvent)以与您在ButtonListener

但是我认为您想要做的实际上是没有MyEventA实现ActionListener,而是将 a 传递new ButtonListener ()给您的按钮,例如

plus.addListener (new ButtonListener ());
于 2013-04-08T04:03:34.597 回答
1

如果要实现 ActionListener 接口,则必须重写 actionPerformed(java.awt.event.ActionEvent) 方法。如果接口中的方法未被重写,编译器会要求将类声明为抽象类。

于 2013-04-08T03:53:56.363 回答
1

重写如下,它适用于我:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class MyEventA extends JFrame implements ActionListener {
    private JButton plus;
    private JButton minus;
    private JButton reset;
    private JButton quit;

    public MyEventA() {
        add(new JLabel("Current Value", JLabel.LEFT), BorderLayout.NORTH);
        JTextField jtfCurrent = new JTextField("0");
        add(jtfCurrent, BorderLayout.EAST);
        JPanel jpSouth = new JPanel();
        plus = new JButton("+");
        minus = new JButton("-");
        reset = new JButton("Reset");
        quit = new JButton("Quit");
        ButtonListener b1 = new ButtonListener(jtfCurrent);
        jtfCurrent.addActionListener(b1);
        plus.addActionListener(this);
        minus.addActionListener(this);
        reset.addActionListener(this);
        quit.addActionListener(this);
        jpSouth.add(plus);
        jpSouth.add(minus);
        jpSouth.add(reset);
        jpSouth.add(quit);
        add(jpSouth, BorderLayout.SOUTH);
    }

    class ButtonListener implements ActionListener {
        private JTextField writeInto; // text field reference
        private int count = 0;

        public ButtonListener(JTextField tf) {
            writeInto = tf;
            count = 0;
        }

        public void actionPerformed(ActionEvent ae) {
            if (ae.getActionCommand().equals("+")) {
                count++;
                writeInto.setText("" + count);
            } else if (ae.getActionCommand().equals("-")) {
                count--;
                writeInto.setText("" + count);
            } else if (ae.getActionCommand().equals("Reset")) {
                count = 0;
                writeInto.setText("" + count);
            } else {
                System.exit(0);
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {// was to be implemented

        javax.swing.JOptionPane.showMessageDialog(null, "clicked");

    }

    public static void main(String[] args) {// main method should be in the outer class
        MyEventA events = new MyEventA();
        events.setTitle("Part 2 Using getSource");
        events.pack();
        events.setDefaultCloseOperation(EXIT_ON_CLOSE);
        events.setSize(300, 100);
        events.setLocation(200, 200);
        events.setVisible(true);
    }

}

如果要使用actionPerformedinButtonListener类,请使用以下代码:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class MyEventA extends JFrame implements ActionListener {
    private JButton plus;
    private JButton minus;
    private JButton reset;
    private JButton quit;   


    public MyEventA() {
        add(new JLabel("Current Value", JLabel.LEFT), BorderLayout.NORTH);
        JTextField jtfCurrent = new JTextField("0");
        add(jtfCurrent, BorderLayout.EAST);
        JPanel jpSouth = new JPanel();
        plus = new JButton("+");
        minus = new JButton("-");
        reset = new JButton("Reset");
        quit = new JButton("Quit");     


        ButtonListener b1 = new ButtonListener(jtfCurrent);
        jtfCurrent.addActionListener(b1);
        plus.addActionListener(new ButtonListener(jtfCurrent));
        minus.addActionListener(new ButtonListener(jtfCurrent));
        reset.addActionListener(new ButtonListener(jtfCurrent));
        quit.addActionListener(new ButtonListener(jtfCurrent));
        jpSouth.add(plus);
        jpSouth.add(minus);
        jpSouth.add(reset);
        jpSouth.add(quit);
        add(jpSouth, BorderLayout.SOUTH);
    }

    class ButtonListener implements ActionListener {
        private JTextField writeInto; // text field reference
        private int count = 0;
        public ButtonListener() {

        }
        public ButtonListener(JTextField tf) {
            writeInto = tf;
            count = 0;
        }

        public void actionPerformed(ActionEvent ae) {
            if (ae.getActionCommand().equals("+")) {
                count++;
                writeInto.setText("" + count);
            } else if (ae.getActionCommand().equals("-")) {
                count--;
                writeInto.setText("" + count);
            } else if (ae.getActionCommand().equals("Reset")) {
                count = 0;
                writeInto.setText("" + count);
            } else {
                System.exit(0);
            }
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        javax.swing.JOptionPane.showMessageDialog(null, "clicked");

    }

    public static void main(String[] args) {
        MyEventA events = new MyEventA();
        events.setTitle("Part 2 Using getSource");
        events.pack();
        events.setDefaultCloseOperation(EXIT_ON_CLOSE);
        events.setSize(300, 100);
        events.setLocation(200, 200);
        events.setVisible(true);
    }

}
于 2013-04-08T04:14:17.433 回答