//Import swing classes for building the GUI
import javax.swing.*;
//import AWT's event-handling classes
import java.awt.event.*;
//We also need to import the specific layout manager classes (we'll just
//include everything in the awt package here...)
import java.awt.*;


/**
 * The GUI will be a custon frame, with components.  We need to implement the
 * ActionListener interface so that we can pick up button presses.  The
 * MyGUI object is now an ActionListener object, and listens for such events.
 */
public class MyGUI extends JFrame implements ActionListener{
  //A place to enter text
  JTextField textField;
  //A button to press
  JButton button;
  //The GUI will have a main panel, and a panel on the left with 2 components
  JPanel mainPanel, leftPanel;



  /**
   * The constructor for the MyGUI object will build the actual GUI.
   * Alternatively, we could define some other method, like createGUI(), that
   * would do the same thing.  It would be called in the constructor.  Such a
   * method would simplify the constructor and make it easier to read,
   * which would be useful if we had to do other things in the constructor
   */
  public MyGUI() {
    //Create the main panel. Give it a FlowLayout (components go left-to-right)
    mainPanel = new JPanel(new FlowLayout());
    //Add the panel to the frame (this).  Notice the getContentPane() call
    this.getContentPane().add(mainPanel);

    //Create the left panel, with a GridLayout
    leftPanel = new JPanel(new GridLayout(2,1));
    //Add it to the left side of the main panel.
    mainPanel.add(leftPanel);

    //Add a label to the top of the left panel.  Notice that we simply
    //create a new label in the argument of the function, since we dont need
    //a handle on it.
    leftPanel.add(new JLabel("Enter some text below"));
    //Create the text field
    textField = new JTextField();
    //Add an ActionListener to the textField to detect if Enter was pushed
    textField.addActionListener(this);
    //Add the textField to the leftPanel
    leftPanel.add(textField);

    //Create the button and add this object as an ActionListener for the button
    button = new JButton("Print text");
    button.addActionListener(this);
    //Add it to the mainPanel. It will go to the right of the leftPanel, since
    //mainPanel uses the FlowLayout
    mainPanel.add(button);

    //Specify that the program should exit when this window is closed
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Set a title if you want....
    this.setTitle("A Sample GUI");
    //Pack all the object together and show the window
    this.pack();
    this.show();
  }



  /**
   * The ActionPerformed() method must be defined in this class, since the
   * MyGUI class implements the ActionListener interface.  The ActionPerformed()
   * method is the only method in the ActionListener interface.
   */
  public void actionPerformed(ActionEvent e) {
    //Check the source of the ActionEvent.  If it was the button object:
    if(e.getSource() == button) {
      System.out.println("You wrote: " + textField.getText());
    }
    //If it was the textField object:
    else if(e.getSource() == textField) {
      System.out.println("You pressed Enter: " + textField.getText());
    }
  }



  /**
   * Main method.  Just create an instance of the MyGUI class.
   */
  public static void main(String[] args) {
    MyGUI gui = new MyGUI();
  }

}