need some help with this error
i just learned GUI applications in my class but im getting an error i dont know why
Code :
package Chapter7;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class RetalPrice extends JFrame {
int itemCost;
double markupPercentage;
JTextField answer;
JTextField answer2;
JButton button;
public RetalPrice()
{
int width = 400;
int length = 150;
setTitle("Retail Price Calculator");
setSize(width,length);
JLabel question = new JLabel("What is the item's cost");
JLabel question2 = new JLabel("what is the mark up percentage");
answer = new JTextField(10);
answer2 = new JTextField(10);
button = new JButton("calculate");
button.addActionListener(new CalcButtonListener()); //Line where im getting the error
JPanel panel = new JPanel();
panel.add(question);
panel.add(answer);
panel.add(question2);
panel.add(answer2);
panel.add(button);
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int price;
int tax;
double finalPrice;
String input;
price = Integer.parseInt(answer.getText());
tax = Integer.parseInt(answer2.getText());
finalPrice = price + price * (tax/100);
JOptionPane.showMessageDialog(null, "the final price is " + finalPrice);
}
}
public static void main (String[]args)
{
RetalPrice tax = new RetalPrice();
}
}
the error says The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguement RetalPrice.CalcButtonListener)
thank you in advance
Re: need some help with this error
I don't know what the problem is for you... I copied that code and tried it and everything worked fine.
But actually, I did uncover another error. I input 50 for the cost and 50 for the markup percentage, and the result was 50. The problem is in this line:
finalPrice = price + price * (tax/100);
particularly the bolded region.
When working with integers, Java doesn't convert to decimal unless you tell it to. So, for example, if I say
int num = 7 / 3;
Java will assign num's value to 2, even though the exact answer isn't 2. To get Java to preserve decimal results, you have to cast an operation to a double. So, I think if you cast tax to a double before you divide it by 100, then the math will be done correctly.
But back to your first question... Is the code you have above ^ the exact code that you are using? I can't think of any reason that it would work for me and not for you... :(
Re: need some help with this error
mmmm.... i dont know how to fix it then... yeah, its the exact code... maybe is the compiler? im using eclipse 3.6.2.... i copied another code from the book and im still getting the same error in that line
Re: need some help with this error
Hmm... I don't really know why that is causing an issue then. I don't know enough about different IDE's and their compilers...
Perhaps you could ask if Eclipse has certain problems with the addActionListener() method in this forum. Someone's bound to know what the problem is. Sorry I can't help more. D: