这些是jdk1.0的写法了。java2的事件处理机制要优美的多

解决方案 »

  1.   

    其实,要是保留原来的模式,可以这样改,把
    if (arg.equals("OK")) setBackground(Color.red); //点击没反应啊?
    改为
    if ((arg.toString()).equals("OK")).....
      

  2.   

    /**
     * <p>Title: This follow is newman's writing</p>
     * <p>Description: I want better writing ,instead of best one!</p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: [email protected]</p>
     * @author Newman
     * @version 1.0
     */import java.awt.*;
    import java.awt.event.*;public class ButtonTest extends Frame implements ActionListener{
        Panel pPanel=new Panel ();
        Button b1=new Button("OK");
        CircleColor color=new CircleColor(0);
        
      public boolean handleEvent(Event evt) {
        if (evt.id == Event.WINDOW_DESTROY) System.exit(0);
        return false;
      }
      public ButtonTest() {
        setTitle("Button Test");
        setLayout(new FlowLayout());
        
        b1.addActionListener(this) ;
        pPanel.add(b1);
        //pPanel.Size(50,80) ; 
        pPanel.setBackground(Color.pink) ;
        this.add ("Center",pPanel);
      }   public void actionPerformed(ActionEvent e){//监听实现
            if(e.getSource ()==b1){
                setBackground(this.color.getColor()); //点击有反应了
                System.out.println("OK pressed");
        }
       }
       
      public static void main(String[] args) {    
        Frame f = new ButtonTest();
        
        //changed by newman
        f.setSize(300, 200);
        f.show();    
      }
    }class CircleColor{
        private int m_nValue=0;
        private Color[] cols={Color.BLACK ,Color.BLUE ,Color.WHITE ,Color.GREEN,Color.RED}; 
        
        CircleColor(int value){
            this.m_nValue =value;
        }
        
        private int getValue(){
            return this.m_nValue ;        
        }
        
        public int increase(){
            this.m_nValue++;
            if(this.m_nValue>4){
                this.m_nValue =0;
            }
            return this.m_nValue ;
        }
        
        public Color getColor(){
            this.increase();
            return this.cols [this.getValue()];        
        }    
    }