Using an inner ActionListener class. : Event Queue « Event « Java






Using an inner ActionListener class.

  

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

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

public class InnerClass {

  class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      System.exit(0);
    }
  }

  public InnerClass() {
    JButton close = new JButton("Close");
    ButtonListener listener = new ButtonListener();
    close.addActionListener(listener);

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

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

  public static void main(String[] args) {
    new InnerClass();
  }
}

   
    
  








Related examples in the same category

1.Use the Event queue to retrieve eventUse the Event queue to retrieve event
2.Event object has information about an event, that has happened.
3.Register several listeners for one event.
4.ActionEvent.getActionCommand()
5.Adding an InputMap to a Component
6.Event source and listener
7.Multiple sources: A listener can be plugged into several sources.
8.void java.awt.Toolkit.addAWTEventListener(AWTEventListener listener, long eventMask)
9.int java.awt.event.WindowEvent.WINDOW_OPENED
10.JComponent.WHEN_IN_FOCUSED_WINDOW
11.Register action
12.Using EventQueue.invokeLater to start a Swing application