-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShoppingListGUI.java
66 lines (52 loc) · 2.01 KB
/
ShoppingListGUI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ShoppingListGUI extends JFrame {
private static final long serialVersionUID = 1L; // serialVersionUID is a constant long that serves as a version control in a Serializable class. It helps to ensure that the class can be deserialized correctly even if changes have been made to the class.
private shoppingList myShopping;
public ShoppingListGUI() {
// Create a Shopping object
myShopping = new shoppingList();
// Set up the GUI
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Shopping List");
setSize(300, 200);
setLocationRelativeTo(null);
// Create buttons
JButton addButton = new JButton("Add Item");
JButton listButton = new JButton("List Items");
// Add action listeners to buttons
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String newItem = JOptionPane.showInputDialog(null, "Enter a new item:", "Add Item", JOptionPane.PLAIN_MESSAGE);
if (newItem != null && !newItem.isEmpty()) {
myShopping.addItem(newItem);
}
}
});
listButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myShopping.listItems();
}
});
// Create a panel and add buttons
JPanel panel = new JPanel();
panel.add(addButton);
panel.add(listButton);
// Add panel to the frame
add(panel);
// Display the frame
setVisible(true);
}
public static void main(String[] args) {
// Create an instance of the GUI
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ShoppingListGUI();
}
});
}
}