Advertisement
Guest User

Untitled

a guest
May 16th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. public class TestClass{
  2.  
  3.     public static void main(String[] args) {
  4.         new TestClass().foo();
  5.     }
  6.  
  7.     public void foo() {
  8.        
  9.         class MyActionListener implements ActionListener {
  10.            
  11.             private int value;
  12.            
  13.             public MyActionListener(int value){
  14.                 this.value = value;
  15.             }
  16.             @Override
  17.             public void actionPerformed(ActionEvent arg0) {
  18.                 System.out.println("You pressed on button " + value + "!");
  19.             }
  20.         }
  21.        
  22.         JFrame frame = new JFrame();
  23.         JPanel panel = new JPanel();
  24.  
  25.         for(int i = 0; i < 10; i++){
  26.             JButton button = new JButton("Button " + i);
  27.             button.addActionListener(new MyActionListener(i));
  28.             panel.add(button);
  29.         }
  30.        
  31.  
  32.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33.        
  34.         frame.add(panel);
  35.  
  36.         frame.pack();
  37.         frame.setVisible(true);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement