-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExitFrame.java
More file actions
executable file
·128 lines (108 loc) · 3.48 KB
/
ExitFrame.java
File metadata and controls
executable file
·128 lines (108 loc) · 3.48 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
/**This class creates the exit window when the close button is clicked or user tries to
* click the close button in the menu bar. Asks the user if the data should be saved.*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class ExitFrame extends JFrame
{
//fields for panels
JPanel savePanel;
JPanel noPanel;
JPanel centerPanel;
JPanel headerPanel;
JPanel cancelPanel;
//object that will hold the data
OverallVote voteData;
//constants to hold the size of the window
private final int WINDOW_WIDTH=300;
private final int WINDOW_HEIGHT=100;
/**Constructor that will create the window
* @param n the OverallVote object that contains all of the program data*/
public ExitFrame(OverallVote n)
{
voteData=n;
//set title and size of window
setTitle("Exit Screen");
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
//create the panels
savePanel = new JPanel();
noPanel = new JPanel();
centerPanel = new JPanel();
headerPanel = new JPanel();
cancelPanel = new JPanel();
//set the layout for the panels and content pane
setLayout(new BorderLayout());
centerPanel.setLayout(new FlowLayout());
//create the buttons and register them with the listener class
JButton item = new JButton("Save");
item.addActionListener(new ButtonListener());
savePanel.add(item);
item = new JButton("No");
item.addActionListener(new ButtonListener());
noPanel.add(item);
item = new JButton("Cancel");
item.addActionListener(new ButtonListener());
cancelPanel.add(item);
//create text field and add it to headerPanel
JLabel item1 = new JLabel("Do you want to save data?");
headerPanel.add(item1);
//add panels
centerPanel.add(savePanel);
centerPanel.add(noPanel);
centerPanel.add(cancelPanel);
add(headerPanel,BorderLayout.NORTH);
add(centerPanel,BorderLayout.CENTER);
//remove decorations
setUndecorated(true);
//set window to visible
setVisible(true);
}
/**Private inner listener class*/
private class ButtonListener implements ActionListener
{
/**Method that will handle the event object fired when the buttons are clicked
* @param e object fired by button when event occurs*/
public void actionPerformed(ActionEvent e)
{
//if else if statements to determine which button was clicked
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);
//close the program
System.exit(0);
}
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("No"))
{
//the program will close without saving
System.exit(0);
}
else if(e.getActionCommand().equals("Cancel"))
{
//the program will go back to the home screen
new HomeFrame(voteData);
dispose();
}
else
{
//window will open saying there is a problem with the program
JOptionPane.showMessageDialog(null, "Problem with program. Terminating program.");
System.exit(0);
}
}
}
}