-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGameItemTestGUI.java
47 lines (32 loc) · 1.17 KB
/
GameItemTestGUI.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class GameItemTestGUI {
public static void main(String[] args) {
ArrayList<GameItem> bag = new ArrayList<GameItem>();
bag.add(new Weapon("Sword", 3, true));
bag.add(new Weapon("Rock", 1, false));
bag.add(new Consumable("Potion", 0.5, "restore health"));
bag.add(new Consumable("Food", 1.5, "sate hunger"));
// ListIterator: see https://www.baeldung.com/java-iterate-list
ListIterator<GameItem> bagIterator = bag.listIterator();
JLabel label = new JLabel(bagIterator.next().toString(), JLabel.CENTER);
JButton buttonNext = new JButton("Next >");
buttonNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (bagIterator.hasNext()) {
label.setText(bagIterator.next().toString());
}
}
});
JPanel panel = new JPanel(new BorderLayout());
panel.add(label, BorderLayout.CENTER);
panel.add(buttonNext, BorderLayout.PAGE_END);
JFrame f = new JFrame();
f.add(panel);
f.setSize(512, 512);
f.setVisible(true);
}
}