mmcnitt
09-25-2009, 03:33 AM
Ok i'm really new at this so i apologize beforehand if i ask a stupid question or anything.
I'm working on a project that displays a GUI asking for a name as well as grades.
when you enter them all it runs the grades through a formula and opens another frame with the results. I don't need any help building the GUI what i need help on is I cannot for the life of me find out how to access the variable from another frame. Everything i've seen online tells me to set the variable outside the class but i need to run the formula inside because the grades come from the user.
Sorry if my code is messy, like i said i'm very new at this
// Loads all neccesary plugins
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SInfo extends JPanel
{
// Setting all text fields, labels and button
public JButton submitInfo;
public JLabel SNamel, Asign, Asignl, Asign1l, Asign2l, Asign3l, Asign4l, Asign5l, Asign6l,
Asign7l, Asign8l, Asign9l, Asign10l, Folderl, Projectl, Project1l, Project2l, Project3l,
Project4l, GProjectl, blank, blank1, Sname, AsgnAvg, ProjectAvg, TotalAvg;
public JTextField SName, Asign1, Asign2, Asign3, Asign4, Asign5, Asign6,
Asign7, Asign8, Asign9, Asign10, Folder, Project1, Project2, Project3,
Project4, GProject;
double a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
p1, p2, p3, p4, gp, fldr, asgnAvg, projectAvg, totalAvg;
public SInfo()
{
setLayout (new GridLayout (20,2));
// Sets the text for the labels
{
blank = new JLabel ("");
blank1 = new JLabel ("");
SNamel = new JLabel ("Student Name: ");
Asign = new JLabel ("");
Asignl = new JLabel ("--------Assignment Grades--------");
Asign1l = new JLabel ("Assignment 1: ");
Asign2l = new JLabel ("Assignment 2: ");
Asign3l = new JLabel ("Assignment 3: ");
Asign4l = new JLabel ("Assignment 4: ");
Asign5l = new JLabel ("Assignment 5: ");
Asign6l = new JLabel ("Assignment 6: ");
Asign7l = new JLabel ("Assignment 7: ");
Asign8l = new JLabel ("Assignment 8: ");
Asign9l = new JLabel ("Assignment 9: ");
Asign10l = new JLabel ("Assignment 10: ");
Folderl = new JLabel ("Class Folder Grade: ");
Projectl = new JLabel ("--------Project Grades--------");
Project1l = new JLabel ("Project 1: ");
Project2l = new JLabel ("Project 2: ");
Project3l = new JLabel ("Project 3: ");
Project4l = new JLabel ("Project 4: ");
GProjectl = new JLabel ("Group Project Grade: ");
}
// Button text and sets listener for it
{
submitInfo = new JButton ("Submit");
submitInfo.addActionListener (new SubmitListener());
}
// Tells the program how long to make the textfield
{
SName = new JTextField (20);
Asign1 = new JTextField (5);
Asign2 = new JTextField (5);
Asign3 = new JTextField (5);
Asign4 = new JTextField (5);
Asign5 = new JTextField (5);
Asign6 = new JTextField (5);
Asign7 = new JTextField (5);
Asign8 = new JTextField (5);
Asign9 = new JTextField (5);
Asign10 = new JTextField (5);
Folder = new JTextField (5);
Project1 = new JTextField (5);
Project2 = new JTextField (5);
Project3 = new JTextField (5);
Project4 = new JTextField (5);
GProject = new JTextField (5);
}
// Adds the objects to the Window
{
add (SNamel);
add (SName);
add (Asign);
add (Asignl);
add (Asign1l);
add (Asign1);
add (Asign2l);
add (Asign2);
add (Asign3l);
add (Asign3);
add (Asign4l);
add (Asign4);
add (Asign5l);
add (Asign5);
add (Asign6l);
add (Asign6);
add (Asign7l);
add (Asign7);
add (Asign8l);
add (Asign8);
add (Asign9l);
add (Asign9);
add (Asign10l);
add (Asign10);
add (Folderl);
add (Folder);
add (blank1);
add (Projectl);
add (Project1l);
add (Project1);
add (Project2l);
add (Project2);
add (Project3l);
add (Project3);
add (Project4l);
add (Project4);
add (GProjectl);
add (GProject);
add (blank);
add (submitInfo);
}
// sets size and color of the window
{
setPreferredSize (new Dimension(400,450));
setBackground (Color.gray);
}
}
public class SubmitListener implements ActionListener
{
// Performs calculation and saves when button is clicked
public void actionPerformed (ActionEvent event)
{
// Sets variables for the text fields
Sname = new JLabel ("Student Name: " + SName);
AsgnAvg = new JLabel ("Average of Assignments: " + asgnAvg);
ProjectAvg = new JLabel ("Average of Individual Projects: " + projectAvg);
TotalAvg = new JLabel ("Total Average for the year: " + totalAvg);
// Assignments
{
String asgn1 = Asign1.getText();
a1 = Double.valueOf(asgn1);
String asgn2 = Asign2.getText();
a2 = Double.valueOf(asgn2);
String asgn3 = Asign3.getText();
a3 = Double.valueOf(asgn3);
String asgn4 = Asign4.getText();
a4 = Double.valueOf(asgn4);
String asgn5 = Asign5.getText();
a5 = Double.valueOf(asgn5);
String asgn6 = Asign6.getText();
a6 = Double.valueOf(asgn6);
String asgn7 = Asign7.getText();
a7 = Double.valueOf(asgn7);
String asgn8 = Asign8.getText();
a8 = Double.valueOf(asgn8);
String asgn9 = Asign9.getText();
a9 = Double.valueOf(asgn9);
String asgn10 = Asign10.getText();
a10 = Double.valueOf(asgn10);
}
// Projects
{
String proj1 = Project1.getText();
p1 = Double.valueOf(proj1);
String proj2 = Project2.getText();
p2 = Double.valueOf(proj2);
String proj3 = Project3.getText();
p3 = Double.valueOf(proj3);
String proj4 = Project4.getText();
p4 = Double.valueOf(proj4);
String gproject = GProject.getText();
gp = Double.valueOf(gproject);
String Sname = SName.getText();
// Folder
String folder = Folder.getText();
fldr = Double.valueOf(folder);
}
// Math Formula
{
// asgnAvg = 20%
// projectAvg = 45%
// fldr = 10%
// gp = 25%
asgnAvg = (a1 + a2 + a3 + a3 + a5 + a6 + a7 + a8 + a9 + a10) / 10;
projectAvg = (p1 + p2 + p3 + p4) / 4;
totalAvg = (asgnAvg * .20) + (projectAvg * .45) + (fldr * .10) + (gp * .25);
}
{
JFrame frame = new JFrame ("Student Average");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SInfo2());
frame.pack();
frame.setVisible(true);
// Show the frame
frame.setSize(300, 300);
setBackground (Color.gray);
frame.setVisible(true);
}
}
}
}
And this is the second frame i am trying to open i haven't really done anything for the GUI i'm just working on it reading the variables now
// Loads all neccesary plugins
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SInfo2 extends SInfo
{
// Setting all text fields, labels and button
public JLabel Sname, AsgnAvg, ProjectAvg, TotalAvg;
public SInfo2()
{
setLayout (new GridLayout (4,2));
{
Sname = new JLabel (Sname); // These are the 4 I cannot retrieve
AsgnAvg = new JLabel (asgnAvg); // These are the 4 I cannot retrieve
ProjectAvg = new JLabel (projectAvg); // These are the 4 I cannot retrieve
TotalAvg = new JLabel (totalAvg); // These are the 4 I cannot retrieve
add (Sname);
add (AsgnAvg);
add (ProjectAvg);
add (TotalAvg);
}
}
}
Thank you in advance for anyone who even looks at it
I'm working on a project that displays a GUI asking for a name as well as grades.
when you enter them all it runs the grades through a formula and opens another frame with the results. I don't need any help building the GUI what i need help on is I cannot for the life of me find out how to access the variable from another frame. Everything i've seen online tells me to set the variable outside the class but i need to run the formula inside because the grades come from the user.
Sorry if my code is messy, like i said i'm very new at this
// Loads all neccesary plugins
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SInfo extends JPanel
{
// Setting all text fields, labels and button
public JButton submitInfo;
public JLabel SNamel, Asign, Asignl, Asign1l, Asign2l, Asign3l, Asign4l, Asign5l, Asign6l,
Asign7l, Asign8l, Asign9l, Asign10l, Folderl, Projectl, Project1l, Project2l, Project3l,
Project4l, GProjectl, blank, blank1, Sname, AsgnAvg, ProjectAvg, TotalAvg;
public JTextField SName, Asign1, Asign2, Asign3, Asign4, Asign5, Asign6,
Asign7, Asign8, Asign9, Asign10, Folder, Project1, Project2, Project3,
Project4, GProject;
double a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
p1, p2, p3, p4, gp, fldr, asgnAvg, projectAvg, totalAvg;
public SInfo()
{
setLayout (new GridLayout (20,2));
// Sets the text for the labels
{
blank = new JLabel ("");
blank1 = new JLabel ("");
SNamel = new JLabel ("Student Name: ");
Asign = new JLabel ("");
Asignl = new JLabel ("--------Assignment Grades--------");
Asign1l = new JLabel ("Assignment 1: ");
Asign2l = new JLabel ("Assignment 2: ");
Asign3l = new JLabel ("Assignment 3: ");
Asign4l = new JLabel ("Assignment 4: ");
Asign5l = new JLabel ("Assignment 5: ");
Asign6l = new JLabel ("Assignment 6: ");
Asign7l = new JLabel ("Assignment 7: ");
Asign8l = new JLabel ("Assignment 8: ");
Asign9l = new JLabel ("Assignment 9: ");
Asign10l = new JLabel ("Assignment 10: ");
Folderl = new JLabel ("Class Folder Grade: ");
Projectl = new JLabel ("--------Project Grades--------");
Project1l = new JLabel ("Project 1: ");
Project2l = new JLabel ("Project 2: ");
Project3l = new JLabel ("Project 3: ");
Project4l = new JLabel ("Project 4: ");
GProjectl = new JLabel ("Group Project Grade: ");
}
// Button text and sets listener for it
{
submitInfo = new JButton ("Submit");
submitInfo.addActionListener (new SubmitListener());
}
// Tells the program how long to make the textfield
{
SName = new JTextField (20);
Asign1 = new JTextField (5);
Asign2 = new JTextField (5);
Asign3 = new JTextField (5);
Asign4 = new JTextField (5);
Asign5 = new JTextField (5);
Asign6 = new JTextField (5);
Asign7 = new JTextField (5);
Asign8 = new JTextField (5);
Asign9 = new JTextField (5);
Asign10 = new JTextField (5);
Folder = new JTextField (5);
Project1 = new JTextField (5);
Project2 = new JTextField (5);
Project3 = new JTextField (5);
Project4 = new JTextField (5);
GProject = new JTextField (5);
}
// Adds the objects to the Window
{
add (SNamel);
add (SName);
add (Asign);
add (Asignl);
add (Asign1l);
add (Asign1);
add (Asign2l);
add (Asign2);
add (Asign3l);
add (Asign3);
add (Asign4l);
add (Asign4);
add (Asign5l);
add (Asign5);
add (Asign6l);
add (Asign6);
add (Asign7l);
add (Asign7);
add (Asign8l);
add (Asign8);
add (Asign9l);
add (Asign9);
add (Asign10l);
add (Asign10);
add (Folderl);
add (Folder);
add (blank1);
add (Projectl);
add (Project1l);
add (Project1);
add (Project2l);
add (Project2);
add (Project3l);
add (Project3);
add (Project4l);
add (Project4);
add (GProjectl);
add (GProject);
add (blank);
add (submitInfo);
}
// sets size and color of the window
{
setPreferredSize (new Dimension(400,450));
setBackground (Color.gray);
}
}
public class SubmitListener implements ActionListener
{
// Performs calculation and saves when button is clicked
public void actionPerformed (ActionEvent event)
{
// Sets variables for the text fields
Sname = new JLabel ("Student Name: " + SName);
AsgnAvg = new JLabel ("Average of Assignments: " + asgnAvg);
ProjectAvg = new JLabel ("Average of Individual Projects: " + projectAvg);
TotalAvg = new JLabel ("Total Average for the year: " + totalAvg);
// Assignments
{
String asgn1 = Asign1.getText();
a1 = Double.valueOf(asgn1);
String asgn2 = Asign2.getText();
a2 = Double.valueOf(asgn2);
String asgn3 = Asign3.getText();
a3 = Double.valueOf(asgn3);
String asgn4 = Asign4.getText();
a4 = Double.valueOf(asgn4);
String asgn5 = Asign5.getText();
a5 = Double.valueOf(asgn5);
String asgn6 = Asign6.getText();
a6 = Double.valueOf(asgn6);
String asgn7 = Asign7.getText();
a7 = Double.valueOf(asgn7);
String asgn8 = Asign8.getText();
a8 = Double.valueOf(asgn8);
String asgn9 = Asign9.getText();
a9 = Double.valueOf(asgn9);
String asgn10 = Asign10.getText();
a10 = Double.valueOf(asgn10);
}
// Projects
{
String proj1 = Project1.getText();
p1 = Double.valueOf(proj1);
String proj2 = Project2.getText();
p2 = Double.valueOf(proj2);
String proj3 = Project3.getText();
p3 = Double.valueOf(proj3);
String proj4 = Project4.getText();
p4 = Double.valueOf(proj4);
String gproject = GProject.getText();
gp = Double.valueOf(gproject);
String Sname = SName.getText();
// Folder
String folder = Folder.getText();
fldr = Double.valueOf(folder);
}
// Math Formula
{
// asgnAvg = 20%
// projectAvg = 45%
// fldr = 10%
// gp = 25%
asgnAvg = (a1 + a2 + a3 + a3 + a5 + a6 + a7 + a8 + a9 + a10) / 10;
projectAvg = (p1 + p2 + p3 + p4) / 4;
totalAvg = (asgnAvg * .20) + (projectAvg * .45) + (fldr * .10) + (gp * .25);
}
{
JFrame frame = new JFrame ("Student Average");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new SInfo2());
frame.pack();
frame.setVisible(true);
// Show the frame
frame.setSize(300, 300);
setBackground (Color.gray);
frame.setVisible(true);
}
}
}
}
And this is the second frame i am trying to open i haven't really done anything for the GUI i'm just working on it reading the variables now
// Loads all neccesary plugins
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SInfo2 extends SInfo
{
// Setting all text fields, labels and button
public JLabel Sname, AsgnAvg, ProjectAvg, TotalAvg;
public SInfo2()
{
setLayout (new GridLayout (4,2));
{
Sname = new JLabel (Sname); // These are the 4 I cannot retrieve
AsgnAvg = new JLabel (asgnAvg); // These are the 4 I cannot retrieve
ProjectAvg = new JLabel (projectAvg); // These are the 4 I cannot retrieve
TotalAvg = new JLabel (totalAvg); // These are the 4 I cannot retrieve
add (Sname);
add (AsgnAvg);
add (ProjectAvg);
add (TotalAvg);
}
}
}
Thank you in advance for anyone who even looks at it