// Allison Obourn
// CS& 141, Winter 2021
// Lecture 17

// An object template class that creates a GUI when constructed. This GUI
// contains a textbox and two buttons, a blue on on the left and a red
// to its right. When the buttons are pressed a dialog box pops up. 
// It contains the text "An event occurred!" and the text of the button that 
// triggered it.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GUIExample1GUI {
    private JTextField text;

    public GUIExample1GUI() {
        JFrame window = new JFrame();
        window.setSize(new Dimension(300, 100));
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(new FlowLayout());
        
        text = new JTextField(10);
        window.add(text);
        
        JButton button1 = new JButton("This is a button");
        button1.setBackground(Color.BLUE); 
        MyActionListener listener = new MyActionListener();
        button1.addActionListener(listener);
        window.add(button1);
        
        JButton button2 = new JButton("another button");
        button2.setBackground(Color.RED); 
        button2.addActionListener(listener);
        window.add(button2);
        
        window.setVisible(true);
    }
    
    // We moved our event listener class inside of our GUI class and made it
    // private so only our GUI class can access it
    private class MyActionListener implements ActionListener {

       // shows a dialog box when the event is triggered
       public void actionPerformed(ActionEvent event){
           JOptionPane.showMessageDialog(null, "An event occurred! " + text.getText());
           
       }
   }
}


