Student Grade Calculator using Java Swing
Consider the following scenario. We need to create a GUI based application which calculates grade of students according to marks obtained in 4 subjects. Below are the range of marks for different grades.
|Between 85% – 90%||B|
|Between 80% – 85%||C|
|Between 70% – 80%||D|
|Between 60% – 70%||E|
|Between 50% – 60%||Poor|
We will be using Netbeans to create GUI applications since it’s a well crafted open-source IDE that provides features of a Component inspector, Drag-and-Drop of widgets, Debugger, Object browser, etc.
Steps to create the Student Grade Calculator
1. Create a new Java application by clicking on New Project -> Java -> Java Application and give a suitable project name. Eg: GeeksForGeeks and click Finish.
2. To create a New File under the same Java project GeeksForGeeks, right-click on the project name on the left-hand side of the window, click New -> JFrame Form and give a suitable file name. Eg. Grade_Calulator.java and click Finish.
3. Now from the palette situated at the right-hand side of the window, start dragging the toolkit widgets as per requirements. To change the background color of the frame, we need to first insert a JPanel and change its properties.
4. Click anywhere on the panel area, go to properties -> background.
5. Double click on the background option and select any color of your choice. Then click OK.
6. After setting background color, drag other widgets onto the design area. To display student details like Student ID, Name, Grade in a separate Dialog Box, drag a JOptionPane widget from the palette. First it overlaps the existing design. Go to navigator on left-hand side of the screen, then drag and move the JOptionPane component from JFrame to Other Components. It will go behind.
7. Let us now quickly go through the usage of each component in the program tabulated below.
|Object Type||Object Name||Description|
|Label||jLabel1: ABC Public School, Mumbai||Describes title of the application|
|jLabel2: Student ID||Defines Student ID|
|jLabel3: Student Name||Defines Student Name|
|jLabel4: Section||Defines Section|
|jLabel5: Physics||Defines marks in subject 1|
|jLabel6: Chemistry||Defines marks in subject 2|
|jLabel7: Maths||Defines marks in subject 3|
|jLabel8: English||Defines marks in subject 4|
|jLabel9: Total Marks||Defines total marks|
|jLabel10: Percentage||Defines percentage obtained|
|jLabel11: Grade||Defines grade achieved|
|Panel||jPanel1||Container to hold components|
|Command Button||jButton1: Calculate||Calculates grade|
|jButton2: Clear||Clears content of all components|
|jButton3: Exit||Exists application|
|OptionPane||JOptionPane1||Displays additional information|
8. Now to type the code, double click on jButton1, you will be directed to the source tab. Here type in the following code.
9. Now to clear all textfields write the following code under the jButton2 ActionPerformed option which can be achieved by clicking twice on Clear Button in the design area.
10. Now to exit from the system, use the following statement under the jButton3 ActionPerformed option which can be achieved by clicking twice on exit button in the design area.
11. Now, right-click anywhere on the screen and select Run File option from the drop-down menu. The final output is shown below. Input necessary details and the application is ready!