This is my code :

import java.awt.Button;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

public class SpringSample extends Frame implements ActionListener


    private static final long serialVersionUID = 1L;


         JFrame frame = new JFrame("Login Area!");
            Container contentPane = frame.getContentPane();
            JPanel p = new JPanel(new SpringLayout());
            SpringLayout layout = (new SpringLayout());
            JPanel p1=new JPanel();    

            Component F_namelbl = new JLabel("Firstname: ");
            Component L_namelbl = new JLabel("Lastname: ");
            Component tf_Fname = new JTextField(15);
            Component tf_Lname = new JTextField(15);
            Component Lbl_HW = new JLabel("< Login Area >");
            Component Btn = new JButton("Login");
            Component btn_cancel = new JButton("Cancel");


            layout.putConstraint(SpringLayout.WEST, Lbl_HW, 140, SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, Lbl_HW, 0, SpringLayout.NORTH, contentPane);    
            layout.putConstraint(SpringLayout.WEST, Lbl_HW, 140, SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, Lbl_HW, 0, SpringLayout.NORTH, contentPane);
            layout.putConstraint(SpringLayout.WEST, F_namelbl, 30, SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, F_namelbl, 25, SpringLayout.NORTH, contentPane);
            layout.putConstraint(SpringLayout.WEST, L_namelbl, 30, SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, L_namelbl, 50, SpringLayout.NORTH, contentPane);
            layout.putConstraint(SpringLayout.NORTH, tf_Fname, 25, SpringLayout.NORTH, contentPane);
            layout.putConstraint(SpringLayout.WEST, tf_Fname, 15, SpringLayout.EAST, F_namelbl);
            layout.putConstraint(SpringLayout.NORTH, tf_Lname, 50, SpringLayout.NORTH, contentPane);
            layout.putConstraint(SpringLayout.WEST, tf_Lname, 15, SpringLayout.EAST, F_namelbl);
            layout.putConstraint(SpringLayout.NORTH, Btn, 80, SpringLayout.NORTH, contentPane);
            layout.putConstraint(SpringLayout.WEST, Btn, 110, SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, btn_cancel, 80, SpringLayout.NORTH, p1);
            layout.putConstraint(SpringLayout.WEST, btn_cancel, 200, SpringLayout.WEST, p1);        


            frame.setSize(350, 200);

  public static void main(String args[]) 

    new SpringSample();


 public void actionPerformed(ActionEvent e) 

    System.out.println("Hello There");


Now the red line is under "addActionListener" on


this line

The error is

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method addActionListener(SpringSample) is undefined for the type Component

    at SpringSample.<init>(
    at SpringSample.main(

I think it is not allowing me to set ActionListener to Button "btn_cancel"

Recommended Answers

All 4 Replies

You are mixing AWT components and Swing components - this is a formula for confusion and errors. Unless you have some very special reason you should always use Swing components rather than AWT. The swing components have names that begin with a J (JFrame, JButton etc). The ones without the initial J (Frame, Button etc) are the mostly-obsolete AWT versions.
Component btn_cancel = new JButton("Cancel");
Component class doesn't have action listeners. Why not simply
JButton btn_cancel = new JButton("Cancel");

Ok thanks ...I didn't know that ,and it works fine now.

Thanks a lot.

Glad to help! If you now have the answer to your original problem please mark this thread "solved" and start a new thread if you have a new problem - it helps to keep all this info properly organised a usable for others.

I already did.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.