Well, may be i am little bad with explaining all this. Let me quote an example here.
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.*;
public class Test extends JFrame {
public Test( ) {
setSize(200, 200);
setLocation(200, 200);
JButton button = new JButton("Click");
button.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
System.out.println("You clicked me.");
}
});
Container content = getContentPane( );
content.setLayout(new FlowLayout( ));
content.add(button);
}
public static void main(String[] args) {
JFrame frame = new Test( );
frame.addWindowListener(new WindowAdapter( ) {
public void windowClosing(WindowEvent we) { System.exit(0); }
});
frame.setVisible(true);
}
}
This example might help you in what are you asking about.