Listing 1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyJavaApp
{
public static void main(String[] args)
{
// create a button
JButton button = new JButton("Press me!");
// register the event handler for pressing the button
button.addActionListener(new MyButtonListener());
// organize the layout of the window
JPanel pane = new JPanel();
pane.add(button);
JFrame frame = new JFrame("My Application");
frame.getContentPane().add(pane, BorderLayout.CENTER);
// register the event handler for closing the window
frame.addWindowListener(new MyWindowListener());
frame.pack();
frame.setVisible(true);
}
}
// this class implements the event handler for a button
class MyButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("I was pressed!!!");
}
}
// this class implements the event handler for
// closing the window
class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}