Results 1 to 3 of 3
  1. #1
    babyruth1193 is offline Steph-A-Nay-Nay
    Join Date
    Nov 2011
    Location
    North Carolina
    Posts
    1
    Rep Power
    0

    Default totally stumped -- java applet with buttons

    ok so I've got the program for the most part. I'm supposed to create an applet with 3 buttons and when you click on a certain button, it changes the background to the color on the button...this is what I have so far and I'll point out where I'm getting errors in red.

    import javax.swing.JApplet;
    import javax.swing.JButton;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    public class RedWhiteBlue extends JApplet implements ActionListener
    {
    public void init()
    {
    Container contentPane = getContentPane();
    contentPane.setBackground(Color.GREEN);

    contentPane.setLayout(new FlowLayout());
    JButton redButton = new JButton("Red");
    contentPane.add(redButton);
    redButton.addActionListener(this);

    JButton whiteButton = new JButton("White");
    contentPane.add(whiteButton);
    whiteButton.addActionListener(this);

    JButton blueButton = new JButton("Blue");
    contentPane.add(blueButton);
    blueButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getActionCommand().equals("Red"))
    contentPane.setBackground(Color.RED);
    else if(e.getActionCommand().equals("White"))
    contentPane.setBackground(Color.WHITE);
    else if(e.getActionCommand().equals("Blue"))
    contentPane.setBackground(Color.BLUE);
    else
    System.out.println("Error in button interface");
    }
    }

    the error code I'm getting says "error: cannot find symbol"
    I'm probably just having a major blank right now, but it's stumping me haha
    :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: totally stumped -- java applet with buttons

    You have what's known as a "scope" issue. your contentPane variable is declared and initialized within init. Because it was declared in init, it is only visible within this method and does not even exist outside of init. You should declare it in the class to allow you to use it everywhere in the class.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,229
    Rep Power
    19

    Default Re: totally stumped -- java applet with buttons

    Um, getContentPane() can be called from anywhere in the class, so there's no need to create an additional field.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 10
    Last Post: 01-17-2011, 10:41 PM
  2. stumped......
    By trueblue in forum New To Java
    Replies: 21
    Last Post: 07-13-2009, 04:16 PM
  3. Replies: 0
    Last Post: 02-11-2009, 11:12 PM
  4. Applet buttons
    By h3ckf1r3 in forum Java Applets
    Replies: 6
    Last Post: 09-22-2008, 09:15 PM
  5. Replies: 1
    Last Post: 09-04-2008, 09:07 AM

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
  •