if (ce==yellowButton) this.setBackground(Color.yellow);
        else if (e==blueButton) this.setBackground(Color.blue);
        else if (e==redButton) this.setBackground(Color.red);
是“==“不是“=“,还有几个问题,我还没有改好

解决方案 »

  1.   

    修订程序如下》:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;class ButtonPanel extends JPanel
    {
    public ButtonPanel()
    {
    JButton yellowButton=new JButton("yellow");
    JButton blueButton=new JButton("blue");
    JButton redButton=new JButton("red");


    add(yellowButton);
    add(blueButton);
    add(redButton);

    ColorAction yellowAction=new ColorAction(Color.yellow);
    ColorAction blueAction=new ColorAction(Color.blue);
    ColorAction redAction=new ColorAction(Color.red);

    yellowButton.addActionListener(yellowAction);
    blueButton.addActionListener(blueAction);
    redButton.addActionListener(redAction);
    }

    private class ColorAction implements ActionListener{
    public ColorAction(Color c)
    {
    backgroundColor=c;
    }
    public void actionPerformed(ActionEvent event)
    {
    setBackground(backgroundColor);
    repaint();
    }

    private Color backgroundColor;
    }
    }class ButtonFrame extends JFrame
    {
    public ButtonFrame()
    {
    setTitle("ButtonTest");
    setSize(WIDTH,HEIGHT);

    ButtonPanel panel=new ButtonPanel();
    Container contentPanel=getContentPane();
    contentPanel.add(panel);
    }
    public static final int WIDTH=300;
    public static final int HEIGHT=500;
    }public class ButtonTest
    {
    public static void main(String[] args)
    {
    ButtonFrame frame=new ButtonFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    }
    }
      

  2.   

    程序我自己已经该好了,如下:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.EventObject;
    public class ButtonTest2
    {
        public static void main(String[] args)
        {
            ButtonFrame frame=new ButtonFrame();
            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
            frame.show();
        }
    }
    class ButtonFrame2 extends JFrame
    {
        public ButtonFrame2()
        {
            setTitle("ButtonTest");
            this.setSize(WIDTH,HEIGHT);
            ButtonPanel2 panel=new ButtonPanel2();
            Container contentpanel=this.getContentPane();
            contentpanel.add(panel);
        }
        public final int WIDTH=300;
        public final int HEIGHT=200;
    }class ButtonPanel2 extends JPanel implements ActionListener
    {
        JButton yellowButton,blueButton,redbutton;
        Object source;
        public ButtonPanel2()
        {
            //create button
            JButton yellowButton=new JButton("yellow");
            JButton blueButton=new JButton("blue");
            JButton redButton=new JButton("red");
            //add button
            this.add(yellowButton);
            this.add(blueButton);
            this.add(redButton);
            yellowButton.addActionListener(this);
            blueButton.addActionListener(this);
            redButton.addActionListener(this);
        }
        public void actionPerformed(ActionEvent e)
        {
            EventObject event=new EventObject(source);
            Object ce=event.getSource();
            if (ce==yellowButton) this.setBackground(Color.yellow);
            else if (e==blueButton) this.setBackground(Color.blue);
            else if (e==redButton) this.setBackground(Color.red);
        }}但是我还是有个问题,我记得比较基本类型的字段是用==,但比较对象字段是用
    equals(object)阿。但这里用了==竟然也可以,不知道为什么?
      

  3.   

    equals比较的只是对象的参数!!!
      

  4.   

    topology:equals() compares the reference of the two objects
      

  5.   

    JButton yellowButton,blueButton,redbutton;
        Object source;
        public ButtonPanel2()
        {
            //create button
            JButton yellowButton=new JButton("yellow");
            JButton blueButton=new JButton("blue");
            JButton redButton=new JButton("red");这种写法是错误的,全局变量中的按钮是空值.
      

  6.   

    class ButtonPanel2 extends JPanel implements ActionListener
    {
        JButton yellowButton,blueButton,redbutton;
        Object source;
        public ButtonPanel2()
        {
            //create button
            JButton yellowButton=new JButton("yellow");
            JButton blueButton=new JButton("blue");
            JButton redButton=new JButton("red");
            //add button
            this.add(yellowButton);
            this.add(blueButton);
            this.add(redButton);
            yellowButton.addActionListener(this);
            blueButton.addActionListener(this);
            redButton.addActionListener(this);
        }
        public void actionPerformed(ActionEvent e)
        {
            EventObject event=new EventObject(source);
                                             <<=====source在那里得到值?
            ... ...
        }
      

  7.   

    假如我不声明:
    JButton yellowButton,blueButton,redbutton;
    编译就不能通过阿,何况这里虽然是空值,但在构造方法中已经新建了对象了阿。这里只是声明一下。至于<<=====source在那里得到值,确实这是个问题,我是想用EventObject的getSource方法,但这个方法的参数是(Object source),所以我就那样写了,程序也没有报错。具体请高手详细说明一下吧!!!期待.........
      

  8.   

    我发现程序这样写也可以运行:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.EventObject;
    public class ButtonTest2
    {
        public static void main(String[] args)
        {
            ButtonFrame frame=new ButtonFrame();
            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
            frame.show();
        }
    }
    class ButtonFrame2 extends JFrame
    {
        public ButtonFrame2()
        {
            setTitle("ButtonTest");
            this.setSize(WIDTH,HEIGHT);
            ButtonPanel2 panel=new ButtonPanel2();
            Container contentpanel=this.getContentPane();
            contentpanel.add(panel);
        }
        public final int WIDTH=300;
        public final int HEIGHT=200;
    }class ButtonPanel2 extends JPanel implements ActionListener
    {
        //JButton yellowButton,blueButton,redButton;
        Object yellowButton,blueButton,redButton,source;
        public ButtonPanel2()
        {
            //create button
            JButton yellowButton=new JButton("yellow");
            JButton blueButton=new JButton("blue");
            JButton redButton=new JButton("red");
            //add button
            this.add(yellowButton);
            this.add(blueButton);
            this.add(redButton);
            yellowButton.addActionListener(this);
            blueButton.addActionListener(this);
            redButton.addActionListener(this);
        }
        public void actionPerformed(ActionEvent e)
        {
            EventObject event=new EventObject(source);//or new EventObject(null)
            Object ce=event.getSource();
            if (ce.equals(yellowButton)) this.setBackground(Color.yellow);
            else if (ce.equals(blueButton)) this.setBackground(Color.blue);
            else if (ce.equals(redButton)) this.setBackground(Color.red);
        }}
      

  9.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.EventObject;public class ButtonTest {
        
        public static void main(String[] args) {
            
            ButtonFrame frame=new ButtonFrame();
            frame.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e)
                    {System.exit(0);}
                });
            frame.show();
        }
    }
    class ButtonFrame extends JFrame {
        
        public final int WIDTH=300;
        public final int HEIGHT=200;
        
        public ButtonFrame() {
            setTitle("ButtonTest");
            this.setSize(WIDTH,HEIGHT);
            ButtonPanel panel=new ButtonPanel();
            Container contentpanel=this.getContentPane();
            contentpanel.add(panel);
        }
        
    }
    class ButtonPanel extends JPanel implements ActionListener {
        
        public JButton yellowButton, blueButton, redButton;
        
        public ButtonPanel() {
            
            //create button
            yellowButton = new JButton("yellow");
            blueButton = new JButton("blue");
            redButton = new JButton("red");
            
            //add button
            this.add(yellowButton);
            this.add(blueButton);
            this.add(redButton);
            
            yellowButton.addActionListener(this);
            blueButton.addActionListener(this);
            redButton.addActionListener(this);
        }
        
        public void actionPerformed(ActionEvent e) {
            
            if (e.getSource() == yellowButton) this.setBackground(Color.yellow);
            else if (e.getSource() == blueButton) this.setBackground(Color.blue);
            else if (e.getSource() == redButton) this.setBackground(Color.red);
        }}
      

  10.   

    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);这句怎么报错??Container contentpanel=this.getContentPane(); 是什么意思,哪位给解释一下。
      

  11.   

    for HawaiiLeo(红桃K)
    你那个肯定不行的,ActionEvent没有getsource()的方法,所以e.getSource() == yellowButton肯定编译通不过。我用的是java.swing.*,所以必须把组件放在内容面板上,所以有:
    Container contentpanel=this.getContentPane();