In: Computer Science
In Java
Create a simple GUI that gets the exam score and shows the corresponding letter grade by clicking on the “Convert Score to Letter Grade” button.
Here is code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Score extends JFrame implements ActionListener {
JTextField txtdata;
JButton calbtn = new JButton("Calc");
JLabel outputLb = new JLabel();
public Score() {
JPanel myPanel = new JPanel();
add(myPanel);
myPanel.setLayout(new GridLayout(1, 2));
JLabel label = new JLabel("Enter Score : ");
myPanel.add(label);
txtdata = new JTextField();
myPanel.add(txtdata);
myPanel.add(calbtn);
calbtn.addActionListener(this);
myPanel.add(outputLb);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == calbtn) {
int data = Integer.parseInt(txtdata.getText()); //perform your operation
String result = "";
if (data > 100) {
result = "Bad score";
} else if (data >= 85 && data <= 100) {
result = "Letter grade : A";
} else if (data >= 75 && data <= 84) {
result = "Letter grade : B";
} else if (data >= 60 && data <= 74) {
result = "Letter grade : C";
} else if (data >= 50 && data <= 59) {
result = "Letter grade : D";
} else if (data < 0) {
result = "Bad score";
} else if (data < 50) {
result = "Letter grade : F";
} else {
result = "Bad score";
}
outputLb.setText(result);
}
}
public static void main(String args[]) {
Score g = new Score();
g.setLocation(10, 10);
g.setSize(500, 70);
g.setVisible(true);
}
}
Output: