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

/**
  a GUI application including an example of a JTextField,
  with a little additional FlowLayout manager playing

  @author Sharon Tuttle
  @version 2021-10-05
*/

public class FlowLayoutPlay
{
    /**
        creates a simple frame with a panel that includes a JTextField
        (along with a few other components), that plays lightly
        with a FlowLayout manager object

        @param args not used here
     */

    public static void main(String[] args)
    {
	EventQueue.invokeLater(
	    () ->
	       {
		   FlowLayoutPlayFrame mainFrame = new FlowLayoutPlayFrame();
		   mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		   mainFrame.setVisible(true);
	       } );
    }
}

/**
     A frame with a panel that includes a JTextField, and plays
     lightly with a FlowLayout layout manager
 */

class FlowLayoutPlayFrame extends JFrame
{
    // data fields

    private static final int DEFAULT_WIDTH = 475;
    private static final int DEFAULT_HEIGHT = 220;
    
    /**
        construct a FlowLayoutPlayFrame instance
    */

    public FlowLayoutPlayFrame()
    {
	setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	setTitle("A Little FlowLayout Play");

	FlowLayoutPlayPanel fPanel = new FlowLayoutPlayPanel();
	add(fPanel);
    }
}

/**
    A panel containing several elements, including a JTextField,
    and a little playing with a FlowLayout manager object
*/

class FlowLayoutPlayPanel extends JPanel
{
    // data fields!

    private JLabel     visitorGreeting;
    private JTextField visitorNameField;

    private static final Font BIG_FONT =
        new Font("SanSerif", Font.PLAIN, 30);

    /**
        constructs a FlowLayoutPlayPanel instance
    */

    public FlowLayoutPlayPanel()
    {
        // useful, here, to give a name to a FlowLayout object,
	//    since I want to make it the layout manager for this
	//    panel and ALSO call one of its methods

	FlowLayout myFlowLayout = new FlowLayout();

	// ...and make THIS the FlowLayout object for THIS panel

	setLayout(myFlowLayout);

	// ...so I can change the default alignment:

	myFlowLayout.setAlignment(FlowLayout.LEFT);
	//myFlowLayout.setAlignment(FlowLayout.RIGHT);

	// prompt the user for a name

	JLabel visitorPrompt = new JLabel("Type your name: ");
	visitorPrompt.setForeground(Color.BLUE);
	visitorPrompt.setFont(BIG_FONT);
	visitorPrompt.setBorder(new EtchedBorder());

	visitorNameField = new JTextField(10);
	visitorNameField.setFont(BIG_FONT);

	visitorGreeting = new JLabel("No visitor has registered yet");
	visitorGreeting.setForeground(Color.RED);
	visitorGreeting.setFont(BIG_FONT);

	JButton submitMe = new JButton("Submit");
	submitMe.setFont(BIG_FONT);

	// since this JPanel is using FlowLayout,
	//    add components in the order you
	//    want them to appear

	add(visitorPrompt);
	add(visitorNameField);
	add(visitorGreeting);
	add(submitMe);

	// oh yes, create a submission action!
        //   and add it to my submit button's list of listeners
	
	submitMe.addActionListener(new SubmitAction());
    }

    /**
        an action listener that changes the greeting label
        to reflect the entered visitor's name
    */

    private class SubmitAction implements ActionListener
    {
	// default constructor will suffice, in this case

        /**
            changes the greeting label to reflect the entered
            visitor's name

            @param event a push of the Submit button
	*/

	public void actionPerformed(ActionEvent event)
        {
	    visitorGreeting.setText("Hi, " +
				    visitorNameField.getText() );
	    visitorNameField.setColumns(2);
	    //visitorNameField.setEditable(false);
	}
    }
}