/*
	The program gets a name from the user using e.getSource
	Then it outputs the name to the screen
	
*
*/

import javax.swing.*;
import java.awt.event.*;

public class NameFrame extends JFrame
{
	JPanel panel;
	JButton okbutton;
	JLabel msg;
	JTextField txtfield;
	
	public NameFrame()
	{
		// Sets the title of the frame
		
		setTitle("Name Window");
		
		// Sets the size of the window
		
		setSize(300, 400);
		
		// Sets the action when window is close
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Sets the visibility of the window
		
		setVisible(true);
		
		// Creates the panel
		
		panel = new JPanel();
		
		// Create a label
		
		msg = new JLabel("Enter a name: ");
		
		// Creates the text field
		
		txtfield = new JTextField(15);
		
		// Creates the button
		
		okbutton = new JButton("OK");
		
		// Adds to the panel
		
		panel.add(msg);
		panel.add(txtfield);
		panel.add(okbutton);
		
		// Add the panel to the content pane
		
		add(panel);
		
		// Creates the action listener
		
		okbutton.addActionListener(new ButtonListener());
		
	}
	
	// Handles the event if a button is clicked
	
	public class ButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			// If ok button is clicked, gets the input name
			
			if(e.getSource()==okbutton)
			{
				// Gets the name from the text field
				
				String name = txtfield.getText();
				
				// If the text field is empty return a msg
				
				if(name.length() == 0)
				{
					JOptionPane.showMessageDialog(null, "You didn't enter anything");
				}
				else
				{
					JOptionPane.showMessageDialog(null, "Hello!! " + name);
					
				}
				txtfield.requestFocus();
			}
		}
	}
	
	public static void main(String[] args)
	{
		new NameFrame();
		
	}
}