hello, i need help with this code, i dont know why i am getting this error
Code java:
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
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.
Re: hello, i need help with this code, i dont know why i am getting this error
Code :
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
Re: hello, i need help with this code, i dont know why i am getting this error
like for me this code is correct...
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.