-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeFrame.java
More file actions
executable file
·307 lines (247 loc) · 8.27 KB
/
HomeFrame.java
File metadata and controls
executable file
·307 lines (247 loc) · 8.27 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**Class that will be the first window that opens when program is opened.*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class HomeFrame extends JFrame {
//fields to store panel and buttons and text
private JPanel votePanel;
private JPanel getVotePanel;
private JButton total;
private JButton age;
private JButton gender;
private JButton ethnicity;
private JButton vote;
private JMenuBar mBar;
//constants to set window size
private final int WINDOW_WIDTH = 300;
private final int WINDOW_HEIGHT = 200;
//field to hold the overall vote object which contains all of the voting data
private OverallVote voteData;
/**Constructor that will create the window with all the panels.*/
public HomeFrame(OverallVote n)
{
//store the OverallVote object in the voteData variable
voteData=n;
//set the title of the window
setTitle("Richmond North Centre");
//set window size
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
//create two panel objects
votePanel = new JPanel();
getVotePanel = new JPanel();
//set layout of content pane and getVotePanel
getVotePanel.setLayout(new GridLayout(2,2));
setLayout(new FlowLayout());
//set titled border for getVotePanel
getVotePanel.setBorder(BorderFactory.createTitledBorder("Select Option to Get Vote:"));
//create the buttons, register event listener and add to panels
vote = new JButton("Vote");
vote.addActionListener(new MenuBarActions());
votePanel.add(vote);
total = new JButton("Total");
total.addActionListener(new MenuBarActions());
getVotePanel.add(total);
age = new JButton("Age");
age.addActionListener(new MenuBarActions());
getVotePanel.add(age);
gender = new JButton("Gender");
gender.addActionListener(new MenuBarActions());
getVotePanel.add(gender);
ethnicity = new JButton("Ethnicity");
ethnicity.addActionListener(new MenuBarActions());
getVotePanel.add(ethnicity);
//add the two panels to the content pane
add(getVotePanel);
add(votePanel);
//call MenuBar method to create a menu bar
MenuBarClass();
//add menu bar to the content pane
setJMenuBar(mBar);
//add listener to the close button
addWindowListener(new WindowListen());
//display window
setVisible(true);
}
/**Main method to start the program*/
public static void main(String[] args)
{
//create a new OverallVote object
OverallVote vote;
//try and catch block to catch any exceptions
try
{
//if statement to check if file exists
//if file doesn't exist a new file will be created
if(!(new File("VoteData.dat").isFile()))
{
//create a new file and save the OverallVote object into it
FileOutputStream outStream = new FileOutputStream("VoteData.dat");
ObjectOutputStream objectOutput = new ObjectOutputStream(outStream);
vote=new OverallVote();
objectOutput.writeObject(vote);
}
else
{
//open the file from the saved data
FileInputStream inStream = new FileInputStream("VoteData.dat");
ObjectInputStream objectInput = new ObjectInputStream(inStream);
vote = (OverallVote) objectInput.readObject();
}
//create the home window, pass a copy of the OverallVote object
new HomeFrame(vote.getObject());
}
catch (Exception e)
{
//message sent to user if exception is thrown while opening file
JOptionPane.showMessageDialog(null, "Could not create or open file to save Data to. Program Terminated.");
}
}
/**Method that will create the menu bar*/
public void MenuBarClass()
{
//create a menu bar object
mBar = new JMenuBar();
//create menu objects and store them in the file and getVote fields
JMenu file=new JMenu("File");
JMenu getVote=new JMenu("Get Vote");
//create menu item objects and store them in the JMenuItem fields
JMenuItem item;
item=new JMenuItem("Reset");
item.addActionListener(new MenuBarActions());
file.add(item);
item=new JMenuItem("Vote");
item.addActionListener(new MenuBarActions());
file.add(item);
item=new JMenuItem("Save");
item.addActionListener(new MenuBarActions());
file.add(item);
item=new JMenuItem("Close");
item.addActionListener(new MenuBarActions());
file.add(item);
item=new JMenuItem("Total");
item.addActionListener(new MenuBarActions());
getVote.add(item);
item=new JMenuItem("Gender");
item.addActionListener(new MenuBarActions());
getVote.add(item);
item=new JMenuItem("Age");
item.addActionListener(new MenuBarActions());
getVote.add(item);
item=new JMenuItem("Ethnicity");
item.addActionListener(new MenuBarActions());
getVote.add(item);
//add the two menus to the menu bar
mBar.add(file);
mBar.add(getVote);
}
/**private class that will handle the events fired by the menu items*/
private class MenuBarActions implements ActionListener {
/**Method that is overridden from interface to handle event objects
* @param e the ActionEvent object that is fired from pressing menu items*/
public void actionPerformed(ActionEvent e)
{
//if statements to determine which button was clicked
if(e.getActionCommand().equals("Reset"))
{
//call the ResetFrame to make sure the user wants to reset the program
new ResetFrame(voteData);
dispose();
}
else if (e.getActionCommand().equals("Vote"))
{
//close the home window and create a voting window
//pass along the data object
dispose();
new VoteFrame(voteData.getObject());
}
else if (e.getActionCommand().equals("Save"))
{
//try catch statement needed to save file object
try
{
//change boolean variable in voteData object to true showing that data has been saved
voteData.changeSaved(true);
//save the OverallVote object, therefore saving the data in the program
FileOutputStream outStream = new FileOutputStream("VoteData.dat");
ObjectOutputStream objectOutput = new ObjectOutputStream(outStream);
objectOutput.writeObject(voteData);
}
catch (Exception f)
{
//message sent to user if exception is thrown while saving file
JOptionPane.showMessageDialog(null, "Problem: Did not save.");
}
}
else if (e.getActionCommand().equals("Close"))
{
//if else statement to determine whether the data has been changed
if(voteData.getSaved())
System.exit(0);
else
{
//close the current window and open the exit window
new ExitFrame(voteData);
dispose();
}
}
else if (e.getActionCommand().equals("Total"))
{
//create results window with overall results
VoteResults window=new VoteResults(voteData.getObject());
window.total();
}
else if (e.getActionCommand().equals("Gender"))
{
//create results window with results based on gender
VoteResults window=new VoteResults(voteData.getObject());
window.gender();
}
else if (e.getActionCommand().equals("Age"))
{
//create results window with results based on age
VoteResults window=new VoteResults(voteData.getObject());
window.age();
}
else if (e.getActionCommand().equals("Ethnicity"))
{
//create results window with results based on age
VoteResults window=new VoteResults(voteData.getObject());
window.ethnicity();
}
else
{
//window will open saying there is a problem with the program
JOptionPane.showMessageDialog(null, "Problem with program. Terminating program.");
System.exit(0);
}
}
}
/**Private inner class that is registered to the close button*/
private class WindowListen implements WindowListener
{
//only need to use the windowClosing method
public void windowOpened(WindowEvent e) {}
/**Method will call the exit screen when the close button is clicked
* @param e the object that is fired when the close button is clicked*/
public void windowClosing(WindowEvent e)
{
//if else statement to determine whether the data has been changed
if(voteData.getSaved())
{
System.exit(0);
}
else
{
//close the current window and open the exit window
new ExitFrame(voteData);
dispose();
}
}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
}