AgentE382

ActionEvent GetSource Example

Apr 11th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. public class PigPanel extends JPanel
  2. {
  3.     private JButton rollButton, passButton;
  4.    
  5.     public PigPanel()
  6.     {
  7.         rollButton = new JButton("Roll");
  8.         rollButton.setPreferredSize(new Dimension(550, 60));
  9.         rollButton.addActionListener(new ButtonListener());
  10.         add(rollButton);
  11.        
  12.         passButton = new JButton("Pass it!");
  13.         passButton.setPreferredSize(new Dimension(150, 50));
  14.         passButton.addActionListener(new ButtonListener());
  15.         add(rollButton);
  16.     }
  17.    
  18.     private class ButtonListener implements ActionListener
  19.     {
  20.         public void actionPerformed(ActionEvent event)
  21.         {
  22.             if (event.getSource() == rollButton)
  23.             {
  24.                 // do one thing
  25.             }
  26.             else if (event.getSource() == passButton)
  27.             {
  28.                 // do another thing
  29.             }
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment