循环中是
i<=2
不是
i<2

解决方案 »

  1.   

    thx,我没注意那。:)
    不过不是那的错:--------------------Configuration: JDK version  <Default>--------------------
    E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:11: ']' expected
    btn[0] = new Button("Yellow");
                ^
    E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:11: <identifier> expected
    btn[0] = new Button("Yellow");
                                         ^
    E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:12: ']' expected
    btn[1] = new Button("Green");
                ^
    E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:12: <identifier> expected
    btn[1] = new Button("Green");
                                        ^
    E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:13: ']' expected
    btn[2] = new Button("Exit");
                ^
    E:\Program\Program\Java\JCreator\MyProjects\appActionEvent.java:13: <identifier> expected
    btn[2] = new Button("Exit");
                                       ^
    6 errorsProcess completed.
      

  2.   

    import java.awt.*;
    import java.awt.event.*;public class appActionEvent extends Frame implements ActionListener
    {
    static appActionEvent frm = new appActionEvent();
    /*
    static Button btn1 = new Button("Yellow");
    static Button btn2 = new Button("Green");
    static Button btn3 = new Button("Exit");
    */
    static Button btn[] = new Button[3];


    public static void main(String args[])
    {
    btn[0] = new Button("Yellow");
    btn[1] = new Button("Green");
    btn[2] = new Button("Exit");
    /*
    btn1.addActionListener(frm);
    btn2.addActionListener(frm);
    btn3.addActionListener(frm);
    */
    for(int i =0;i <=2;i++)
    btn[i].addActionListener(frm);

    frm.setLayout(new FlowLayout(FlowLayout.CENTER));
    frm.setTitle("Action Event");
    frm.setSize(200,150);

    /*
    frm.add(btn1);
    frm.add(btn2);
    frm.add(btn3);
    */
    for(int i =0;i <=2;i++)
    frm.add(btn[i]);

    frm.setVisible(true);
    frm.addWindowListener
    (
    new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );
    }

    public void actionPerformed(ActionEvent e)
    {
    Button bt = (Button) e.getSource();

    if(bt==btn[0]) //if(btn ==btn1)
    frm.setBackground(Color.yellow);
    else if(bt == btn[1]) //else if(btn ==btn2)
    frm.setBackground(Color.green);
    else
    System.exit(0);
    }
    }
      

  3.   

    谢谢楼上的~!
    不过btn[]不是已经声明为static吗?为什么要在main()里面赋值才行?
      

  4.   

    赋值语句只能在方法中出现;
    不管是不是static 和 对象等 一切都是
    如你上面的可以这样写;
    声明的时候赋值;
    如 class A {
         static Button btn[] = {new Button("1"),new Button("2"),new Button("3")};
         //int x;//
         /* x =1;*/ 不能在这赋值;
         int x =1 ;
       }