- 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"); } };