我的程序现在遇到了很大的麻烦。我不明白这条错误消息“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);
}
}
}