这种 hack 的缺点是什么,除了无法用一个听众听很多按钮:
public class Activator<E> extends JButton implements ActionListener {
protected E controller;
public Activator( String label, E controller ) {
super( label );
this.addActionListener( this );
this.controller = controller;
}
@Override
public void actionPerformed( ActionEvent e ) {
return;
}
}
按钮通过以下方式实例化:
this.buttons = new LinkedHashMap<String, JButton>();
this.buttons.put( "create",
new Activator<MainMenu>( "Create new definition", this ) {
@Override
public void actionPerformed( ActionEvent e ) {
this.controller.createDefinition();
}
}
);