Reputation: 107
I'm having trouble with my small applet.
public CdProgram()
{
//Create the four labels
amountL = new JLabel("Enter the amount deposited: ",
SwingConstants.RIGHT);
yearsL = new JLabel("Enter the years: ",
SwingConstants.RIGHT);
interestL = new JLabel("Enter the interest rate: ", SwingConstants.RIGHT);
certificateL = new JLabel("Certificate: ",
SwingConstants.RIGHT);
//Create the four text fields
amountTF = new JTextField(10);
yearsTF = new JTextField(10);
interestTF = new JTextField(10);
certificateTF = new JTextField(10);
//Create Calculate Button
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
//Create Exit Button
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
//Set the title of the window
setTitle("Certificate of amount on maturity");
//Get the container
Container pane = getContentPane();
//Set the layout
pane.setLayout(new GridLayout(5, 2));
//Place the components in the pane
pane.add(amountL);
pane.add(amountTF);
pane.add(yearsL);
pane.add(yearsTF);
pane.add(interestL);
pane.add(interestTF);
pane.add(certificateL);
pane.add(certificateTF);
pane.add(calculateB);
pane.add(exitB);
//Set the size of the window and display it
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
CdProgram CertObjt = new CdProgram();
}
}
private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double amount, years, interst, certificate;
amount = Double.parseDouble(amountTF.getText());
years = Double.parseDouble(yearsTF.getText());
interest = Double.parseDouble(interestTF.getText());
certificate = amount * Math.pow(1 + rate/100, years);
certificateTF.setText("" + certificate);
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
My sub classes are not recognizing when i implement the action listener feature so i keep getting errors like:
CalculateButtonHandler.java:1: error: cannot find symbol
private class CalculateButtonHandler implements ActionListener
CalculateButtonHandler.java:3: error: cannot find symbol
public void actionPerformed(ActionEvent e)
CdProgram.java:39: error: method addActionListener in class AbstractButton cannot be applied to given types;
calculateB.addActionListener(cbHandler);
^
required: ActionListener
found: CalculateButtonHandler
Im not quite sure what I am doing wrong here. Thanks for the help.
Upvotes: 1
Views: 144
Reputation: 3749
i think you have to make your sub classes inner classes, like that they can talk directly with your principal classe, and change attributes with each other.
so try this and see:
alculateB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
double amount, years, interst, certificate;
amount = Double.parseDouble(amountTF.getText());
years = Double.parseDouble(yearsTF.getText());
interest = Double.parseDouble(interestTF.getText());
certificate = amount * Math.pow(1 + rate/100, years);
certificateTF.setText("" + certificate);
}
});
exitB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
let us know if it's working ;)
Upvotes: 2