Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2003, 07:39 PM   PM User | #1
JavaTest?
New Coder

 
Join Date: Jan 2003
Location: USA
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
JavaTest? is an unknown quantity at this point
Java code problem

Here's a program I'm trying to write that uses checkboxes. But when I compile it, it doesn't work. Can anyone please point out any errors they see in the code?

import java.applet.Applet.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JApplet implements ActionListener
{
String companyName = new String("Event Handlers, Inc");
Font bigFont = new Font("Arial", Font.PLAIN, 24);
JCheckBox cocktailBox = new JCheckBox("Cocktails");
JCheckBox dinnerBox = new JCheckBox("Dinner");
int cocktailPrice = 300, dinnerPrice = 600, totalPrice = 200;

public void init()
{
add(cocktailBox);
cocktailBox.addActionListener(this);
add(dinnerBox);
dinnerBox.addActionListener(this);
}

public void paint(Graphics gr)
{
gr.setFont(bigFont);
gr.setColor(Color.magenta);
gr.drawString(companyName, 10, 100);
gr.drawString("Event Price Estimate", 10, 150);
gr.setColor(Color.blue);
gr.drawString(Integer.toString(totalPrice), 280, 150);
}

public void itemStateChanged(ActionEvent check)
{
totalPrice = 200;
if(cocktailBox.getState())
{
totalPrice += cocktailPrice;
}
if(dinnerBox.getState())
{
totalPrice += dinnerPrice;
}
repaint();
}
}
JavaTest? is offline   Reply With Quote
Old 02-13-2003, 01:44 PM   PM User | #2
bcarl314
Mega-ultimate member


 
Join Date: Jun 2002
Location: Winona, MN - The land of 10,000 lakes
Posts: 1,855
Thanks: 1
Thanked 45 Times in 42 Posts
bcarl314 will become famous soon enough
What's the error message you get when compiling?
bcarl314 is offline   Reply With Quote
Old 02-13-2003, 04:02 PM   PM User | #3
Josh Campbell
New Coder

 
Join Date: Jun 2002
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Josh Campbell is an unknown quantity at this point
Taken from official Java API doc:

The JApplet class is slightly incompatible with java.applet.Applet. JApplet contains a JRootPane as it's only child. The contentPane should be the parent of any children of the JApplet. This is different than java.applet.Applet, e.g. to add a child to an an java.applet.Applet you'd write:

applet.add(child);

However using JApplet you need to add the child to the JApplet's contentPane instead:

applet.getContentPane().add(child);
Josh Campbell is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:49 PM.


Advertisement
Log in to turn off these ads.