Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Two Errors in Java code Please help?

Hi could someone please assist me in correcting these two errors?

code: import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import java.awt.Label.*;

public class Freddie extends Applet implements ItemListener

{

//Construct Components

Label sandwichPromptLabel = new Label("sandwich");

Label sandwichInputField = new TextField("Input");

Label sizePromptLabel = new Label("size");

CheckboxGroup sandwichGroup = new CheckboxGroup();

Checkbox catsupBox = new Checkbox("catsup",false,sandwichGroup);

Checkbox mustardBox = new Checkbox("mustard",false,sandwichGroup);

Checkbox picklesBox = new Checkbox("pickles",false,sandwichGroup);

Checkbox sizeGroupBox = new Checkbox("sizeGroup",true,sandwichGroup);

Checkbox smallBox = new Checkbox("small",false,sandwichGroup);

Checkbox mediumBox = new Checkbox("medium",false,sandwichGroup);

Checkbox largeBox = new Checkbox("large",false,sandwichGroup);

public void init()

{

setBackground(red);

add(sizePromptLabel);

add(sandwichInputField);

add(sizePromptLabel);

add(catsupBox);

catsupBox.addItemListener(this);

add(mustardBox);

mustardBox.addItemListener(this);

add(picklesBox);

picklesBox.addItemListener(this);

add(sizeGroupBox);

sizeGroupBox.addItemListener(this);

add(smallBox);

smallBox.addItemListener(this);

add(mediumBox);

mediumBox.addItemListener(this);

add(largeBox);

largeBox.addItemListener(this);

}

public void itemStateChanged(ItemEvent choice)

{

}

}

Errors: C:\Course Technology\59850d\Chapter 04\Freddie.java:22: incompatible types

found : java.awt.TextField

required: java.awt.Label

Label sandwichInputField = new TextField("Input");

^

C:\Course Technology\59850d\Chapter 04\Freddie.java:36: cannot find symbol

symbol : variable red

location: class Freddie

setBackground(red);

^

2 errors

Thank You :-)

3 Answers

Relevance
  • 9 years ago
    Favorite Answer

    as for setbackgroung it should be

    setBackground(color.red);

    and how label is = to a new text field

    it should be

    TextField sandwichInputField = new TextField("Input");

    other people are copying my solution into their answer to this question !? come on !

  • MANOJ
    Lv 4
    9 years ago

    import java.applet.Applet;

    import java.awt.Checkbox;

    import java.awt.CheckboxGroup;

    import java.awt.Color;

    import java.awt.Label;

    import java.awt.TextField;

    import java.awt.event.ItemEvent;

    import java.awt.event.ItemListener;

    public class Freddie extends Applet implements ItemListener

    {

    //Construct Components

    Label sandwichPromptLabel = new Label("sandwich");

    TextField sandwichInputField = new TextField("Input");

    Label sizePromptLabel = new Label("size");

    CheckboxGroup sandwichGroup = new CheckboxGroup();

    Checkbox catsupBox = new Checkbox("catsup",false,sandwichGroup);

    Checkbox mustardBox = new Checkbox("mustard",false,sandwichGroup);

    Checkbox picklesBox = new Checkbox("pickles",false,sandwichGroup);

    Checkbox sizeGroupBox = new Checkbox("sizeGroup",true,sandwichGroup);

    Checkbox smallBox = new Checkbox("small",false,sandwichGroup);

    Checkbox mediumBox = new Checkbox("medium",false,sandwichGroup);

    Checkbox largeBox = new Checkbox("large",false,sandwichGroup);

    public void init()

    {

    setBackground(Color.RED);

    add(sizePromptLabel);

    add(sandwichInputField);

    add(sizePromptLabel);

    add(catsupBox);

    catsupBox.addItemListener(this);

    add(mustardBox);

    mustardBox.addItemListener(this);

    add(picklesBox);

    picklesBox.addItemListener(this);

    add(sizeGroupBox);

    sizeGroupBox.addItemListener(this);

    add(smallBox);

    smallBox.addItemListener(this);

    add(mediumBox);

    mediumBox.addItemListener(this);

    add(largeBox);

    largeBox.addItemListener(this);

    }

    public void itemStateChanged(ItemEvent choice)

    {

    }

    }

    http://javacodespot.blogspot.com/

  • 9 years ago

    Well the first error is quite obvious. You even have the line number of the error.

    The second one should be

    setBackground(Color.RED);

    Though this works too.

    setBackground(Color.red);

    Perhaps you should look into using either Eclipse, Netbeans or Notepad++.

    Have fun.

Still have questions? Get your answers by asking now.