Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: actionListner code doesn't work ????

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default actionListner code doesn't work ????

    hi everyone
    I am still new at learning java language
    and I use eclipse program
    but when I run this code it doesn't work

    the code is :
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.FlowLayout;
    import java.awt.Color;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    public class coloredPanels extends JFrame implements ActionListener
    {
     
    	private int width = 300;
    	private int height = 200;
     
    	private JPanel greenPanel;
    	private JPanel whitePanel;
    	private JPanel grayPanel;
     
     
    	public static void main(String[] args){
    		coloredPanels gui =new coloredPanels();
    		gui.setVisible(true);		
    	}
     
     
    	public coloredPanels()
    	{
     
    		//we created the frame of the GUI
    	super("Panel for 3 colors");
    	setSize(width,height);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	setLayout(new BorderLayout());
     
    	//we created the biggest panel that has the other panels of each colored bars
    	JPanel biggerPanel = new JPanel();
    	biggerPanel.setLayout(new GridLayout(1,3));
     
    	//the first panel has created
    	greenPanel  = new JPanel();
    	greenPanel.setBackground(Color.LIGHT_GRAY);
    	biggerPanel.add(greenPanel);
     
     
    	//the second panel has created
    	whitePanel  = new JPanel();
    	whitePanel.setBackground(Color.LIGHT_GRAY);
    	biggerPanel.add(whitePanel);
     
    	//the third panel has created
    	grayPanel  = new JPanel();
    	grayPanel.setBackground(Color.LIGHT_GRAY);
    	biggerPanel.add(grayPanel);
     
    	//place the biggest panel with the 3 other panels at the center of the screen
    	add(biggerPanel,BorderLayout.CENTER);
     
    	//now we created panel for 3 buttons
    	JPanel buttonPanel = new JPanel();
    	buttonPanel.setBackground(Color.LIGHT_GRAY);
    	buttonPanel.setLayout(new FlowLayout());
     
    	JButton greenButton = new JButton("Green");
    	greenButton.setBackground(Color.GREEN);
    	greenButton.addActionlistner(this);
    	buttonPanel.add(greenButton);
     
    	JButton whiteButton = new JButton("White");
    	whiteButton.setBackground(Color.WHITE);
    	whiteButton.addActionlistner(this);
    	buttonPanel.add(whiteButton);
     
     
    	JButton grayButton = new JButton("Gray");
    	grayButton.setBackground(Color.GRAY);
    	grayButton.addActionlistner(this);
    	buttonPanel.add(grayButton);
     
     
    	add(buttonPanel,BorderLayout.NORTH);
    	}
     
    public void actionPerformed(ActionEvent e){
    	String buttonText = e.getActionCommand();
     
    	if(buttonText.equalsIgnoreCase("Green"))
    		greenPanel.setBackground(Color.GREEN);
    	else if(buttonText.equalsIgnoreCase("White"))
    		whitePanel.setBackground(Color.WHITE);
    	else if(buttonText.equalsIgnoreCase("Gray"))
    		greenPanel.setBackground(Color.GRAY);
    	else
    		System.out.println("error");
    }
     
    }


    and the errors are :
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method addActionlistner(coloredPanels) is undefined for the type JButton
    The method addActionlistner(coloredPanels) is undefined for the type JButton
    The method addActionlistner(coloredPanels) is undefined for the type JButton

    at coloredPanels.<init>(coloredPanels.java:67)
    at coloredPanels.main(coloredPanels.java:23)


    =======

    if you plz help me


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: actionListner code doesn't work ????

    addActionlistner
    check your spelling against the names of the methods in the API doc for JButton

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: actionListner code doesn't work ????

    Quote Originally Posted by Sean4u View Post
    check your spelling against the names of the methods in the API doc for JButton
    thank you
    but how can I check them ??

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: actionListner code doesn't work ????

    but how can I check them ??
    JButton (Java Platform SE 7 )
    ...copy...paste
    addActionListener
    Your post
    ...copy...paste
    addActionlistner
    ...edit a bit to get one above the other...
    addActionListener
    addActionlistner
    Compare the two strings of characters one-character-at-a-time, like a computer would. Are they the same?

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: actionListner code doesn't work ????

    Quote Originally Posted by Sean4u View Post
    JButton (Java Platform SE 7 )
    ...copy...paste

    Your post
    ...copy...paste

    ...edit a bit to get one above the other...



    Compare the two strings of characters one-character-at-a-time, like a computer would. Are they the same?
    I will try
    thanks alot

Similar Threads

  1. HI, could someone please tell me why my code doesnt work?
    By joelmeler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 3rd, 2011, 02:37 AM
  2. Scanner code won't work
    By r19ecua in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 21st, 2011, 05:49 PM
  3. Timer code does not work!
    By mariapatrawala in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2010, 10:03 AM
  4. WHY this code dont work?
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: December 9th, 2010, 10:47 AM
  5. please tell me why this code does not work
    By amr in forum Java Theory & Questions
    Replies: 9
    Last Post: December 6th, 2010, 06:46 PM