×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Dread Morpheus
Added: Aug 14, 2017 12:20 AM
Modified: Sep 7, 2017 7:17 PM
Views: 2
Using an array of implemented commands, easy to set like components so long as you exploit that ActionCommand.
  1. package listeners;
  2.  
  3. import command.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.JButton;
  7.  
  8. /**
  9.  * Listener for all buttons to execute their respective commands.
  10.  *
  11.  * @author Diallo Thomas - 1695768@bdeb.qc.ca
  12.  */
  13. public class ActionListeners implements ActionListener {
  14.  
  15.     /**
  16.      * Objets de type Command.
  17.      */
  18.     private final Command[] commands = {new Command0(), new Command1(),
  19.         new Command2(), new Command3(), new Command4(), new Command5(),
  20.         new Command6(), new Command7(), new Command8(), new Command9(),
  21.         new CommandAdd(), new CommandClear(), new CommandDecimal(),
  22.         new CommandDivide(), new CommandMemAdd(), new CommandMemSubtract(),
  23.         new CommandMemRecall(), new CommandSubtract(), new CommandMultiply(),
  24.         new CommandSolve()};
  25.  
  26.     @Override
  27.     public void actionPerformed(ActionEvent e) {
  28.         if (e.getSource() instanceof JButton) {
  29.             String x = e.getActionCommand();
  30.             int index = Integer.parseInt(x);
  31.             commands[index].execute();
  32.         }
  33.     }
  34. }
  35.