77import navjot .sgpaCalculatorGUI .textVerifier .NumericTextVerifier ;
88
99import javax .swing .*;
10- import javax .swing .border .LineBorder ;
1110import java .awt .*;
1211import java .awt .event .ActionEvent ;
1312import java .awt .event .KeyAdapter ;
@@ -19,12 +18,10 @@ public class GUI {
1918 SUBJECT_COLUMN_TITLE = "Subject number" , GRADE_COLUMN_TITLE = "Grade" , CREDITS_COLUMN_TITLE = "Credits" , DEFAULT_SUBJECTS_COUNT = "8" ,
2019 ERROR_MESSAGE_INVALID_INPUT_IN_SUBJECTS = "Please enter an integral number between 0 and 13 in the number of subjects input" ,
2120 ERROR_MESSAGE_INVALID_INPUT_IN_CREDITS = "Please enter an integral number in the credits input" ;
22- private final static LineBorder GRAY_LINE_BORDER = new LineBorder (Color .GRAY , 1 , true ), BLACK_LINE_BORDER = new LineBorder (Color .BLACK , 1 , true );
2321 private final static Font DEFAULT_TEXT_FONT = new Font (Font .MONOSPACED , Font .PLAIN , 15 );
24- private final static Color [] COLOR_PALETTE = new Color []{Color .BLACK , Color .DARK_GRAY , Color .GRAY , Color .WHITE , Color .LIGHT_GRAY };
2522 private final static int WINDOW_SIDE = 250 ;
26- private final static int WINDOW_SIDE_LARGE = 600 ;
27- private final static int WINDOW_SIDE_MEDIUM = 400 ;
23+ private final static int WINDOW_HEIGHT = 800 ;
24+ private final static int WINDOW_WIDTH = 425 ;
2825 private final JFrame mainFrame ;
2926 private final JPanel mainPanel ;
3027 private final JLabel subjectInputLabel ;
@@ -46,9 +43,8 @@ public GUI() {
4643 initializeTextFieldAndButton ();
4744 initializeCalculateSGPAButton ();
4845 initializeMainPanel ();
49- initializeMainFrame ();
50-
5146 simulateEnteredEightSubjectsAndSubmitButtonClicked ();
47+ initializeMainFrame ();
5248 }
5349
5450 private void simulateEnteredEightSubjectsAndSubmitButtonClicked () {
@@ -58,16 +54,10 @@ private void simulateEnteredEightSubjectsAndSubmitButtonClicked() {
5854
5955 private void initializeTextFieldAndButton () {
6056 subjectInputTextField .setInputVerifier (new NumericTextVerifier ());
61- subjectInputTextField .setBackground (COLOR_PALETTE [1 ]);
62- subjectInputTextField .setBorder (GRAY_LINE_BORDER );
63- subjectInputTextField .setForeground (COLOR_PALETTE [3 ]);
6457 subjectInputTextField .setFont (DEFAULT_TEXT_FONT );
6558
6659 submit .addActionListener (this ::submitButtonClicked );
6760 submit .setFont (DEFAULT_TEXT_FONT );
68- submit .setBackground (COLOR_PALETTE [2 ]);
69- submit .setForeground (COLOR_PALETTE [1 ]);
70- submit .setBorder (BLACK_LINE_BORDER );
7161 submit .addKeyListener (new KeyAdapter () {
7262 @ Override
7363 public void keyPressed (KeyEvent e ) {
@@ -100,7 +90,9 @@ private void submitButtonClicked(ActionEvent actionEvent) {
10090
10191 mainPanel .removeAll ();
10292 initializeMainPanel ();
103- mainPanel .setPreferredSize (new Dimension (WINDOW_SIDE_MEDIUM , WINDOW_SIDE_LARGE ));
93+ int height = (int ) (mainPanel .getLayout ().preferredLayoutSize (mainPanel ).getHeight () * (Math .pow ((size + 1 ), 1.00003 )));
94+ height = Math .max (Math .min (height , WINDOW_HEIGHT ), WINDOW_SIDE );
95+ mainPanel .setPreferredSize (new Dimension (WINDOW_WIDTH , height ));
10496
10597 initializeNewItems (size );
10698 addNewItemsToMainPanel ();
@@ -114,12 +106,10 @@ private void initializeNewItems(int size) {
114106
115107 for (int i = 0 ; i < subjects .length ; i ++) {
116108 subjects [i ] = new JLabel (SUBJECT + (i + 1 ));
117- subjects [i ].setForeground (COLOR_PALETTE [4 ]);
118109 subjects [i ].setFont (DEFAULT_TEXT_FONT );
119110 }
120111 for (int i = 0 ; i < gradeOptions .length ; i ++) {
121112 gradeOptions [i ] = new JComboBox <>();
122- gradeOptions [i ].setBackground (COLOR_PALETTE [2 ]);
123113 gradeOptions [i ].setFont (DEFAULT_TEXT_FONT );
124114
125115 for (Grade value : Grade .values ()) {
@@ -129,17 +119,11 @@ private void initializeNewItems(int size) {
129119 for (int i = 0 ; i < credits .length ; i ++) {
130120 credits [i ] = new FocusJTextField ();
131121 credits [i ].setInputVerifier (new NumericTextVerifier ());
132- credits [i ].setBackground (COLOR_PALETTE [1 ]);
133- credits [i ].setBorder (GRAY_LINE_BORDER );
134- credits [i ].setForeground (COLOR_PALETTE [3 ]);
135122 credits [i ].setFont (DEFAULT_TEXT_FONT );
136123 }
137124 }
138125
139126 private void initializeCalculateSGPAButton () {
140- calculateSGPA .setBackground (COLOR_PALETTE [2 ]);
141- calculateSGPA .setForeground (COLOR_PALETTE [1 ]);
142- calculateSGPA .setBorder (BLACK_LINE_BORDER );
143127 calculateSGPA .addActionListener (this ::calculateSGPA );
144128 calculateSGPA .setFont (DEFAULT_TEXT_FONT );
145129 calculateSGPA .addKeyListener (new KeyAdapter () {
@@ -166,7 +150,6 @@ private void addNewItemsToMainPanel() {
166150 g .gridwidth = 1 ;
167151
168152 JLabel subjectColumnTitle = new JLabel (SUBJECT_COLUMN_TITLE );
169- subjectColumnTitle .setForeground (COLOR_PALETTE [3 ]);
170153 subjectColumnTitle .setFont (DEFAULT_TEXT_FONT );
171154
172155 mainPanel .add (subjectColumnTitle , g );
@@ -175,7 +158,6 @@ private void addNewItemsToMainPanel() {
175158 g .gridx ++;
176159
177160 JLabel gradeColumnTitle = new JLabel (GRADE_COLUMN_TITLE );
178- gradeColumnTitle .setForeground (COLOR_PALETTE [3 ]);
179161 gradeColumnTitle .setFont (DEFAULT_TEXT_FONT );
180162
181163 mainPanel .add (gradeColumnTitle , g );
@@ -184,7 +166,6 @@ private void addNewItemsToMainPanel() {
184166 g .gridx ++;
185167
186168 JLabel creditsColumnTitle = new JLabel (CREDITS_COLUMN_TITLE );
187- creditsColumnTitle .setForeground (COLOR_PALETTE [3 ]);
188169 creditsColumnTitle .setFont (DEFAULT_TEXT_FONT );
189170
190171 mainPanel .add (creditsColumnTitle , g );
@@ -238,7 +219,6 @@ private void calculateSGPA(ActionEvent actionEvent) {
238219 }
239220
240221 private void initializeMainPanel () {
241- mainPanel .setBackground (COLOR_PALETTE [1 ]);
242222 mainPanel .setPreferredSize (new Dimension (WINDOW_SIDE , WINDOW_SIDE ));
243223 mainPanel .setLayout (new GridBagLayout ());
244224 GridBagConstraints g = new GridBagConstraints ();
@@ -250,7 +230,6 @@ private void initializeMainPanel() {
250230 g .gridx = 0 ;
251231 g .gridy = 0 ;
252232
253- subjectInputLabel .setForeground (COLOR_PALETTE [3 ]);
254233 subjectInputLabel .setFont (DEFAULT_TEXT_FONT );
255234
256235 mainPanel .add (subjectInputLabel , g );
@@ -274,7 +253,6 @@ private void initializeMainPanel() {
274253 g .weightx = 0.0 ;
275254
276255 JLabel credits = new JLabel (CREDITS , SwingConstants .CENTER );
277- credits .setForeground (COLOR_PALETTE [3 ]);
278256 credits .setFont (DEFAULT_TEXT_FONT );
279257
280258 mainPanel .add (credits , g );
@@ -285,6 +263,7 @@ private void initializeMainFrame() {
285263 mainFrame .setDefaultCloseOperation (WindowConstants .EXIT_ON_CLOSE );
286264 mainFrame .pack ();
287265 mainFrame .setTitle (TITLE_OF_APPLICATION );
266+ mainFrame .setLocationRelativeTo (null );
288267 mainFrame .setVisible (true );
289268 }
290269}
0 commit comments