| | |
Cannot figure out why I'm getting these Errors
![]() |
•
•
Join Date: Apr 2004
Posts: 5
Reputation:
Solved Threads: 0
my code is posted below the program is to be very simple just just a button a choice box a text box and a canvas the user is to be able to select what item they want and then the price appears in the label....then the user is able to put in a quantity and when they hit order the program should calculate the price and display it nice and neat in the canvas....I'm new to Java and ...if this were C++ I could do it in an hour I just don't understand why it's not working...please someone help
thank you
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class grocerystore extends Applet{
private Button order = new Button("Order");
private Label price = new Label("Your price will appear here");
private Choice food = new Choice();
private TextField quantity = new TextField();
double quantitySelected;
double eggs = 1.90;
double milk = 1.47;
double bread = 2.12;
double total;
display output = new display();
public void init() {
add(order);
add(price);
add(quantity);
add(food);
food.add("Select your food");
food.add("Eggs");
food.add("Milk");
food.add("Bread");
order.addActionListener(this);
food.addItemListener(this);
add(output);
output.setSize(250,100);
order.addActionListener(this)
food.addActionListener(this)
}
class display extends Canvas
implements ActionListener, ItemListener {
int itemSelected = food.getSelectedIndex();
public void actionPerformed(ActionEvent action) {
quantitySelected = new Double(quantity.getText()).doubleValue();
if (itemSelected==1)
total = quantitySelected * eggs;
if (itemSelected==2)
total = quantitySelected * milk;
if (itemSelected==3)
total = quantitySelected * bread;
}
public void itemStateChanged(ItemEvent event) {
if (itemSelected==1)
price.setText("The price of a dozen eggs is " + eggs);
if (itemSelected==2)
price.setText("The price of a quart of milk is " + milk);
if (itemSelected==3)
price.setText("The price of a loaf of bread is " + bread);
}
public void paint(Graphics g){
if (itemSelected==1){
g.drawString("You purchased " + quantitySelected + "dozen eggs",20,20);
g.drawString("for a total of " + total,20,30);
}
}
}
}
thank you
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class grocerystore extends Applet{
private Button order = new Button("Order");
private Label price = new Label("Your price will appear here");
private Choice food = new Choice();
private TextField quantity = new TextField();
double quantitySelected;
double eggs = 1.90;
double milk = 1.47;
double bread = 2.12;
double total;
display output = new display();
public void init() {
add(order);
add(price);
add(quantity);
add(food);
food.add("Select your food");
food.add("Eggs");
food.add("Milk");
food.add("Bread");
order.addActionListener(this);
food.addItemListener(this);
add(output);
output.setSize(250,100);
order.addActionListener(this)
food.addActionListener(this)
}
class display extends Canvas
implements ActionListener, ItemListener {
int itemSelected = food.getSelectedIndex();
public void actionPerformed(ActionEvent action) {
quantitySelected = new Double(quantity.getText()).doubleValue();
if (itemSelected==1)
total = quantitySelected * eggs;
if (itemSelected==2)
total = quantitySelected * milk;
if (itemSelected==3)
total = quantitySelected * bread;
}
public void itemStateChanged(ItemEvent event) {
if (itemSelected==1)
price.setText("The price of a dozen eggs is " + eggs);
if (itemSelected==2)
price.setText("The price of a quart of milk is " + milk);
if (itemSelected==3)
price.setText("The price of a loaf of bread is " + bread);
}
public void paint(Graphics g){
if (itemSelected==1){
g.drawString("You purchased " + quantitySelected + "dozen eggs",20,20);
g.drawString("for a total of " + total,20,30);
}
}
}
}
•
•
Join Date: Apr 2004
Posts: 6
Reputation:
Solved Threads: 1
another thing ... quick paste into eclipse and it suggests that you cast "this" with (ActionListener) and (ItemListener) .... Also I think you may have meant addItemListener on the second food.add* line.
order.addActionListener((ActionListener)this);
food.addItemListener((ItemListener)this);
order.addActionListener((ActionListener)this);
food.addItemListener((ItemListener)this);
//edit ... left off the semicolons myself that time
order.addActionListener((ActionListener)this);
food.addItemListener((ItemListener)this);
order.addActionListener((ActionListener)this);
food.addItemListener((ItemListener)this);
//edit ... left off the semicolons myself that time
•
•
Join Date: Apr 2004
Posts: 5
Reputation:
Solved Threads: 0
well......after an hour of staring and trying things I come to another dead end......thank you for the help before I appreciate it but now my applet doesn't initialize....and all I get is a blank rectangle I get no errors so I'm not sure what I'm doing wrong...I'll keep working at it but if someone finds something before I do I'd appreciate it a lot
Thank you in advance
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class grocerystore extends Applet{
private Button order = new Button("Order");
private Label price = new Label("Your price will appear here");
private Choice food = new Choice();
private TextField quantity = new TextField();
double quantitySelected;
double eggs = 1.90;
double milk = 1.47;
double bread = 2.12;
double total;
display output = new display();
public void init() {
add(order);
add(price);
add(quantity);
add(food);
food.add("Select your food");
food.add("Eggs");
food.add("Milk");
food.add("Bread");
add(output);
output.setSize(250,100);
order.addActionListener((ActionListener)this);
food.addItemListener((ItemListener)this);
order.addActionListener((ActionListener)this);
food.addItemListener((ItemListener)this);
}
class display extends Canvas
implements ActionListener, ItemListener {
int itemSelected = food.getSelectedIndex();
public void actionPerformed(ActionEvent action) {
quantitySelected = new Double(quantity.getText()).doubleValue();
if (itemSelected==1)
total = quantitySelected * eggs;
if (itemSelected==2)
total = quantitySelected * milk;
if (itemSelected==3)
total = quantitySelected * bread;
}
public void itemStateChanged(ItemEvent event) {
if (itemSelected==1)
price.setText("The price of a dozen eggs is " + eggs);
if (itemSelected==2)
price.setText("The price of a quart of milk is " + milk);
if (itemSelected==3)
price.setText("The price of a loaf of bread is " + bread);
}
public void paint(Graphics g){
if (itemSelected==1){
g.drawString("You purchased " + quantitySelected + "dozen eggs",20,20);
g.drawString("for a total of " + total,20,30);
}
}
}
}
Thank you in advance
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class grocerystore extends Applet{
private Button order = new Button("Order");
private Label price = new Label("Your price will appear here");
private Choice food = new Choice();
private TextField quantity = new TextField();
double quantitySelected;
double eggs = 1.90;
double milk = 1.47;
double bread = 2.12;
double total;
display output = new display();
public void init() {
add(order);
add(price);
add(quantity);
add(food);
food.add("Select your food");
food.add("Eggs");
food.add("Milk");
food.add("Bread");
add(output);
output.setSize(250,100);
order.addActionListener((ActionListener)this);
food.addItemListener((ItemListener)this);
order.addActionListener((ActionListener)this);
food.addItemListener((ItemListener)this);
}
class display extends Canvas
implements ActionListener, ItemListener {
int itemSelected = food.getSelectedIndex();
public void actionPerformed(ActionEvent action) {
quantitySelected = new Double(quantity.getText()).doubleValue();
if (itemSelected==1)
total = quantitySelected * eggs;
if (itemSelected==2)
total = quantitySelected * milk;
if (itemSelected==3)
total = quantitySelected * bread;
}
public void itemStateChanged(ItemEvent event) {
if (itemSelected==1)
price.setText("The price of a dozen eggs is " + eggs);
if (itemSelected==2)
price.setText("The price of a quart of milk is " + milk);
if (itemSelected==3)
price.setText("The price of a loaf of bread is " + bread);
}
public void paint(Graphics g){
if (itemSelected==1){
g.drawString("You purchased " + quantitySelected + "dozen eggs",20,20);
g.drawString("for a total of " + total,20,30);
}
}
}
}
Something is wrong with how you use the ActionListner().
//order.addActionListener((ActionListener)this);
//food.addItemListener((ItemListener)this);
//order.addActionListener((ActionListener)this);
//food.addItemListener((ItemListener)this);
in the init() method, those are cuasing the applet not to intialize correctly and why do you do it twice?
EDIT: I suspect that you are handleing the action events wrong.
//order.addActionListener((ActionListener)this);
//food.addItemListener((ItemListener)this);
//order.addActionListener((ActionListener)this);
//food.addItemListener((ItemListener)this);
in the init() method, those are cuasing the applet not to intialize correctly and why do you do it twice?
EDIT: I suspect that you are handleing the action events wrong.
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
•
•
Join Date: Apr 2004
Posts: 5
Reputation:
Solved Threads: 0
Alright well I took out the extra two lines so they aren't in there twice and instead of using the keyword this I applied them to thier respective areas....price and output but it still won't initialize I even tried it keeping it with the keyword this and still no progress.......
order.addActionListener((ActionListener)output);
food.addItemListener((ItemListener)price);
order.addActionListener((ActionListener)this);
food.addItemListener((ItemListener)this);
niether way works :-(
EDIT----ok well I worked on it some more and I got the applet to initalize when I commented out the second line so I had
order.addActionListener((ActionListener)output);
//food.addItemListener((ItemListener)price);
it initialized but didn't do anything when I entered a int into the text box and hit the order button
I don't know why it won't initalize with the second line
order.addActionListener((ActionListener)output);
food.addItemListener((ItemListener)price);
order.addActionListener((ActionListener)this);
food.addItemListener((ItemListener)this);
niether way works :-(
EDIT----ok well I worked on it some more and I got the applet to initalize when I commented out the second line so I had
order.addActionListener((ActionListener)output);
//food.addItemListener((ItemListener)price);
it initialized but didn't do anything when I entered a int into the text box and hit the order button
I don't know why it won't initalize with the second line
•
•
Join Date: Apr 2004
Posts: 5
Reputation:
Solved Threads: 0
<html>
<body>
<applet code="grocerystore.java"width="250"height="250">
</applet>
</body>
</html>
that is the html code I'm using.....it's a simple webpage used to just test the code I'm getting a cast exception on line 30 and 31 which are the addActionListener and the addItemListener....I don't know why I can't figure it out.....:-( I e-mailed my professor with the question...I'm gonna see what he has to say about it.....and I'll work on it some more....if you looked at it some more that would be great one of these times my eyes are going to start bleeding from staring too long.......
<body>
<applet code="grocerystore.java"width="250"height="250">
</applet>
</body>
</html>
that is the html code I'm using.....it's a simple webpage used to just test the code I'm getting a cast exception on line 30 and 31 which are the addActionListener and the addItemListener....I don't know why I can't figure it out.....:-( I e-mailed my professor with the question...I'm gonna see what he has to say about it.....and I'll work on it some more....if you looked at it some more that would be great one of these times my eyes are going to start bleeding from staring too long.......
![]() |
Similar Threads
- VC++ 6.0 errors (C++)
- compilation errors (C++)
Other Threads in the Java Forum
- Previous Thread: help to set up "Sun Java™ System Application Server PE 8" for web application
- Next Thread: java code newbie
| Thread Tools | Search this Thread |
-xlint 911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character class client code compile component consumer csv database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list mac map method methods mobile netbeans notdisplaying number objects online oriented printf problem program project projects recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing system test textfields threads time title tree tutorial-sample ubuntu update windows working





