|
-
February 24th, 2011, 07:55 PM
#1
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.");
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|