import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/** DemoIconButton - program to demonstrate using icons on buttons.<p>
*
* A <code>JButton</code> can contain text, an image, or both.
* To display an image within a button, the constructor is
* passed an <code>Icon</code> object as an argument.<p>
*
* Screen snap...<br>
* <center><img src="DemoIconButton-1.gif"></center><p>
*
* @see <a href="DemoIconButton.java">source code</a>
* @author Scott MacKenzie, 2003
*/
public class DemoIconButton
{
   public static void main(String[] args)
   {
      DemoIconButtonFrame frame = new DemoIconButtonFrame();
      frame.setTitle("DemoIconButton");
      frame.pack();
      frame.show();
   }
}

class DemoIconButtonFrame extends JFrame implements ActionListener 
{
   private int count;
   private JTextField countField;
   private JButton decrementButton;
   private JButton incrementButton;
   private JButton exitButton;

   // constructor

   public DemoIconButtonFrame()
   {
      count = 5;

      // -------------------------------
      // create and configure components
      // -------------------------------

      countField = new JTextField("" + count);
      countField.setFont(new Font("Serif", Font.BOLD, 28));
      countField.setForeground(Color.blue);
      countField.setBackground(Color.pink);
      countField.setHorizontalAlignment(SwingConstants.CENTER);
		countField.setEditable(false);

      ImageIcon decIcon = new ImageIcon("decrementIcon.gif");
      ImageIcon incIcon = new ImageIcon("incrementIcon.gif");

      decrementButton = new JButton(decIcon);
      incrementButton = new JButton(incIcon);

      exitButton = new JButton("Exit");

      // -------------
      // add listeners
      // -------------

      decrementButton.addActionListener(this);
      incrementButton.addActionListener(this);
      exitButton.addActionListener(this);
      this.addWindowListener(new WindowCloser());

      // ------------------
      // arrange components
      // ------------------

      // put components in panels

      JPanel topPanel = new JPanel();
      topPanel.setLayout(new GridLayout(1, 2));
      topPanel.add(decrementButton);
      topPanel.add(incrementButton);

      JPanel bottomPanel = new JPanel();
      bottomPanel.setBorder(BorderFactory.createEmptyBorder(50, 30, 50, 30));
      bottomPanel.setLayout(new GridLayout(3, 1));
      bottomPanel.add(topPanel);
      bottomPanel.add(countField);
      bottomPanel.add(exitButton);

      // create a container for this (extended) JFrame's content pane

      Container contentPane = this.getContentPane();

      //  add panels to the container

      contentPane.add(bottomPanel, "Center");
   }

   // -------------------------------
   // implement ActionListener method
   // -------------------------------

   public void actionPerformed(ActionEvent ae)
   {
      if (ae.getSource() == decrementButton)
      {
         count--;
         countField.setText("" + count);
      }
      else if (ae.getSource() == incrementButton)
      {
         count++;
         countField.setText("" + count);
      }

      else if (ae.getSource() == exitButton)
         System.exit(0);   
   }

   // Note: WindowAdapter implements WindowListener

   private class WindowCloser extends WindowAdapter
   {
      public void windowClosing(WindowEvent event)
      {
         System.exit(0);
      }
   }
}

