/**
 * This program displays a greeting "Hello <name>"
 * after you enter your name in the textbox
 * and press the 'Enter' key.
 * @author
 * @version
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DisplayName extends JFrame implements ActionListener{
    // instance variables
    private static final int FRAME_WIDTH = 300;
    private static final int FRAME_HEIGHT = 300;

    private static final int FRAME_X_ORIGIN = 150;
    private static final int FRAME_Y_ORIGIN = 250;

    private static final int BUTTON_WIDTH = 80;
    private static final int BUTTON_HEIGHT = 30;

    private JButton cancelButton;
    private JButton okButton;

    private JTextField inputLine;
	private JTextField outputLine;

	private JLabel label;

    public static void main(String[] args){
        DisplayName frame = new DisplayName();
        frame.setVisible(true);
    }

    /**
     * Constructor for objects of class DisplayName
     */
    public DisplayName(){
        Container contentPane;

        //Set the frame properties
        setSize         (FRAME_WIDTH, FRAME_HEIGHT);
        setResizable    (false);
        setTitle        ("Display Name");
        setLocation     (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

        contentPane = getContentPane();
        contentPane.setLayout(null);
		contentPane.setBackground( Color.white );

        //Create and Place the Buttons on the frame
        okButton = new JButton("OK");
        okButton.setBounds(70, 155, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(okButton);

        cancelButton = new JButton("CANCEL");
        cancelButton.setBounds(160, 155, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(cancelButton);

        //Register this frame as an Action listener of the buttons
        okButton.addActionListener(this);
        cancelButton.addActionListener(this);

		  //Create and Place the TextFields on the frame
        inputLine = new JTextField();
        inputLine.setBounds(90, 70, 130, 25);
        contentPane.add(inputLine);

		outputLine = new JTextField();
        outputLine.setBounds(90, 100, 200, 25);
		outputLine.setEditable( false );
        contentPane.add(outputLine);

		//Action listener of the TextField
		inputLine.addActionListener(this);


		//Create and place a label on the frame
		label = new JLabel("Enter your name and press Return.");
		label.setBounds(10,20, 200, 25);
		contentPane.add(label);

		//Exit the program when the viewer is closed
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

	//Event handler
    public void actionPerformed(ActionEvent event){
        if(event.getSource() instanceof JButton){
            JButton clickedButton = (JButton) event.getSource();

            String buttonText = clickedButton.getText();

          	outputLine.setText("You have pressed the " + buttonText + " button.");

				if(buttonText.equals("OK"))
				{
					outputLine.setText("Hello " + inputLine.getText());
				}
				else
				{
					outputLine.setText("You have pressed the " + buttonText + " button.");
				}

        }
        else{
    			outputLine.setText("Hello " + inputLine.getText());
        }
    }
}



