import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.UIManager;


public class AddressbookUI extends JFrame {
    
    private JTextField nameInput;
    private JButton saveButton;

    public AddressbookUI() {
        super("Address Book");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.buildUI();
        this.pack();
    }
    
    private void buildUI() {
        this.nameInput = new JTextField(15);
        this.saveButton = new JButton("Save");
        JLabel nameLabel = new JLabel("Name:");
        
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(this.nameInput, BorderLayout.CENTER);
        contentPane.add(nameLabel, BorderLayout.LINE_START);
        contentPane.add(this.saveButton, BorderLayout.PAGE_END);
        
        this.saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                String message = "Contact " + AddressbookUI.this.nameInput.getText() + " saved.";
                JOptionPane.showMessageDialog(AddressbookUI.this, message);
            }
        });
    }
        
    public static void main(String[] args) {
    
        // We use the crossplatform L&F, since otherwise running the application
        // with ant isn't guaranteed to work.
        try {
            UIManager.setLookAndFeel(
                UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

        AddressbookUI addressbookUI = new AddressbookUI();
        addressbookUI.setVisible(true);
    }

}
