Results 1 to 2 of 2

Thread: setBackground

  1. #1

    Thread Starter
    Member pungentSapling's Avatar
    Join Date
    Jul 2002
    Location
    Ontario, Canada
    Posts
    34

    setBackground

    Java NEWBIE
    I can't get setBackground to work.
    I will include the code.

    The program is a simple GUI that is supposed to change the background color to the color you select in the choice control.

    this is an awt exercise.

    my concernis with the

    pMain.setBackground(Color."a color name") parts I can not get the codde to compile the color name throws an error... cannot resolve symbol symbol: Variable red for example

    [java code]
    import java.awt.*;
    import java.awt.event.*;
    public class ColorChanger implements ActionListener,ItemListener {


    private Frame f;
    private Panel pMain;

    private Choice cColor;

    private MenuBar mb;
    private Menu m;
    private MenuItem miClose, miClear;

    public ColorChanger(){



    Frame f= new Frame("Color Changer with ItemListener");
    Panel pMain = new Panel();

    mb= new MenuBar();
    m = new Menu("File");
    miClose = new MenuItem("Close");
    miClear = new MenuItem("Clear");
    miClose.addActionListener(this);
    miClear.addActionListener(this);
    m.add(miClose);
    m.add(miClear);
    mb.add(m);


    cColor= new Choice();
    cColor.addItemListener(this);
    cColor.add("Choose a Color");
    cColor.add("Green");
    cColor.add("Red");
    cColor.add("Blue");
    cColor.add("Yellow");
    cColor.add("Orange");

    //pMain.setMenuBar(mb);
    pMain.add(cColor);
    f.addWindowListener(new WindowCloser());
    f.setMenuBar(mb);
    f.add(pMain);
    f.setSize(300,300);
    f.setVisible(true);
    //f.setBackground(Color.red);

    }
    public void actionPerformed(ActionEvent e) {
    System.out.println("actionPerformed called");
    if (e.getSource() == miClose) { //if menu item close is selected then exit
    System.exit(0);
    }
    if (e.getSource() == miClear) {
    ColorChanger a = new ColorChanger(); //resets all settings by recalling the class
    }
    }

    public void itemStateChanged(ItemEvent e) {
    String vCol;
    Color Orange;

    vCol = cColor.getSelectedItem();
    System.out.println(vCol);
    System.out.println("itemStateChanged called");

    if (vCol=="Red") {
    System.out.println("Success Red");
    pMain.setBackground(Color.red);

    }
    if (vCol=="Blue"){
    System.out.println("Sucess Blue");
    //f.setBackground(Color.Blue);
    }
    if (vCol=="Yellow"){
    System.out.println("Success Yellow");
    //f.setBackground(Color.Yellow);
    }
    if (vCol=="Green"){
    System.out.println("Success Green");
    //f.setBackground(Color.Green);
    }
    if (vCol =="Orange"){
    System.out.println("Success Orange");
    //f.setBackground(Color.Orange);
    }

    }

    public static void main (String [] args) {
    ColorChanger a= new ColorChanger();




    }

    class WindowCloser extends WindowAdapter{
    public void windowClosing(WindowEvent we){
    System.exit(0);
    }
    }



    }
    [/java code]
    any help would be appreciated.

  2. #2
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    Your pMain variable is not defined in the procedure you're calling it in. Its a local variable for ColorChanger

Posting Permissions

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



Click Here to Expand Forum to Full Width