here is my code for our finals. a grading system were the grades are all in 24 JComboBox. some with numbers till 10, 20 and 30. each quarter they are computed and shown in a JTextfield. when the four quarters are complete a Jbutton "COMPUTE ALL" calculates the entire data and will show in another frame the student's grade and weather he or she have passed or failed. this code works good with some problems: (1) my Jbutton "CLEAR ALL" doesn't work. it should show the default number "0" when clicked and the Jtextfields must be empty-(THIS ALREADY WORKS "TEXTFIELD TO EMPTY". (2) i want to hide the second frame when the Jbutton for compute all is clicked (f2.hide();f3.show();) but it keeps saying (cannot find symbol
symbol: variable f2cannot find symbol) (3) and lastly in my 3rd frame where the student's final grade and final rating are shown i cant really have it shown in a message dialog stating weather he or she passed or failed. THANKS. sorry for the poor coding.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class forfinals extends JFrame{
JFrame f1 = new JFrame ("HELLO");
JLabel Lb1 = new JLabel ("Enter your name");
JLabel Lb2 = new JLabel ("QUIZZES (20)");
JLabel Lb3 = new JLabel ("ASSIGNMENTS (10)");
JLabel Lb4 = new JLabel ("PROJECTS (20)");
JLabel Lb5 = new JLabel ("PARTICIPATION (10)");
JLabel Lb6 = new JLabel ("ATTENDANCE(10)");
JLabel Lb7 = new JLabel ("MAJOR EXAM (30)");
JLabel Lb8 = new JLabel ("Western College SY: 2011-2012");
JLabel Lb9 = new JLabel ("Final Grade");
JLabel Lb10 = new JLabel ("PRELIM");
JLabel Lb11 = new JLabel ("MIDTERM");
JLabel Lb12 = new JLabel ("PREFINAL");
JLabel Lb13 = new JLabel ("FINALS");
JLabel label1 = new JLabel ("YOUR FINAL GRADE");
JLabel label2 = new JLabel ("YOU EARNED THE RATING");
JTextField txt1 = new JTextField (15);
JTextField txt2 = new JTextField (2);
JTextField txt3 = new JTextField (2);
JTextField txt4 = new JTextField (2);
JTextField txt5 = new JTextField (2);
JTextField finalg = new JTextField (5);
JTextField finalr = new JTextField (5);
JButton Btn1 = new JButton ("OK");
JButton Btn2 = new JButton ("CANCEL");
JButton Btn3 = new JButton ("COMPUTE");
JButton Btn4 = new JButton ("COMPUTE");
JButton Btn5 = new JButton ("COMPUTE");
JButton Btn6 = new JButton ("COMPUTE");
JButton jcompute = new JButton ("COMPUTE ALL");
JButton jclear = new JButton ("CLEAR ALL");
JButton jexit = new JButton ("EXIT");
JButton finalb = new JButton ("OK");
//prelim
JComboBox cb1 = new JComboBox();
JComboBox cb2 = new JComboBox();
JComboBox cb3 = new JComboBox();
JComboBox cb4 = new JComboBox();
JComboBox cb5 = new JComboBox();
JComboBox cb6 = new JComboBox();
//midterm
JComboBox cb7 = new JComboBox();
JComboBox cb8 = new JComboBox();
JComboBox cb9 = new JComboBox();
JComboBox cb10 = new JComboBox();
JComboBox cb11 = new JComboBox();
JComboBox cb12 = new JComboBox();
//prefinal
JComboBox cb13 = new JComboBox();
JComboBox cb14 = new JComboBox();
JComboBox cb15 = new JComboBox();
JComboBox cb16 = new JComboBox();
JComboBox cb17 = new JComboBox();
JComboBox cb18 = new JComboBox();
//finals
JComboBox cb19 = new JComboBox();
JComboBox cb20 = new JComboBox();
JComboBox cb21 = new JComboBox();
JComboBox cb22 = new JComboBox();
JComboBox cb23 = new JComboBox();
JComboBox cb24 = new JComboBox();
public forfinals(){
f1.getContentPane().setLayout(null);
f1.setSize (300,350);
f1.getContentPane().add(Lb1);
f1.getContentPane().add(txt1);
f1.getContentPane().add(Btn1);
f1.getContentPane().add(Btn2);
Lb1.setBounds(40,70,100,75);
txt1.setBounds(150,90,100,30);
Btn1.setBounds(40,170,100,40);
Btn2.setBounds(150,170,100,40);
Btn1.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String x;
x = txt1.getText();
JFrame f2 = new JFrame (x);
f2.getContentPane().setLayout(null);
f2.setSize (830,600);
f1.hide();
f2.show();
f2.getContentPane().add(Lb2);
f2.getContentPane().add(Lb3);
f2.getContentPane().add(Lb4);
f2.getContentPane().add(Lb5);
f2.getContentPane().add(Lb6);
f2.getContentPane().add(Lb7);
f2.getContentPane().add(Lb8);
f2.getContentPane().add(Lb9);
f2.getContentPane().add(Lb10);
f2.getContentPane().add(Lb11);
f2.getContentPane().add(Lb12);
f2.getContentPane().add(Lb13);
f2.getContentPane().add(jcompute);
f2.getContentPane().add(jclear);
f2.getContentPane().add(jexit);
Lb2.setBounds(30,120,90,70);
Lb3.setBounds(30,170,110,70);
Lb4.setBounds(30,220,90,70);
Lb5.setBounds(30,270,120,70);
Lb6.setBounds(30,320,100,70);
Lb7.setBounds(30,370,110,70);
Lb8.setBounds(280,20,230,20);
Lb9.setBounds(30,420,80,70);
Lb10.setBounds(190,60,100,100);
Lb11.setBounds(315,60,100,100);
Lb12.setBounds(440,60,100,100);
Lb13.setBounds(570,60,100,100);
jcompute.setBounds(660,160,120, 60);
jclear.setBounds(660,260,120, 60);
jexit.setBounds(660,360,120,60);
//PRELIM
f2.getContentPane().add(cb1);
f2.getContentPane().add(cb2);
f2.getContentPane().add(cb3);
f2.getContentPane().add(cb4);
f2.getContentPane().add(cb5);
f2.getContentPane().add(cb6);
f2.getContentPane().add(Btn3);
f2.getContentPane().add(txt2);
txt2.setEditable(false);
cb1.setBounds(190,140,50,30);
cb2.setBounds(190,190,50,30);
cb3.setBounds(190,240,50,30);
cb4.setBounds(190,290,50,30);
cb5.setBounds(190,340,50,30);
cb6.setBounds(190,390,50,30);
Btn3.setBounds(170,490,95,40);
txt2.setBounds(190,440,55,35);
int numbers_to_add_max = 10;
for (int i = 0; i <= numbers_to_add_max; i++) {
cb2.addItem(new Integer(i));
cb4.addItem(new Integer(i));
cb5.addItem(new Integer(i));
}
int numbers = 20;
for (int i = 0; i <= numbers; i++) {
cb1.addItem(new Integer(i));
cb3.addItem(new Integer(i));
}
int numbers_to_add = 30;
for (int i = 0; i <= numbers_to_add; i++) {
cb6.addItem(new Integer(i));
}
//MIDTERM
f2.getContentPane().add(cb7);
f2.getContentPane().add(cb8);
f2.getContentPane().add(cb9);
f2.getContentPane().add(cb10);
f2.getContentPane().add(cb11);
f2.getContentPane().add(cb12);
f2.getContentPane().add(Btn4);
f2.getContentPane().add(txt3);
txt3.setEditable(false);
cb7.setBounds(315,140,50,30);
cb8.setBounds(315,190,50,30);
cb9.setBounds(315,240,50,30);
cb10.setBounds(315,290,50,30);
cb11.setBounds(315,340,50,30);
cb12.setBounds(315,390,50,30);
Btn4.setBounds(295,490,95,40);
txt3.setBounds(315,440,55,35);
int nu = 10;
for (int i = 0; i <= nu; i++) {
cb8.addItem(new Integer(i));
cb10.addItem(new Integer(i));
cb11.addItem(new Integer(i));
}
int num = 20;
for (int i = 0; i <= num; i++) {
cb7.addItem(new Integer(i));
cb9.addItem(new Integer(i));
}
int numb = 30;
for (int i = 0; i <= numb; i++) {
cb12.addItem(new Integer(i));
}
//PREFINAL
f2.getContentPane().add(cb13);
f2.getContentPane().add(cb14);
f2.getContentPane().add(cb15);
f2.getContentPane().add(cb16);
f2.getContentPane().add(cb17);
f2.getContentPane().add(cb18);
f2.getContentPane().add(Btn5);
f2.getContentPane().add(txt4);
txt4.setEditable(false);
cb13.setBounds(440,140,50,30);
cb14.setBounds(440,190,50,30);
cb15.setBounds(440,240,50,30);
cb16.setBounds(440,290,50,30);
cb17.setBounds(440,340,50,30);
cb18.setBounds(440,390,50,30);
Btn5.setBounds(420,490,95,40);
txt4.setBounds(440,440,55,35);
int toaddmax = 10;
for (int i = 0; i <= toaddmax; i++) {
cb14.addItem(new Integer(i));
cb16.addItem(new Integer(i));
cb17.addItem(new Integer(i));
}
int numbersadd = 20;
for (int i = 0; i <= numbersadd; i++) {
cb13.addItem(new Integer(i));
cb15.addItem(new Integer(i));
}
int numbers_toadd = 30;
for (int i = 0; i <= numbers_toadd; i++) {
cb18.addItem(new Integer(i));
}
//FINALS
f2.getContentPane().add(cb19);
f2.getContentPane().add(cb20);
f2.getContentPane().add(cb21);
f2.getContentPane().add(cb22);
f2.getContentPane().add(cb23);
f2.getContentPane().add(cb24);
f2.getContentPane().add(Btn6);
f2.getContentPane().add(txt5);
txt5.setEditable(false);
cb19.setBounds(565,140,50,30);
cb20.setBounds(565,190,50,30);
cb21.setBounds(565,240,50,30);
cb22.setBounds(565,290,50,30);
cb23.setBounds(565,340,50,30);
cb24.setBounds(565,390,50,30);
Btn6.setBounds(545,490,95,40);
txt5.setBounds(565,440,55,35);
int add_max = 10;
for (int i = 0; i <= add_max; i++) {
cb20.addItem(new Integer(i));
cb22.addItem(new Integer(i));
cb23.addItem(new Integer(i));
}
int number = 20;
for (int i = 0; i <= number; i++) {
cb19.addItem(new Integer(i));
cb21.addItem(new Integer(i));
}
int to_add = 30;
for (int i = 0; i <= to_add; i++) {
cb24.addItem(new Integer(i));
}
}
});
Btn2.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
Btn3.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb1Int = Integer.parseInt(cb1.getSelectedItem().toString());
int cb2Int = Integer.parseInt(cb2.getSelectedItem().toString());
int cb3Int = Integer.parseInt(cb3.getSelectedItem().toString());
int cb4Int = Integer.parseInt(cb4.getSelectedItem().toString());
int cb5Int = Integer.parseInt(cb5.getSelectedItem().toString());
int cb6Int = Integer.parseInt(cb6.getSelectedItem().toString());
txt2.setText(String.valueOf(cb1Int + cb2Int + cb3Int + cb4Int + cb5Int + cb6Int));
}
});
Btn4.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb7Int = Integer.parseInt(cb7.getSelectedItem().toString());
int cb8Int = Integer.parseInt(cb8.getSelectedItem().toString());
int cb9Int = Integer.parseInt(cb9.getSelectedItem().toString());
int cb10Int = Integer.parseInt(cb10.getSelectedItem().toString());
int cb11Int = Integer.parseInt(cb11.getSelectedItem().toString());
int cb12Int = Integer.parseInt(cb12.getSelectedItem().toString());
txt3.setText(String.valueOf(cb7Int + cb8Int + cb9Int + cb10Int + cb11Int + cb12Int));
}
});
Btn5.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb13Int = Integer.parseInt(cb13.getSelectedItem().toString());
int cb14Int = Integer.parseInt(cb14.getSelectedItem().toString());
int cb15Int = Integer.parseInt(cb15.getSelectedItem().toString());
int cb16Int = Integer.parseInt(cb16.getSelectedItem().toString());
int cb17Int = Integer.parseInt(cb17.getSelectedItem().toString());
int cb18Int = Integer.parseInt(cb18.getSelectedItem().toString());
txt4.setText(String.valueOf(cb13Int + cb14Int + cb15Int + cb16Int + cb17Int + cb18Int));
}
});
Btn6.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb19Int = Integer.parseInt(cb19.getSelectedItem().toString());
int cb20Int = Integer.parseInt(cb20.getSelectedItem().toString());
int cb21Int = Integer.parseInt(cb21.getSelectedItem().toString());
int cb22Int = Integer.parseInt(cb22.getSelectedItem().toString());
int cb23Int = Integer.parseInt(cb23.getSelectedItem().toString());
int cb24Int = Integer.parseInt(cb24.getSelectedItem().toString());
txt5.setText(String.valueOf(cb19Int + cb20Int + cb21Int + cb22Int + cb23Int + cb24Int));
}
});
jcompute.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String prelim, midterm, prefinal, finals, total;
double a, b, c, d, tg;
prelim = txt2.getText();
midterm = txt3.getText();
prefinal = txt4.getText();
finals = txt5.getText();
a = Double.parseDouble(prelim);
b = Double.parseDouble(midterm);
c = Double.parseDouble(prefinal);
d = Double.parseDouble(finals);
tg = (a + b + c + d)/4;
total = Double.toString(tg);
finalg.setText(total);
JFrame f3 = new JFrame ("STUDENT FINAL RATING");
f3.getContentPane().setLayout(null);
f3.setSize (350,300);
//the frame2 (f2); the two frames are still visible
f2.hide();
f3.show();
f3.getContentPane().add(label1);
f3.getContentPane().add(label2);
f3.getContentPane().add(finalg);
finalg.setEditable(false);
f3.getContentPane().add(finalr);
finalr.setEditable(false);
f3.getContentPane().add(finalb);
label1.setBounds(70,20,150,70);
label2.setBounds(90,70,200,70);
finalg.setBounds(205,40,50,30);
finalr.setBounds(140,140,50,40);
finalb.setBounds(130,200,70,30);
//EQUIVALENT
double grade, equiv;
grade = Double.parseDouble(finalg.getText());
if(grade>=99.50 && grade<101)
equiv = 1.00;
else if(grade<99.50 && grade>=98.50)
equiv = 1.10;
else if(grade<98.50 && grade>=97.50)
equiv = 1.20;
else if(grade<97.50 && grade>=96.50)
equiv = 1.30;
else if(grade<96.50 && grade>=95.50)
equiv = 1.40;
else if(grade<95.50 && grade>=94.50)
equiv = 1.50;
else if(grade<94.50 && grade>=93.50)
equiv = 1.60;
else if(grade<93.50 && grade>=92.50)
equiv = 1.70;
else if(grade<92.50 && grade>=91.50)
equiv = 1.80;
else if(grade<91.50 && grade>=90.50)
equiv = 1.90;
else if(grade<90.50 && grade>=89.50)
equiv = 2.00;
else if(grade<89.50 && grade>=88.50)
equiv = 2.10;
else if(grade<88.50 && grade>=87.50)
equiv = 2.20;
else if(grade<87.50 && grade>=86.50)
equiv = 2.30;
else if(grade<86.50 && grade>=85.50)
equiv = 2.40;
else if(grade<85.50 && grade>=84.50)
equiv = 2.50;
else if(grade<84.50 && grade>=83.50)
equiv = 2.60;
else if(grade<83.50 && grade>=82.50)
equiv = 2.70;
else if(grade<82.50 && grade>=81.50)
equiv = 2.80;
else if(grade<81.50 && grade>=80.50)
equiv = 2.90;
else if(grade<80.50 && grade>=79.50)
equiv = 3.00;
else if(grade<79.50 && grade>=78.50)
equiv = 3.10;
else if(grade<78.50 && grade>=77.50)
equiv = 3.20;
else if(grade<77.50 && grade>=76.50)
equiv = 3.30;
else if(grade<76.50 && grade>=75.50)
equiv = 3.40;
else if(grade<75.50 && grade>=74.50)
equiv = 3.50;
else
equiv = 5.0;
finalr.setText("" + equiv);
finalr.setEditable(false);
}
});
finalb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "" + equiv);
if (equi >=1.00 && equiv <=3.0) JOptionPane.showMessageDialog(null, " YOU PASSED!");
' else if (equiv >=3.10 && equiv <=75) JOptionPane.showMessageDialog(null, " YOU FAILED!");
}
});
jclear.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
//this part doesn't work. the JComboBox still contains the initialized number even though the "CLEAR ALL" button is clicked
cb1.setSelectedItem("0");
cb2.setSelectedItem("0");
cb3.setSelectedItem("0");
cb4.setSelectedItem("0");
cb5.setSelectedItem("0");
cb6.setSelectedItem("0");
cb7.setSelectedItem("0");
cb8.setSelectedItem("0");
cb9.setSelectedItem("0");
cb10.setSelectedItem("0");
cb11.setSelectedItem("0");
cb12.setSelectedItem("0");
cb13.setSelectedItem("0");
cb14.setSelectedItem("0");
cb15.setSelectedItem("0");
cb16.setSelectedItem("0");
cb17.setSelectedItem("0");
cb18.setSelectedItem("0");
cb19.setSelectedItem("0");
cb20.setSelectedItem("0");
cb21.setSelectedItem("0");
cb22.setSelectedItem("0");
cb23.setSelectedItem("0");
cb24.setSelectedItem("0");
txt2.setText(" ");
txt3.setText(" ");
txt4.setText(" ");
txt5.setText(" ");
}
});
jexit.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
f1.show();
}
public static void main (String args []){
forfinals xx = new forfinals();
}
}
Best Answer
Here is the complete corrected code,
These were the problems,
1.When you give zero in quotes like "0" its taken as string, whereas
Integer
is what required.2.The jFrame f2 was not accessible because it was defined inside another action event.
3.The same was the problem with the variable
equiv
, thats why it was not accessible in the event of jOptionpane.