See the code below:
Sometimes it happens that we override the abstract method with wrong name spelling or wrong signature, then also we see the same error message. Note that Java is a case sensitive language; most often we write the method name with wrong case (example, actionperformed/ActionPerformed instead of actionPerformed) and see this error message.
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyFrame extends JFrame implements ActionListener{
private JButton exitButton = new JButton("Exit");
public MyFrame()
{
Container container = getContentPane();
container.setLayout(new FlowLayout());
container.add(exitButton);
exitButton.addActionListener(this);
setSize(300,200);
}
}
If this code is compiled, we will see the error "MyFrame is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener"
Why? When we implement an interface(ActionListener in this example) or extend an abstract class in a concrete class, we must override the abstract method(s). In the above example we have implemented ActionListener but have not overridden the abstract method actionPerformed.
The correct code of the above is:
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyFrame extends JFrame implements ActionListener
{
private JButton exitButton = new JButton("Exit");
public MyFrame()
{
Container container = getContentPane();
container.setLayout(new FlowLayout());
container.add(exitButton);
exitButton.addActionListener(this);
setSize(300,200);
}
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
}
Sometimes it happens that we override the abstract method with wrong name spelling or wrong signature, then also we see the same error message. Note that Java is a case sensitive language; most often we write the method name with wrong case (example, actionperformed/ActionPerformed instead of actionPerformed) and see this error message.
For implementing an interface or extending an abstract class, we must override all of the abstract methods (if more than one), though we do not need all. For example, if we implement KeyListener we must override each of the three abstract methods keyPressed, keyTyped and keyReleased though we may not need all of them. In that case, method body can be kept blank. Instead of interface, we can use adapter class (if available) to get rid of overriding all abstract methods.
In summary, override the abstract method with correct name spelling and signature to remove the error.
Muchas gracias, me sirvió mucho.
ReplyDeleteimport java.awt.event.*;
ReplyDeleteimport javax.swing.*;
import java.applet.*;
import java.io.*;
public class regform extends JFrame implements ActionListener
{
JLabel j1,j2,j3,j4,j5,j6,j7;
JTextField t1,t2,t3,t4,t5;
JButton b1;
JRadioButton r1,r2;
ButtonGroup bg;
public void regform()
{
setBackground(Color.PINK);
Container cn= getContentPane();
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(15,3));
cn.setLayout(new FlowLayout());
j1=new JLabel("REGISTRATION FORM",Label.CENTER);
j1.setForeground(Color.red);
t1= new JTextField(15);
jp.add(new JLabel());
jp.add(new JLabel());
jp.add(j1);
jp.add(t1);
j2=new JLabel("NAME*:");
j2.setForeground(Color.red);
t2=new JTextField(15);
jp.add(j2);
jp.add(t2);
j3=new JLabel("ADDRESS*:");
j3.setForeground(Color.red);
t3=new JTextField(15);
jp.add(j3);
jp.add(t3);
j4=new JLabel("contact*:");
j4.setForeground(Color.red);
t4=new JTextField(15);
jp.add(j4);
jp.add(t4);
j5=new JLabel("GENGER*:");
j5.setForeground(Color.red);
r1=new JRadioButton("Male");
r2=new JRadioButton("Female");
r1.addActionListener(this);
r2.addActionListener(this);
JPanel jp1=new JPanel();
jp1.add(r1);
jp1.add(r2);
jp.add(jp1);
bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
j6=new JLabel("email id*:");
j6.setForeground(Color.red);
t5=new JTextField(15);
jp.add(j6);
jp.add(t5);
JPanel jp2=new JPanel();
b1=new JButton("SAVE");
jp.add(new JLabel());
jp.add(new JLabel());
j7=new JLabel("* marked fields are mandotory");
jp2.add(b1);
jp.add(jp2);
b1.addActionListener(this);
b1.setActionCommand("SAVE");
jp.setBorder(BorderFactory.createTitledBorder("REGISTRATION FORM"));
cn.add(jp);
cn.setBackground(Color.pink);
setVisible(true);
setSize(800,700);
this.setLocation(250,5);
setResizable(false);
pack();
}
public void ActionPerformed(ActionEvent ae)
{
String s="";
if(ae.getActionCommand().equals("SAVE"))
{
s+=j1.getText();
s+=t1.getText();
s+="\n";
s+=j2.getText();
s+=t2.getText();
s+="\n";
s+=j3.getText();
s+=t3.getText();
s+="\n";
s+=j4.getText();
s+=t4.getText();
s+="\n";
s+=j5.getText();
if(r1.isSelected())
s+=r1.getText();
if(r2.isSelected())
s+=r2.getText();
s+="\n";
s+=j6.getText();
s+="\n";
s+=j7.getText();
s+="\n";
}
}
public static void main(String args[])
{
new regform ();
}
ActionListener is abstract and does not override abstract method actionPerformed —
ReplyDeleteimport java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.io.*;
public class regform extends JFrame implements ActionListener
{
JLabel j1,j2,j3,j4,j5,j6,j7;
JTextField t1,t2,t3,t4,t5;
JButton b1;
JRadioButton r1,r2;
ButtonGroup bg;
public void regform()
{
setBackground(Color.PINK);
Container cn= getContentPane();
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(15,3));
cn.setLayout(new FlowLayout());
j1=new JLabel("REGISTRATION FORM",Label.CENTER);
j1.setForeground(Color.red);
t1= new JTextField(15);
jp.add(new JLabel());
jp.add(new JLabel());
jp.add(j1);
jp.add(t1);
j2=new JLabel("NAME*:");
j2.setForeground(Color.red);
t2=new JTextField(15);
jp.add(j2);
jp.add(t2);
j3=new JLabel("ADDRESS*:");
j3.setForeground(Color.red);
t3=new JTextField(15);
jp.add(j3);
jp.add(t3);
j4=new JLabel("contact*:");
j4.setForeground(Color.red);
t4=new JTextField(15);
jp.add(j4);
jp.add(t4);
j5=new JLabel("GENGER*:");
j5.setForeground(Color.red);
r1=new JRadioButton("Male");
r2=new JRadioButton("Female");
r1.addActionListener(this);
r2.addActionListener(this);
JPanel jp1=new JPanel();
jp1.add(r1);
jp1.add(r2);
jp.add(jp1);
bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
j6=new JLabel("email id*:");
j6.setForeground(Color.red);
t5=new JTextField(15);
jp.add(j6);
jp.add(t5);
JPanel jp2=new JPanel();
b1=new JButton("SAVE");
jp.add(new JLabel());
jp.add(new JLabel());
j7=new JLabel("* marked fields are mandotory");
jp2.add(b1);
jp.add(jp2);
b1.addActionListener(this);
b1.setActionCommand("SAVE");
jp.setBorder(BorderFactory.createTitledBorder("REGISTRATION FORM"));
cn.add(jp);
cn.setBackground(Color.pink);
setVisible(true);
setSize(800,700);
this.setLocation(250,5);
setResizable(false);
pack();
}
public void ActionPerformed(ActionEvent ae)
{
String s="";
if(ae.getActionCommand().equals("SAVE"))
{
s+=j1.getText();
s+=t1.getText();
s+="\n";
s+=j2.getText();
s+=t2.getText();
s+="\n";
s+=j3.getText();
s+=t3.getText();
s+="\n";
s+=j4.getText();
s+=t4.getText();
s+="\n";
s+=j5.getText();
if(r1.isSelected())
s+=r1.getText();
if(r2.isSelected())
s+=r2.getText();
s+="\n";
s+=j6.getText();
s+="\n";
s+=j7.getText();
s+="\n";
}
}
public static void main(String args[])
{
new regform ();
}
In the above code, the method name would be actionPerformed, not ActionPerformed. Start this method name with lower case "a" instead of upper case "A".
ReplyDeleteYou were right i misspelled: put ActionPerformed instead of actionPerformed, THANKS!
ReplyDelete