SWING
To start building our GUI with swing we first import it:
import javax.swing.*
While at it, we can import a few more useful packages and classes that we will use:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
java.awt.*
contains classes for creating GUI components like buttons, windows etc.
java.awt.event.ActionEvent
classrepresents an action event that happens when a user interacts with a GUI component, like clicking a button.
java.awt.event.ActionListener
interface is used to handle such action events. When a component, such as a button, generates an action event, the ActionListener's actionPerformed
method is called. This method contains the code that responds to the event, allowing you to define what should happen when the user interacts with the component.
So when import what we need, under the class line we should create the following objects witch are used for creating the GUI window. All of this objects have to be used for a GUI.
The objects that can be used for creating a simple GUI with swing are JFrame, JPanel, JButton, JLabel.
public class CoffeeShopGUI
static private JFrame frame;
static private JPanel panel;
static private JButton viewMenuButton;
static private JButton orderButton;
static private JButton seeBasketButton;
static private JButton seeBillButton;
static private JButton payAndExitButton;
Here, frame is the window, panel is a box in the window that holds the buttons, and a button is a button🙂 A good practice is to call the objects like this at the top of the class, and use private and static.
Next thing to remember is that everything related to the window settings is written in the java constructor of the class, because the constructor is where you initialize the frame and set its properties before making it visible.
Window Setting-up
public CoffeeShopGUI() {
//creating the frame object
frame = new JFrame();
//creating the panel object
panel = new JPanel();
//creating the buttons objects with text on the buttons
viewMenuButton = new JButton("View Menu");
orderButton = new JButton("Order");
seeBasketButton = new JButton("See Basket");
seeBillButton = new JButton("See Bill");
payAndExitButton = new JButton("Pay and Exit");
viewMenuButton.addActionListener(this);
orderButton.addActionListener(this);
seeBasketButton.addActionListener(this);
seeBillButton.addActionListener(this);
payAndExitButton.addActionListener(this);
In this code snippet, this
refers to the current object, which is assumed to implement the ActionListener
interface. The addActionListener
method is used to register the current object as an action listener for multiple buttons.
When a button is clicked, an action event is generated, and the actionPerformed
method of the action listener (in this case, the current object) is called. By registering the current object as the action listener for multiple buttons, you can handle the action events from these buttons in a single actionPerformed
method, which can differentiate between the buttons based on the event source.
panel.setBorder(BorderFactory.createEmptyBorder(30, 40, 30, 40));
panel.setLayout(new GridLayout(0, 1));
panel.add(viewMenuButton);
panel.add(orderButton);
panel.add(seeBasketButton);
panel.add(seeBillButton);
panel.add(payAndExitButton);
Here the buttons are added to the panel, dont mind the first 2 rows i dont getthe mesurements as well, saw a lot of people just writing this numbers.
//add the panel in the center of the frame
frame.add(panel, BorderLayout.CENTER);
//Some exit settings
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set title
frame.setTitle("Coffee Shop");
//always write this dunno why
frame.pack();
//make the window visable on screen
frame.setVisible(true);
Functionality
public static void main(String[] args) {
new CoffeeShopGUI();
In the main method of the GUI create a new constructor.
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
The actionPerformed
method is called when an action event occurs, such as a button click. It takes an ActionEvent
parameter (e
), which provides information about the event. The getActionCommand()
method of the ActionEvent
class retrieves the command string associated with the event, which is often the text displayed on a button or the action command set for other components. This allows you to determine which component triggered the event and take appropriate action based on that information.
Now we can use a switch
statement to call the appropriate function depending on what the user clicks.
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
switch (action) {
case "View Menu":
displayMenu();
break;
case "Order":
order();
break;
case "See Basket":
displayBasket();
break;
case "See Bill":
displayTotalPrice();
break;
case "Pay and Exit":
payAndExit();
break;
default:
break;
}
}
the last thing left is to use the object from the main coffee shop code and call the functions that we defined from there.
private void displayMenu() {
CoffeeShop.displayMenu();
}
private void order() {
Scanner input = new Scanner(System.in);
CoffeeShop.order(input);
input.close();
}
private void displayBasket() {
CoffeeShop.displayBasket();
}
private void displayTotalPrice() {
CoffeeShop.displayTotalPrice();
}
private void payAndExit() {
//pop-up message window
JOptionPane.showMessageDialog(frame, "Payment received! Thank you for visiting Mladen's Coffee Shop!");
System.exit(0);
}
}
And like that, using swing we made our simple GUI for our Coffe Shop.

CheetSheets
Last updated