日期:2014-05-20  浏览次数:20650 次

为什么在新建对象后面跟了一个方法。。。
Java code
public void init() { 
     ActionListener al = new ActionListener() { 
        public void actionPerformed(ActionEvent e){ 
           String name = 
               ((JButton)e.getSource()).getText(); 
           t.setText(name + " Pressed"); 
        } 
    }; 
    b1.addActionListener(al); 
    add(b1); 
    b2.addActionListener(al); 
    add(b2); 
    add(t); 
 } 

RT,新建了ActionListener对象后,为什么在后面更了一个方法,还用{}括起来了?

------解决方案--------------------
Java code
ActionListener al = new ActionListener() { 
        public void actionPerformed(ActionEvent e){ 
           String name = 
               ((JButton)e.getSource()).getText(); 
           t.setText(name + " Pressed"); 
        } 
    };