You are on page 1of 3

Code :

package expt;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ExamResultGUI extends JFrame implements ActionListener {

private JLabel subject1Label, subject2Label,


subject3Label,nameLabel,moblabel,branchlabel;
private JTextField subject1Field, subject2Field, subject3Field,
nameField,mobfield,branchfield;
private JButton calculateButton;

public ExamResultGUI() {
// Set up the main window
setTitle("Student Exam Marks");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setLocationRelativeTo(null);

// Create the input fields for each subject


nameLabel= new JLabel("Student Name");
nameField= new JTextField(20);
branchlabel= new JLabel("Branch");
branchfield= new JTextField(20);
moblabel= new JLabel("Mobile No.");
mobfield= new JTextField(20);
subject1Label = new JLabel("Maths");
subject1Field = new JTextField(20);
subject2Label = new JLabel("Physics");
subject2Field = new JTextField(20);
subject3Label = new JLabel("Chemistry");
subject3Field = new JTextField(20);

// Create the button to calculate the result


calculateButton = new JButton("Calculate Result");
calculateButton.addActionListener(this);

// Set up the main panel


JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(7, 2));
mainPanel.add(nameLabel);
mainPanel.add(nameField);
mainPanel.add(branchlabel);
mainPanel.add(branchfield);
mainPanel.add(moblabel);
mainPanel.add(mobfield);
mainPanel.add(subject1Label);
mainPanel.add(subject1Field);
mainPanel.add(subject2Label);
mainPanel.add(subject2Field);
mainPanel.add(subject3Label);
mainPanel.add(subject3Field);
mainPanel.add(calculateButton);
// Add the main panel to the window
getContentPane().add(mainPanel);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


if (e.getSource() == calculateButton) {
// Get the marks for each subject from the input field
double mark1 = Double.parseDouble(subject1Field.getText());
double mark2 = Double.parseDouble(subject2Field.getText());
double mark3 = Double.parseDouble(subject3Field.getText());

// Calculate the total marks and percentage


double totalMarks = mark1 + mark2 + mark3;
double percentage = totalMarks / 3;

// Display the result in a separate window


JFrame resultFrame = new JFrame("Result");
resultFrame.setSize(500, 300);
resultFrame.setLocationRelativeTo(null);
JLabel nameLabel = new JLabel("Name :"+nameField.getText());
JLabel Branch = new JLabel("Branch : "+branchfield.getText()
);
JLabel mobile = new JLabel("Mobile no.
:"+mobfield.getText());
JLabel marksLabel = new JLabel("Total Marks: " +
totalMarks);
JLabel resultLabel = new JLabel("Percentage " + percentage);

resultFrame.setLayout(new GridLayout(5, 2));


resultFrame.add(nameLabel);
resultFrame.add(Branch);
resultFrame.add(mobile);
resultFrame.add(resultLabel);
resultFrame.add(marksLabel);
resultFrame.setVisible(true);
getContentPane().add(resultFrame);

}
}

public static void main(String[] args) {


ExamResultGUI gui = new ExamResultGUI();
}
}
Output :

You might also like