-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewClassScreen.java
251 lines (231 loc) · 7.78 KB
/
NewClassScreen.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
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
package ws.thurn.dossier;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* .
*/
/**
* The NewClassScreen class contains the GUI and methods for adding a new class
* to the program.
*
* @author Derek Thurn
*/
public class NewClassScreen implements ActionListener
{
JLabel Title;
JPanel top;
JPanel opt;
JLabel info;
JButton create;
JButton cancel;
JPanel upper;
JTextField schoolNameField;
JLabel schoolNameLabel;
JTextField teacherNameField;
JLabel teacherNameLabel;
JTextField classNameField;
JLabel classNameLabel;
JPanel low;
JPanel mid;
JPanel doIt;
JPanel classInfo;
JFrame newClass;
/**
* Creates a new screen for adding a class to the program.
*/
public NewClassScreen()
{
init();
}
/**
* Displays the GUI for the screen
*
* @pre the correct objects have been declared
* @post the GUI is displayed.
*/
public void init()
{
newClass = new JFrame();
newClass.setTitle( "New Class" );
newClass.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container c = newClass.getContentPane();
BoxLayout thisLayout = new BoxLayout( newClass.getContentPane(),
javax.swing.BoxLayout.Y_AXIS );
newClass.setLayout( thisLayout );
{
top = new JPanel();
c.add( top );
top.setPreferredSize( new java.awt.Dimension( 292, 40 ) );
{
Title = new JLabel();
top.add( Title );
Title.setLayout( null );
Title.setText( "Create A New Class" );
Title.setFont( new java.awt.Font( "Comic Sans MS", 1, 20 ) );
}
}
{
classInfo = new JPanel();
BoxLayout classInfoLayout = new BoxLayout( classInfo,
javax.swing.BoxLayout.Y_AXIS );
classInfo.setLayout( classInfoLayout );
c.add( classInfo );
classInfo.setSize( 50, 50 );
{
upper = new JPanel();
FlowLayout upperLayout = new FlowLayout();
upper.setLayout( upperLayout );
classInfo.add( upper );
upper.setPreferredSize( new java.awt.Dimension( 292, 60 ) );
{
classNameLabel = new JLabel();
upper.add( classNameLabel );
classNameLabel.setText( "Class Name:" );
}
{
classNameField = new JTextField();
upper.add( classNameField );
classNameField.setPreferredSize( new java.awt.Dimension(
139, 20 ) );
}
}
{
mid = new JPanel();
FlowLayout midLayout = new FlowLayout();
mid.setLayout( midLayout );
classInfo.add( mid );
mid.setPreferredSize( new java.awt.Dimension( 292, 54 ) );
{
teacherNameLabel = new JLabel();
mid.add( teacherNameLabel );
teacherNameLabel.setText( "Teacher Name*:" );
teacherNameLabel.setOpaque( true );
}
{
teacherNameField = new JTextField();
mid.add( teacherNameField );
teacherNameField.setSize( 6, 20 );
teacherNameField.setPreferredSize( new java.awt.Dimension(
150, 20 ) );
}
}
{
low = new JPanel();
FlowLayout lowLayout = new FlowLayout();
low.setLayout( lowLayout );
classInfo.add( low );
low.setPreferredSize( new java.awt.Dimension( 292, 58 ) );
{
schoolNameLabel = new JLabel();
low.add( schoolNameLabel );
schoolNameLabel.setText( "School Name*:" );
}
{
schoolNameField = new JTextField();
low.add( schoolNameField );
schoolNameField.setPreferredSize( new java.awt.Dimension(
146, 20 ) );
}
}
}
{
opt = new JPanel();
c.add( opt );
{
info = new JLabel();
opt.add( info );
info.setText( "* Optional" );
}
}
{
doIt = new JPanel();
c.add( doIt );
doIt.setPreferredSize( new java.awt.Dimension( 292, 43 ) );
{
create = new JButton();
doIt.add( create );
create.setText( "Create" );
}
{
cancel = new JButton();
doIt.add( cancel );
cancel.setText( "Cancel" );
}
}
newClass.setSize( 300, 300 );
newClass.setVisible( true );
newClass.setTitle( "Add a new class." );
GradeBook.center( newClass );
create.addActionListener( this );
cancel.addActionListener( this );
schoolNameField.addActionListener( this );
teacherNameField.addActionListener( this );
classNameField.addActionListener( this );
}
/**
* Listens for button presses
*
* @pre listeners have been added
* @post the correct action is taken after an event.
*/
public void actionPerformed( ActionEvent evt )
{
if( evt.getSource().equals( cancel ) )
{
newClass.dispose();
new ClassesScreen();
}
if( evt.getSource().equals( create ) )
{
if( ( classNameField.getText() ).equals( "" ) )
{
JOptionPane.showMessageDialog( null,
"The class name field cannot be blank.", "Error",
JOptionPane.ERROR_MESSAGE );
return;
}
if( classNameField.getText().length() > GradeBook.PROGRAM_LIMIT )
{
GradeBook
.error( "The class name is too long. Please shorten it." );
return;
}
else
{
String cn = classNameField.getText();
String tn;
String sn;
if( teacherNameField.getText() != null )
tn = teacherNameField.getText();
else
tn = "";
if( schoolNameField.getText() != null )
sn = schoolNameField.getText();
else
sn = "";
Klass temp = new Klass( cn, tn, sn );
if( GradeBook.isDuplicate( temp ) )
{
JOptionPane.showMessageDialog( null,
"A class with this name already exists.", "Error",
JOptionPane.ERROR_MESSAGE );
}
else
{
GradeBook.addClass( temp );
newClass.dispose();
new ClassesScreen();
}
}
}
}
}