Anonymous inner class : Anonymous class « Class « Java






Anonymous inner class

  

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class SimpleEvent {

  public static void main(String[] args) {
    JButton close = new JButton("Close");
    close.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        System.exit(0);
      }

    });
    JFrame f = new JFrame();
    f.add(close);

    f.setSize(300, 200);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}

   
    
  








Related examples in the same category

1.an example of a simple anonymous class
2.Tick Tock with an Anonymous Class
3.Access inner class from outside
4.This program demonstrates anonymous inner classes