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: hello, i need help with this code, i dont know why i am getting this error

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default hello, i need help with this code, i dont know why i am getting this error

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class RetailPrice extends JFrame{
     
    	private JPanel panel;
    	private JPanel panel2;
    	private JTextField text;
    	private JLabel label;
    	private JTextField text2;
    	private JLabel label2;
    	private JButton calculate;
    	private int width = 350;
    	private int heigth = 150;
     
    	public RetailPrice()
    	{
    		setTitle("retail price");
    		setSize(width,heigth);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		buildPanel();
     
    		add(panel);
     
     
     
    		setVisible(true);
     
    	}
     
    	public void buildPanel()
    	{
    		label = new JLabel("What is the price of the sale?");
    		text = new JTextField(10);
     
    		label2 = new JLabel("What is the mark up price?");
    		text2 = new JTextField(10);
     
    		calculate = new JButton("Calculate");
     
    		CalculateListener listener = new CalculateListener();
    		calculate.addActionListener(listener);             //*****this is the line im getting the error**** it says The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments 		
     
                             panel = new JPanel();
     
    		panel.add(label);
    		panel.add(text);
    		panel.add(label2);
    		panel.add(text2);
    		panel.add(calculate);
    	}
     
    	private class CalculateListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			String input;
    			int cost;
    			double perc, add, finalPrice;
     
    			input = text.getText();
    			cost = Integer.parseInt(input);
     
    			input = text2.getText();
    			perc = Integer.parseInt(input)/100f;
     
    			finalPrice = cost + (cost * perc);
    			JOptionPane.showMessageDialog(null, "the final price is "+ finalPrice);
     
     
    		}
    	}
    }

    im learning how to handle events with action listeners, thank you
    Last edited by KevinWorkman; July 25th, 2011 at 01:19 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: hello, i need help with this code, i dont know why i am getting this error

    Hmm, seems to compile fine for me.

    PS- You forgot the highlight tags. I added them for you this time.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    tpopono (July 25th, 2011)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: hello, i need help with this code, i dont know why i am getting this error

    import java.awt.event.*;
    public interface ActionListener {
     
    	public void actionPerformed(ActionEvent e);
     
    }
    maybe its the interface?? i dont know why i does not let me run it, im using eclipse

  5. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: hello, i need help with this code, i dont know why i am getting this error

    like for me this code is correct...

  6. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: hello, i need help with this code, i dont know why i am getting this error

    Have you written your own ActionListener interface? If so then don't. You are trying to add your own Listener to the button but it is expecting an Listener from the awt.event package to be added. Just because you import this package into your ActionListener interface does not mean that your ActionListener is a java.awt.event.ActionListener. They are quite different.
    Improving the world one idiot at a time!

Similar Threads

  1. My code have a simple error but i dont know how to solve it !!!
    By Blackhawk1993 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 17th, 2011, 02:12 PM
  2. Missing a line or two of code,b ut dont know what it is.
    By backdown in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 20th, 2011, 09:13 AM
  3. i'm getting this error, dont know why? please help
    By amr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 17th, 2010, 06:14 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. [SOLVED] I dont know how to complete this code?
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 21st, 2010, 10:07 AM