Results 1 to 6 of 6
  1. #1
    javanoobita is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default my code doesn't change background color

    my code doesn't change color pls help:(

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    public class ColorChange extends Applet implements ActionListener
    {
    Button a,b,c;
    Color bgcolor;
    public void init()
    {
    Button a = new Button("Blue");
    Button b = new Button("Red");
    Button c = new Button("Green");
    add(a);
    add(b);
    add(c);
    a.addActionListener(this);
    b.addActionListener(this);
    c.addActionListener(this);
    bgcolor=Color.white;
    }

    public void paint(Graphics g){
    setBackground(bgcolor);
    }


    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==a){
    bgcolor=Color.blue;
    }else if(e.getSource()==b){
    bgcolor=Color.red;
    }else if(e.getSource()==c){
    bgcolor=Color.green;
    }
    repaint();
    }

    }

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

    Default

    Please don't cross-post the same question in multiple fora as it's considered inconsiderate here.

    You declare your buttons twice, once in the class and again in the init method. The init method's buttons are the ones that are placed on the applet, but it's the class buttons that are looked for in the actionperformed method (if e.getSource == ....).

    Don't do this. Only declare the buttons once and here only in the class. Also if it were me, I wouldn't do a background paint like this in the paint method but I'd just call setBackground in the actionPerformed method. i'd also use JApplets not applets.

  3. #3
    javanoobita is offline Member
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default

    thx, now i try to edit the code with an execute button however there are errors... i don't know how to fix it :confused:

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    public class ColorChange2 extends Applet implements ActionListener, ItemListener
    {

    Color bgcolor;

    Button a = new Button("Blue");
    Button b = new Button("Red");
    Button c = new Button("Green");
    Button d = new Button("Execute");

    public void init()
    {

    add(a);
    add(b);
    add(c);
    add(d);

    a.addItemListener(this);
    b.addItemListener(this);
    c.addItemListener(this);
    d.addActionListener(this);

    bgcolor=Color.white;
    }


    public void paint(Graphics g){
    setBackground(bgcolor);
    }




    public void ItemStateChanged(ItemEvent e){

    if(e.getItemSelectable()==a){
    bgcolor=Color.blue;
    }else if(e.getItemSelectable()==b){
    bgcolor=Color.red;
    }else if(e.getItemSelectable()==c){
    bgcolor=Color.green;
    }

    }


    public void actionPerformed(ActionEvent e)
    {
    repaint();
    }

    }

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

    Default

    what do the errors say? They will usually tell you what needs to be fixed.

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

    Default

    Please don't cross-post the same question in multiple fora.

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

    Default

    note: the original poster has abandoned this thread for the forums.java.sun thread.
    Last edited by Fubarable; 02-22-2009 at 04:59 AM.

Similar Threads

  1. Replies: 6
    Last Post: 01-02-2009, 09:36 PM
  2. Replies: 13
    Last Post: 10-21-2008, 02:32 PM
  3. setting background color of JFrame form with NetBeans 6.1
    By onefootswill in forum New To Java
    Replies: 4
    Last Post: 08-12-2008, 07:02 AM
  4. JButton onClick change color background
    By behrk2 in forum AWT / Swing
    Replies: 6
    Last Post: 07-09-2008, 04:54 PM
  5. window background color?
    By javan00b in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 10:43 AM

Posting Permissions

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