import java.awt.*;
import java.awt.event.*;public class TestPanel {
public static void main(String args[]) {
W w = new W();
}
}class Mypanel extends Panel implements ActionListener {
Button b1,b2;
Color backColor;
Mypanel() {
Button b1 = new Button("确定");
Button b2 = new Button("取消");
add(b1);
add(b2);
setBackground(Color.cyan);
backColor = getBackground();
b1.addActionListener(this);
b2.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1) {
setBackground(Color.yellow);
}
else if(e.getSource()==b2) {
setBackground(backColor);
}
}
}class W extends Frame implements ActionListener {
Mypanel p1,p2;
Button exit;
W() {
setLayout(new FlowLayout());
p1 = new Mypanel();
p2 = new Mypanel();
exit = new Button("退出");
add(p1);
add(p2);
add(exit);
exit.addActionListener(this);
setBounds(50,50,150,150);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}

解决方案 »

  1.   


    if(e.getSource()==b1) {这行代码 条件判断不会为真~~~找下原因吧
      

  2.   

    问题解决 自己看代码import java.awt.*;
    import java.awt.event.*;public class TestPanel {
        public static void main(String args[]) {
            W w = new W();
        }
    }class Mypanel extends Panel implements ActionListener {
        Button b1,b2;
        Color backColor;
        Mypanel() {
            b1 = new Button("确定");
            b2 = new Button("取消");

            add(b1);
            add(b2);
            setBackground(Color.cyan);
            backColor = getBackground();
            b1.addActionListener(this);
            b2.addActionListener(this);
        }
        
        public void actionPerformed(ActionEvent e) {

            if(e.getSource().equals(b1)) {
    System.out.println("K1");
                this.setBackground(Color.yellow);
    this.repaint();
            }
            else if(e.getSource()==b2) {
        System.out.println("K2");
                setBackground(backColor);
            }
        }
    }class W extends Frame implements ActionListener {
        Mypanel p1,p2;
        Button exit;
        W() {
            setLayout(new FlowLayout());
            p1 = new Mypanel();
            p2 = new Mypanel();
            exit = new Button("退出");
            add(p1);
            add(p2);
            add(exit);
            exit.addActionListener(this);
            setBounds(50,50,150,150);
            setVisible(true);
            validate();
        }
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
      

  3.   

    为什么if(e.getSource()==b1)不会为真
      

  4.   

    //请注意这里:楼主这里定义的是局部变量~~~~楼主粗心了。import java.awt.*;
    import java.awt.event.*;public class TestPanel {
        public static void main(String args[]) {
            W w = new W();
        }
    }class Mypanel extends Panel implements ActionListener {
        Button b1,b2;
        Color backColor;
        Mypanel() {
            //请注意这里:楼主这里定义的是局部变量~~~~楼主粗心了。
            b1 = new Button("确定");
            b2 = new Button("取消");
            add(b1);
            add(b2);
            setBackground(Color.cyan);
            backColor = getBackground();
            b1.addActionListener(this);
            b2.addActionListener(this);
        }
        
        public void actionPerformed(ActionEvent e) {
        
            if(e.getSource().equals(b1)) {
                System.out.println("K1");
                this.setBackground(Color.yellow);
                this.repaint();
            }
            else if(e.getSource()==b2) {
                System.out.println("K2");
                setBackground(backColor);
            }
        }
    }class W extends Frame implements ActionListener {
        Mypanel p1,p2;
        Button exit;
        W() {
            setLayout(new FlowLayout());
            p1 = new Mypanel();
            p2 = new Mypanel();
            exit = new Button("退出");
            add(p1);
            add(p2);
            add(exit);
            exit.addActionListener(this);
            setBounds(50,50,150,150);
            setVisible(true);
            validate();
        }
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
      

  5.   


    Button b1,b2;
        Color backColor;
        Mypanel() {
            Button b1 = new Button("确定");
            Button b2 = new Button("取消");
      

  6.   

    能解释下问什么重复声明 b1 b2.eclipse没有报错,但是改不了颜色。
    Button b1,b2;
    Color backColor;
    Mypanel() {
         b1 = new Button("确定");
         b2 = new Button("取消");

      

  7.   

    没报错,只能说明语法没有错误!e.getSource()==b1
    b1是成员变量,e.getSource()获得的是局部变量
    e.getSource()==b1永远是false