adding listeners to java applets
Im having trouble compiling this code I wrote it says that i can't add a listener to it. But as far as i know I have add all the proper imports. So could some one look at it and tell me where I'm going wrong.
Here is my code
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class applet3 extends JApplet
{
private Container mycontentpane=getContentPane();
private JPanel mypanel=new JPanel();
private JTextField firstname, lastname, email;
private JButton submit;
private JComboBox protection;
private String protectionlist []={" Select one","antvirse","anti-malware","virse+malware"};
private JTextArea results= new JTextArea();
private String howlong;
private JRadioButton y1,y2,y5,lt;
private ButtonGroup time; // how long they want the protection for varible
private double totalprice=0.0,price=0.0;
private int numyear=0;
public void init()
{
mypanel.setLayout(new GridLayout(11,2));
mypanel.setBackground(Color.GRAY);
// header message
JLabel heading=new JLabel("Order Form");
heading.setFont(new Font("SansSerif",Font.PLAIN,17));
mypanel.add(heading);
//empty cell
JLabel emptycell=new JLabel("");
mypanel.add(emptycell);
//text fields
JLabel fnamelbl= new JLabel("First Name:");
firstname=new JTextField(30);
mypanel.add(fnamelbl);
mypanel.add(firstname);
JLabel lnamelbl= new JLabel("Last Name:");
lastname=new JTextField(30);
mypanel.add(lnamelbl);
mypanel.add(lastname);
JLabel emaillbl= new JLabel("E-mail address:");
email=new JTextField(60);
mypanel.add(emaillbl);
mypanel.add(email);
//drop down list
JLabel protectionlbl=new JLabel("What type of protection do you want?");
mypanel.add(protectionlbl);
protection= new JComboBox(protectionlist);
mypanel.add(protection);
// radiobutton list
JLabel timelbl=new JLabel("How long do you want protection");
mypanel.add(timelbl);
y1=new JRadioButton("1 year",true);
y2=new JRadioButton("2 year");
y5=new JRadioButton("5 year");
lt=new JRadioButton("lifetime");
time.add(y1);
time.add(y2);
time.add(y5);
time.add(lt);
// listerner for radio buttons
y1.addItemListener(this);
y2.addItemListener(this);
y5.addItemListener(this);
lt.addItemListener(this);
mypanel.add(y1);
JLabel emptycell1=new JLabel("");
mypanel.add(emptycell1);
mypanel.add(y2);
JLabel emptycell2=new JLabel("");
mypanel.add(emptycell2);
mypanel.add(y5);
JLabel emptycell3=new JLabel("");
mypanel.add(emptycell3);
mypanel.add(lt);
//results
results.setEditable(false);
mypanel.add(results);
JLabel emptycell4=new JLabel("");
mypanel.add(emptycell4);
JLabel emptycell5=new JLabel("");
mypanel.add(emptycell5);
//submit button
submit=new JButton("submit");
submit.setMnemonic(KeyEvent.VK_S);
mypanel.add(submit);
// action listerner for the button
submit.addActionListener(this);
mycontentpane.add(mypanel);
}
public void itemStateChanged (ItemEvent e)
{
if (y1.isSelected())
{numyear=1;
howlong="1 year";}
else if (y2.isSelected())
{numyear=2;
howlong="2 years";}
else if (y5.isSelected())
{numyear=4;
howlong="5 years";}
else if (lt.isSelected())
{numyear=10;
howlong="life time";}
}
public void actionPerformed(ActionEvent e)
{//find price
if (protection.getSelectedItem().equals("antvirse"))
price=29.99;
else if (protection.getSelectedItem().equals("anti-malware"))
price=24.99;
else if (protection.getSelectedItem().equals("virse+malware"))
price=44.99;
//calcutlaing total price
totalprice=price*numyear;
//displaying results
results.setText(
firstname.getText()+" "+lastname.getText()+", \n"+
"You order the "+ protection.getSelectedItem()+" protection \n"+
"for "+ howlong+" your total comes to $"+totalprice+"\n"+
"A summary email will be sent to "+ email.getText()+" shortly.");
}
}
Re: adding listeners to java applets
If you want help with a Java error, please post the full text of the error message and stack trace if present.
You're trying to add the applet as an ItemListener and an ActionListener, but although it has actioPerformed and itemStateChanged methods, it doesn't implement those interfaces.
Incidentally, I strongly recommend that you follow the Java Coding Conventions - particularly the naming conventions - class names start with an uppercase letter, method and variable names start with a lowercase letter. Java names follow 'camelHump' format where the first letter in each following word in the name is capitalised, e.g. Applet3, myContentName, totalPrice, etc.
These aren't rules - the compiler won't complain if you don't follow them, but they are pretty much universal among Java developers. If you want people to help with your code, or if you're thinking of working in a team at some point, it's advisable to follow the conventions.
Act in haste and repent at leisure; code too soon and debug forever...
R. Kennington