1 Replies - 449 Views - Last Post: 12 April 2013 - 07:35 AM Rate Topic: -----

#1 chdboy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 26-July 12

The method addActionListener(SpringSample) is undefined for the type

Posted 12 April 2013 - 07:03 AM

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;
	
	SpringSample()
	{
		
		 JFrame frame = new JFrame("Login Area!");
		    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		    Container contentPane = frame.getContentPane();
		    JPanel p = new JPanel(new SpringLayout());
		    SpringLayout layout = (new SpringLayout());
		    contentPane.setLayout(layout);		   
		    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");
		    
		    
		    frame.add(p1);
		    contentPane.add(F_namelbl);
		    contentPane.add(L_namelbl);
		    contentPane.add(tf_Fname);
		    contentPane.add(tf_Lname);
		    contentPane.add(Lbl_HW);
		    contentPane.add(Btn);
		    contentPane.add(btn_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);	    
		    
		    
		    btn_cancel.addActionListener(this);
		    
		   
		    
		    frame.pack();
		    frame.setSize(350, 200);
		    frame.setVisible(true);
		    
		
		
	}
  public static void main(String args[]) 
  {	 
   
    new SpringSample();
    
  }
  

 public void actionPerformed(ActionEvent e) 
{
	
	System.out.println("Hello There");

}
}


I got a red line under "addActionListener"
btn_cancel.addActionListener(this);


And error

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

	at SpringSample.<init>(SpringSample.java:81)
	at SpringSample.main(SpringSample.java:96)


Is This A Good Question/Topic? 0
  • +

Replies To: The method addActionListener(SpringSample) is undefined for the type

#2 chdboy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 26-July 12

Re: The method addActionListener(SpringSample) is undefined for the type

Posted 12 April 2013 - 07:35 AM

I think I got my answer
Component btn_cancel = new JButton("Cancel");

I was using obsolete AWT versions.
I should have used
JButton btn_cancel = new JButton("Cancel");

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1