Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2003
    Location
    USA
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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();
    }
    }

  • #2
    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
    What's the error message you get when compiling?

  • #3
    New Coder
    Join Date
    Jun 2002
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •