Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestClass{
- public static void main(String[] args) {
- new TestClass().foo();
- }
- public void foo() {
- class MyActionListener implements ActionListener {
- private int value;
- public MyActionListener(int value){
- this.value = value;
- }
- @Override
- public void actionPerformed(ActionEvent arg0) {
- System.out.println("You pressed on button " + value + "!");
- }
- }
- JFrame frame = new JFrame();
- JPanel panel = new JPanel();
- for(int i = 0; i < 10; i++){
- JButton button = new JButton("Button " + i);
- button.addActionListener(new MyActionListener(i));
- panel.add(button);
- }
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(panel);
- frame.pack();
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement