// Allison Obourn
// CS 142, Spring 2024
// Lecture 27

// Opens up a new GUI window that contains four buttons. Three are at the
// top and just big enough to fit their contents. The fourth is at the
// bottom of the window and stretched to fit the width. The program will 
// end when the window's upper right x is pressed. 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 

public class HelloWorldGUI {
   public static void main(String[] args) {
      // For those of you who are running your GUI on OSX, you may want to include 
      // the following try/catch. Otherwise you will not be able to see the result
      // of setting any background colors. 
     try {
       UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
     } catch (Exception e) {
       System.out.println("An error occurred when setting the look and feel");
     }
     JFrame window = new JFrame("this is my title");
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     window.setSize(300, 200);
    
     ActionListener listener = new HelloActionListener();    
     JButton button = createButton("button 1", Color.RED, listener);
     JButton second = createButton("2", Color.BLUE, listener);
     JButton yellow = createButton("button 3", Color.YELLOW, listener);
     JButton green = createButton("button 4", Color.GREEN, listener);  
     
     // adds a single button to the bottom of the window   
     window.add(yellow, BorderLayout.SOUTH);
     
     // adds several buttons to a panel and places that at the top
     JPanel panel = new JPanel();
     panel.add(button);
     panel.add(second);
     panel.add(green);
     window.add(panel, BorderLayout.NORTH);

     window.setVisible(true);
   }
   
   // returns a new JButton with the passed in text displayed on it, a background color 
   // of the passed in color and sets the passed in listener to go off when the button is 
   // pressed.
   public static JButton createButton(String text, Color color, ActionListener listener) {
     JButton other = new JButton(text);
     other.setBackground(color);
     other.addActionListener(listener);
     return other;
   }
}
