if( source == yellow )
要用双等号,不能用单等号。

解决方案 »

  1.   

    if( source = yellow )  --------------呵呵,你学没学过java呀?
    怎么可以这样写呢?
    if(source == yellow)
      

  2.   

    不好意思,应该是这样的:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;class ButtonPanel extends JPanel implements ActionListener
    {
    public ButtonPanel()
    {

    JButton yellow = new JButton("yellow");
    JButton blue = new JButton("blue");

    add(yellow);
    add(blue);

    yellow.addActionListener(this);
    blue.addActionListener(this);
    }
    public void actionPerformed(ActionEvent event)
    {
    Object source = event.getSource();

    if( source == yellow )  //出错 variable yellow
       setBackground( Color.yellow );
    else setBackground( Color.blue );
    repaint();

    }


    }class myframe extends JFrame
    {
    public myframe()
    {
    setSize(200,200);
    setTitle("fuck");

    ButtonPanel bp = new ButtonPanel();
    Container c = getContentPane();
    c.add(bp);

    }
    }public class ButtonTest
    {
    public static void main(String[] args)
    {
    myframe mf = new myframe();
    mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mf.show();
    }
    }
      

  3.   

    JButton yellow = new JButton("yellow");
    JButton blue = new JButton("blue");
    你这两个对象是在public ButtonPanel()方法中定义的,是区域变量,出了方法外面就无法调用,虽说你是在构造函数里申明,也很容易出现这样那样的问题,建议你这样:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    {
        JButton yellow,blue;
    class ButtonPanel extends JPanel implements ActionListener
    {
    public ButtonPanel()
    {

    yellow = new JButton("yellow");
             blue = new JButton("blue");

    add(yellow);
    add(blue);

    yellow.addActionListener(this);
    blue.addActionListener(this);
    }
    public void actionPerformed(ActionEvent event)
    {
    Object source = event.getSource();

    if( source == yellow )  //出错 variable yellow
       setBackground( Color.yellow );
    else setBackground( Color.blue );
    repaint();

    }


    }class myframe extends JFrame
    {
    public myframe()
    {
    setSize(200,200);
    setTitle("fuck");

    ButtonPanel bp = new ButtonPanel();
    Container c = getContentPane();
    c.add(bp);

    }
    }public class ButtonTest
    {
    public static void main(String[] args)
    {
    myframe mf = new myframe();
    mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mf.show();
    }
    }
      

  4.   

    抱歉,格式贴错了,应该是这样:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    class ButtonPanel extends JPanel implements ActionListener
    {
       JButton yellow,blue;
       public ButtonPanel()
    {

    yellow = new JButton("yellow");
             blue = new JButton("blue");

    add(yellow);
    add(blue);

    yellow.addActionListener(this);
    blue.addActionListener(this);
    }
    public void actionPerformed(ActionEvent event)
    {
    Object source = event.getSource();

    if( source == yellow )  //出错 variable yellow
       setBackground( Color.yellow );
    else setBackground( Color.blue );
    repaint();

    }


    }class myframe extends JFrame
    {
    public myframe()
    {
    setSize(200,200);
    setTitle("fuck");

    ButtonPanel bp = new ButtonPanel();
    Container c = getContentPane();
    c.add(bp);

    }
    }public class ButtonTest
    {
    public static void main(String[] args)
    {
    myframe mf = new myframe();
    mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mf.show();
    }
    }