我的程序现在遇到了很大麻烦。我不明白这个错误消息“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);
}
}
}
答案 0 :(得分:2)
通常,当一个(非抽象)类implements
接口时,会签订一个契约,建议该类实现接口中提供的所有方法。
您的课程MyEventA
implements
界面ActionListener
,需要覆盖方法(void actionPerformed(ActionEvent)
)。由于MyEventA
不是抽象的,编译器告诉您需要在此处实现该方法。
如果您想知道如何实现该方法,那么您在实现的子类(ButtonListener
)中有一个很好的示例。在这里,您可以看到ButtonListener
实现了相同的接口ActionListener
,并实现了它的方法。
答案 1 :(得分:1)
如果要实现ActionListener接口,则必须覆盖actionPerformed(java.awt.event.ActionEvent)方法。如果未覆盖接口中的方法,编译器会要求将类声明为abstarct。
答案 2 :(得分:1)
因为MyEventA
已经实现了ActionListener
,并且因为它不是一个抽象类,所以必须以与actionPeformed (ActionEvent)
中相同的方式实现/定义方法ButtonListener
但我认为你想要做的事实上没有MyEventA
实施ActionListener
,而是将new ButtonListener ()
传递给你的按钮,如
plus.addListener (new ButtonListener ());
答案 3 :(得分: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);
}
}
如果您想使用actionPerformed
课程中的ButtonListener
,请使用以下代码:
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);
}
}