-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardLayoutDemo.java
116 lines (91 loc) · 3.33 KB
/
CardLayoutDemo.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//John Goulden
//CardLayout demo
//with student JPanel widgets
//Original version OCCC Spring 2015
//This version OCCC Spring 2021
//CardLayout example based on code found at
//http://www.java-tips.org/java-se-tips/java.awt/how-to-use-awt-cardlayout.html
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.util.Vector;
public class CardLayoutDemo extends JFrame {
private int currentCard = 1;
private JPanel cardPanel;
private CardLayout cl;
public CardLayoutDemo() {
setTitle("Card Layout Example - Student JPanel Widgets");
setSize(600, 600);
cardPanel = new JPanel();
cl = new CardLayout();
cardPanel.setLayout(cl);
/////////////////////////////////////////////////////
Vector<JPanel> jp = new Vector<JPanel>();
////////////////////////////////////////////////////
// SAMPLE
// jp.add(new JDG_MusicPlayer());
// jp.add(new JDG_EmployeeHourlyPayDemo());
// jp.add(new JDG_ISSFinder());
// jp.add(new JDG_GoogleMapsDemo());
///////////////////////////////////////////////////////
// ADD WIDGETS HERE
jp.add(new TTT_CurrencyConverter());
jp.add(new TTT_TemperatureConverter());
jp.add(new TTT_TicTacToe());
jp.add(new TTT_DrawingTable());
///////////////////////////////////////////////////////
for(int i = 0; i < jp.size(); i++){
cardPanel.add(jp.get(i), (i+1)+"");
}
//////////////////////////////////////////////////////
JPanel buttonPanel = new JPanel();
JButton firstBtn = new JButton("First");
JButton nextBtn = new JButton("Next");
JButton previousBtn = new JButton("Previous");
JButton lastBtn = new JButton("Last");
buttonPanel.add(firstBtn);
buttonPanel.add(previousBtn);
buttonPanel.add(nextBtn);
buttonPanel.add(lastBtn);
firstBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
cl.first(cardPanel);
currentCard = 1;
}
});
lastBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
cl.last(cardPanel);
currentCard = jp.size();
}
});
nextBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (currentCard < jp.size()) {
currentCard += 1;
cl.show(cardPanel, "" + (currentCard));
}
}
});
previousBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (currentCard > 1) {
currentCard -= 1;
cl.show(cardPanel, "" + (currentCard));
}
}
});
getContentPane().add(cardPanel, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
CardLayoutDemo cl = new CardLayoutDemo();
cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cl.setVisible(true);
}
}