import javax.swing.*; import java.awt.event.*; public class AnonInner { public AnonInner() { //make a window, put a button in it JFrame frame=new JFrame("A Window"); JButton theButton=new JButton("Press me!"); frame.add(theButton); frame.pack(); frame.setVisible(true); //now for the anonymous inner class part; to make the button respond when clicked, we need to add an // 'ActionListener'; a class that has an actionPerformed() method to call when the button is clicked. //ActionListener is an interface, and has 1 method; actionPerformed(). //here, we declare a class inside the '()' of our method call. // we don't name it - we just specify that it implements ActionListener - hence the 'anonymous' part theButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { if(event.getActionCommand().equals("Press me!")) //make sure the action is clicking the button JOptionPane.showMessageDialog(null,"You pressed the button"); } } ); //an alternative: theButton.addActionListener(new SomeClassThatImplementsActionListener()); } public static void main(String[] args) { AnonInner ai=new AnonInner(); } }