在我添加前两个if循环后,应用程序开始崩溃。应用程序应该与顾问一起查看顾问并注册新的顾问...因为我将学习程序作为JRadioButtons连接到JComboBox,其中包括根据选择的专业。在点击注册新学生按钮后,应用程序崩溃,我无法弄清楚问题是什么。程序运行正常,但在注册新学生按钮后崩溃。这是主要的课程。之后还有4个课程。据我所知,知道问题出在主要班级。
AguDbaseApp.java;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;
public class AguDbaseApp extends JFrame {
private JLabel advisorLabel, adviseeLabel, profTextAreaLabel,
studentTextAreaLabel, createStudentLabel, studentFullNameLabel, studentIDLabel, collegeLabel,
programTextFieldLabel,gpaLabel;
private JComboBox advisors, advisees, college, program;
private JRadioButton cecButton, cobButton, cob_Button, cec_Button, allAdviseesButton, cgpaLessThan2Button, abovespecificCGPAbutton;
private JTextField cgpaTextField, studentFullNameTextField, studentID_TextField, gpa_TextField;
private ButtonGroup cgpaGrp, profGroup, majorGroup;
private JTextArea profDetails, studentDetails;
private JButton registerNewStudentButton;
//Create an array of type AguProfessor and set it to hold up to 50 objects
private AguProfessor aguAdvisors[] = new AguProfessor[50];
private AguStudent aguStudents[] = new AguStudent[100];
int size;
private String cec[]= {"CIS", "CSE"};
private String cob[]= {"BBA", "MBA"};
public AguDbaseApp (){
super( "AGU Advisor List" );
setLayout( new GridBagLayout() );
aguStudents[0] = new AguStudent("Hammad Beloushi", "20142247", "USA", 25, "CEC", "CIS", 3.45);
aguStudents[1] = new AguStudent("James Holding", "20142248", "British", 29, "CEC", "CSE", 3.87);
aguStudents[2] = new AguStudent("Fin Aaz", "20142249", "Indonesia", 25, "CEC", "CCS", 2.56);
aguStudents[3] = new AguStudent("Josan Cruz", "20142250", "India", 25, "CEC", "CSE", 1.79);
aguStudents[4] = new AguStudent("Smith Jobs", "20142251", "UK", 25, "CEC", "CIS", 1.95);
aguStudents[5] = new AguStudent("Steve Solls", "20142252", "Canada", 25, "CoB", "CCS", 3.95);
aguStudents[6] = new AguStudent("Ali Minhas", "20142253", "Pakistan", 25, "CoB", "CSE", 3.78);
aguStudents[7] = new AguStudent("Mehdi Hassan", "20142254", "Spain", 25, "CoB", "CIS", 2.56);
aguStudents[8] = new AguStudent("Greme Smith", "20142255", "Germany", 25, "CoB", "CCS", 1.56);
aguStudents[9] = new AguStudent("Andrew Fin", "20142256", "Russia", 25, "CoB", "CSE", 1.85);
size=10;
//Declare and initialize an object of the GridBagConstraints class
GridBagConstraints gBC = new GridBagConstraints();
gBC.fill = GridBagConstraints.HORIZONTAL;
gBC.ipadx = 10;
gBC.ipady = 10;
gBC.insets = new Insets(0, 0, 0, 10);
aguAdvisors[0] = new AguProfessor("James Carmichael", "E500", "Barbados", "CEC", 45, "associate", 18000);
aguAdvisors[1] = new AguProfessor("Syed Ahmed", "E501", "India", "CEC", 40, "assistant", 15000);
aguAdvisors[2] = new AguProfessor("Mohammed Ramzy", "E502", "Egypt", "CoB", 38, "assistant", 15000);
aguAdvisors[3] = new AguProfessor("Ibrahim Siddiq", "E503", "Sudan", "CoB", 38, "associate", 18000);
aguStudents[0].setAdvisor(aguAdvisors[0]);
aguStudents[1].setAdvisor(aguAdvisors[1]);
aguStudents[2].setAdvisor(aguAdvisors[0]);
aguStudents[3].setAdvisor(aguAdvisors[1]);
aguStudents[4].setAdvisor(aguAdvisors[0]);
aguStudents[5].setAdvisor(aguAdvisors[2]);
aguStudents[6].setAdvisor(aguAdvisors[3]);
aguStudents[7].setAdvisor(aguAdvisors[2]);
aguStudents[8].setAdvisor(aguAdvisors[3]);
aguStudents[9].setAdvisor(aguAdvisors[2]);
gBC.gridx = 0;
gBC.gridy = 0;
advisorLabel= new JLabel("Select Advising Professor:");
add(advisorLabel, gBC);
gBC.gridx = 0;
gBC.gridy = 1;
advisors = new JComboBox();
advisors.setMaximumRowCount(3);
add(advisors, gBC);
advisors.setEditable(false);
gBC.gridx = 1;
gBC.gridy = 1;
advisees = new JComboBox();
advisees.setMaximumRowCount(3);
add(advisees, gBC);
advisees.setEditable(false);
gBC.gridx = 1;
gBC.gridy = 0;
adviseeLabel = new JLabel("Advisee List for selected advisor:");
add(adviseeLabel, gBC);
gBC.ipady = 1;
gBC.gridx = 2;
gBC.gridy = 1;
allAdviseesButton = new JRadioButton( "Show All Advisees", false );
add(allAdviseesButton, gBC);
gBC.gridx = 2;
gBC.gridy = 2;
cgpaLessThan2Button = new JRadioButton( "Show Advisees on probation", false );
add(cgpaLessThan2Button, gBC);
gBC.gridx = 2;
gBC.gridy = 3;
abovespecificCGPAbutton = new JRadioButton( "Show Advisees with CGPA above:", false );
add(abovespecificCGPAbutton, gBC);
gBC.gridx = 3;
gBC.gridy = 3;
cgpaTextField = new JTextField(5);
add(cgpaTextField, gBC);
cgpaGrp = new ButtonGroup();
cgpaGrp.add( allAdviseesButton);
cgpaGrp.add( cgpaLessThan2Button );
cgpaGrp.add( abovespecificCGPAbutton );
gBC.gridx = 0;
gBC.gridy = 2;
cecButton = new JRadioButton( "CEC Profs", false );
add(cecButton, gBC);
gBC.gridx = 0;
gBC.gridy = 3;
cobButton = new JRadioButton( "CoB Profs", false );
add(cobButton, gBC);
//Implement event handling for all objects using the ItemListener
RadioButtonHandler radButtonHandler = new RadioButtonHandler();
cecButton.addItemListener(radButtonHandler);
cobButton.addItemListener(radButtonHandler);
advisors.addItemListener(radButtonHandler);
allAdviseesButton.addItemListener(radButtonHandler);
cgpaLessThan2Button.addItemListener(radButtonHandler);
abovespecificCGPAbutton.addItemListener(radButtonHandler);
advisees.addItemListener(radButtonHandler);
profGroup = new ButtonGroup();
profGroup.add( cecButton);
profGroup.add( cobButton );
gBC.gridx = 0;
gBC.gridy = 4;
profTextAreaLabel = new JLabel("Advisor Info");
add(profTextAreaLabel, gBC);
gBC.gridx = 1;
gBC.gridy = 4;
studentTextAreaLabel = new JLabel("Student Record(s)");
add(studentTextAreaLabel, gBC);
gBC.gridx = 0;
gBC.gridy = 5;
//We want the JTextArea object to span across 3 grid columns
//so we set the 'gBC.gridwidth' variable to a value of '3'
gBC.gridheight = 4;
gBC.gridwidth = 1;
//initialize the studentDetails JTextArea object,
//specifying the number of rows and columns
profDetails = new JTextArea(5, 35);
profDetails.setWrapStyleWord(true);
profDetails.setEditable(false);
profDetails.setToolTipText("Selected professor's details will appear in this text box");
JScrollPane scrollingArea = new JScrollPane(profDetails);
add(scrollingArea, gBC);
gBC.gridx = 1;
gBC.gridy = 5;
gBC.gridwidth = 3;
studentDetails = new JTextArea(5, 35);
studentDetails.setWrapStyleWord(true);
studentDetails.setEditable(false);
studentDetails.setToolTipText("Selected student's transcript will appear in this text box");
JScrollPane scrollingArea2 = new JScrollPane(studentDetails);
add(scrollingArea2, gBC);
gBC.gridheight = 1;
gBC.gridwidth = 1;
gBC.gridx = 0;
gBC.gridy = 11;
createStudentLabel = new JLabel("REGISTER A NEW STUDENT AND ASSIGN TO AN ADVISOR:");
add(createStudentLabel, gBC);
gBC.gridx = 0;
gBC.gridy = 12;
studentFullNameLabel = new JLabel("Enter Student's name");
add(studentFullNameLabel, gBC);
gBC.gridx = 0;
gBC.gridy = 13;
studentFullNameTextField = new JTextField(20);
add(studentFullNameTextField, gBC);
gBC.gridx = 1;
gBC.gridy = 12;
studentIDLabel = new JLabel("Enter Student's ID");
add(studentIDLabel, gBC);
gBC.gridx = 1;
gBC.gridy = 13;
studentID_TextField = new JTextField(20);
add(studentID_TextField, gBC);
gBC.gridx = 2;
gBC.gridy = 13;
registerNewStudentButton = new JButton("Register New Student");
add(registerNewStudentButton, gBC);
registerNewStudentButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String name = studentFullNameTextField.getText();
String id = studentID_TextField.getText();
double gpa = -1;
try{
gpa = Double.parseDouble(gpa_TextField.getText());
}catch(Exception ex){
gpa = -1;
System.out.println("Double Parsing error!!!");
}
if(name.length()>3&&id.length()>0&&profGroup.getSelection()!=null)
{
String colg = college.getSelectedItem().toString();
String prog = program.getSelectedItem().toString();
aguStudents[size] = new AguStudent(name, id, "Null", 0, colg, prog, gpa);
for(int i=0; i<aguAdvisors.length; i++)
{
if(aguAdvisors[i]!=null&&aguAdvisors[i].getName().equals(advisors.getSelectedItem().toString()))
{
aguStudents[size].setAdvisor(aguAdvisors[i]);
}
}
studentFullNameTextField.setText("");
studentID_TextField.setText("");
gpa_TextField.setText("");
}
}
});
gBC.gridx = 0;
gBC.gridy = 14;
collegeLabel = new JLabel("College of affiliation:");
add(collegeLabel, gBC);
gBC.gridx = 0;
gBC.gridy = 15;
cec_Button = new JRadioButton( "CEC ", false );
add(cec_Button, gBC);
gBC.gridx = 0;
gBC.gridy = 16;
cob_Button = new JRadioButton( "COB ", false );
add(cob_Button, gBC);
majorGroup = new ButtonGroup();
majorGroup.add(cec_Button);
majorGroup.add(cob_Button);
cec_Button.addItemListener(radButtonHandler);
cob_Button.addItemListener(radButtonHandler);
gBC.gridx = 1;
gBC.gridy = 14;
programTextFieldLabel = new JLabel("Program for this student: ");
add(programTextFieldLabel, gBC);
gBC.gridx = 1;
gBC.gridy = 15;
program = new JComboBox();
program.setMaximumRowCount(3);
add(program, gBC);
program.setEditable(false);
// programTextField = new JTextField(10);
// add(programTextField, gBC);
gBC.gridx = 2;
gBC.gridy = 15;
gpaLabel = new JLabel(" GPA");
add(gpaLabel, gBC);
gBC.gridx = 3;
gBC.gridy = 15;
gpa_TextField = new JTextField(3);
add(gpa_TextField, gBC);
} // end of constructor
public void writeInfoToProfTextArea() {
profDetails.setText("");
for (int y=0; y < aguAdvisors.length; y++){
if(aguAdvisors[y]!=null && advisors.getItemCount()>0){
if (aguAdvisors[y].toString().trim().equals(advisors.getSelectedItem().toString().trim() )) {
profDetails.append("Professor's full name: " + aguAdvisors[y].getName());
profDetails.append("\nProfessor's e-mail:" + aguAdvisors[y].getEmail());
profDetails.append("\nProfessor's AGU College: " + aguAdvisors[y].getCollege());
profDetails.append("\nProfessor's AGU ID: " + aguAdvisors[y].getID());
}
}
}
}
public void writeInfoToStdTextArea(){
studentDetails.setText("");
for(int i=0; i<aguStudents.length; i++)
{
if(aguStudents[i]!=null&&advisees.getItemCount()>0)
{
if(aguStudents[i].getID().equals(advisees.getSelectedItem().toString()))
{
studentDetails.append("Name: "+aguStudents[i].getName());
studentDetails.append("\tID: "+ aguStudents[i].getID());
studentDetails.append("\nCollege: "+ aguStudents[i].getCollege());
studentDetails.append("\t\tProgram of Study: "+ aguStudents[i].getProgram());
studentDetails.append("\nAdvisor: "+ aguStudents[i].getAdvisor().getName());
studentDetails.append("\tCGPA: "+aguStudents[i].getCGPA());
}
}
}
}
private class RadioButtonHandler implements ItemListener {
public void itemStateChanged( ItemEvent radioButtonEvent ) {
if (radioButtonEvent.getSource()==cec_Button) {
//clear all items from the JComboBox object
program.removeAllItems();
int f;
for (f=0; f< cec.length; f++){
if (cec[f]!=null) {
if (cec[f].trim() != null) {
program.addItem(cec[f]);
}
}
}
}
else if (radioButtonEvent.getSource()==cob_Button) {
//clear all items from the JComboBox object
program.removeAllItems();
int f;
for (f=0; f< cob.length; f++){
if (cob[f]!=null) {
if (cob[f].trim() != null) {
program.addItem(cob[f]);
}
}
}
}
else if (radioButtonEvent.getSource()==cecButton) {
//clear all items from the JComboBox object
advisors.removeAllItems();
//clear all text from the JTextArea object
profDetails.setText("");
int i;
for (i=0; i< aguAdvisors.length; i++){
if (aguAdvisors[i]!=null) {
if (aguAdvisors[i].getCollege().trim().equalsIgnoreCase("CEC")) {
advisors.addItem(aguAdvisors[i]);
}
}
}
writeInfoToProfTextArea();
} else if (radioButtonEvent.getSource()==cobButton) {
//clear all items from the JComboBox object
advisors.removeAllItems();
//clear all text from the JTextArea object
profDetails.setText("");
for (int j=0; j< aguAdvisors.length; j++) {
if (aguAdvisors[j]!=null) {
if (aguAdvisors[j].getCollege().equalsIgnoreCase("CoB")) {
advisors.addItem(aguAdvisors[j]);
}
}
}
writeInfoToProfTextArea();
} else if (radioButtonEvent.getSource()==advisors){
writeInfoToProfTextArea();
}
if((radioButtonEvent.getSource() == allAdviseesButton)&& (profGroup.getSelection()!=null))
{
advisees.removeAllItems();
studentDetails.setText("");
for(int i=0; i<aguStudents.length; i++)
{
if(aguStudents[i]!=null)
{
if(aguStudents[i].getAdvisor().getName().equals(advisors.getSelectedItem().toString()))
{
advisees.addItem(aguStudents[i].getID());
}
}
}
writeInfoToStdTextArea();
}
else if((radioButtonEvent.getSource()== cgpaLessThan2Button)&&(profGroup.getSelection()!=null))
{
advisees.removeAllItems();
studentDetails.setText("");
for(int i=0; i<aguStudents.length; i++)
{
if(aguStudents[i]!=null)
{
if((aguStudents[i].getAdvisor().getName().equals(advisors.getSelectedItem().toString()))&&(aguStudents[i].getCGPA()<2.00))
{
advisees.addItem(aguStudents[i].getID());
}
}
}
writeInfoToStdTextArea();
}
else if((radioButtonEvent.getSource()== abovespecificCGPAbutton)&&(profGroup.getSelection()!=null)&&(cgpaTextField.getText().length()>0))
{
advisees.removeAllItems();
studentDetails.setText("");
double cgpa = Double.parseDouble(cgpaTextField.getText());
for(int i=0; i<aguStudents.length; i++)
{
if(aguStudents[i]!=null)
{
if((aguStudents[i].getAdvisor().getName().equals(advisors.getSelectedItem().toString()))&&(aguStudents[i].getCGPA()> cgpa))
{
advisees.addItem(aguStudents[i].getID());
}
}
}
writeInfoToStdTextArea();
}
else if(radioButtonEvent.getSource()==advisees)
{
writeInfoToStdTextArea();
}
}
}
public static void main(String[] args) {
AguDbaseApp aguDbase = new AguDbaseApp();
aguDbase.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
// set frame size by specifying x-coordinate and y-coordinate
aguDbase.setSize( 1000, 600 ); //the setSize() method takes the x coordinate, then y
aguDbase.setVisible( true ); // display frame
}
}
AguPerson.java;
public abstract class AguPerson {
protected String nationality;
protected String name;
protected String id;
protected String eMail;
protected String college;
protected int age;
public AguPerson(String ismay, String iD, String nat, String coll, int ag) {
name = ismay;
id = iD;
nationality=nat;
age = ag;
eMail = null;
college = coll;
}
public AguPerson(String ismay, String iD, String nat, String coll, String email, int ag) {
name = ismay;
id = iD;
nationality=nat;
age = ag;
eMail = email;
college = coll;
}
public void setCollege (String newCollege){
college = newCollege;
}
public String getCollege (){
return college;
}
public String getName() {
return name;
}
public void setName (String newName){
name =newName;
}
public String getID() {
return id;
}
public String getEmail() { return eMail; };
public abstract void setEmail();
}
AguProfessor.java,
public class AguProfessor extends AguPerson {
private String rank; // can be "assistant", "associate" or "full"
private double monthlySalary;
//Implement a class field of type AguProfessor (which will represent the student's advisor).
//and don't forget to implement the usual 'get' and 'set' methods
public AguProfessor (String fullName, String iD, String nat, String coll, int ag, String rnk, double sal) {
super(fullName, iD, nat, coll, ag);
rank = rnk; monthlySalary = sal;
setEmail();
}
@Override
public void setEmail() {
eMail = name.replaceAll(" ", "") + "@agu.ac.ae";
}
public void setSalary(){
if(rank.equalsIgnoreCase("assistant")){
monthlySalary=15000;
} else if (rank.equalsIgnoreCase("associate")){
monthlySalary=18000;
} else if (rank.equalsIgnoreCase("full")){
monthlySalary=21000;
}
}
public String toString(){
return name;
}
}
AguStudent.java;
public class AguStudent<string> extends AguPerson implements Comparable<AguStudent> {
private String program;
private Transcript transcr;
private AguProfessor advisor;
private double cgpa, gpa;
public AguStudent (String fullName, String iD, String nat, int ag, String coll, String prog, Double gpa) {
super(fullName, iD, nat, coll, ag);
program = prog; cgpa = gpa;
}
public void setEmail() {
eMail = id+"agu.ac.ae";
}
public void setProgram (String newProgram){
program = newProgram;
}
public String getProgram (){
return program;
}
public void setNationality (String newNationality){
nationality = newNationality;
}
public String getNationality (){
return nationality;
}
public void setgpa (Double newgpa){
gpa = newgpa;
}
public Double getgpa (){
return gpa;
}
public void setCGPA (double newCGPA){
cgpa = newCGPA;
}
public double getCGPA (){
return cgpa;
}
public int compareTo(AguStudent otherStudent) {
if(this.getCGPA() < otherStudent.getCGPA()) {
return -1;
} else if(this.getCGPA() == otherStudent.getCGPA()) {
return 0;
} else {
return 1;
}
}
public String toString(){
return name;
}
public void setAdvisor(AguProfessor advisor){
this.advisor = advisor;
}
public AguProfessor getAdvisor(){
return advisor;
}
}
Transcript.java;
public class Transcript {
private String[][] courseCodesAndGrades = new String[50][2];
private double[] creditHours = new double[50];
private String studentID, studentName;
private double cGPA=0;
public Transcript(String name, String id, String[][] codesAndGrades, double[] credits) {
studentName= name;
studentID = id;
addCourseCodesAndGrades(codesAndGrades);
addCredits(credits);
}
public Transcript(String name, String id) {
studentName= name;
studentID = id;
}
public String getStudentID(){ return studentID; }
public void setStudentID(String newID){ studentID = newID; }
public String getStudentName(){ return studentName; }
//TASK 1: Insert values for the "B+", "C+", "D+", "F" and "WF" grades. NOTE that
//a "WF" grade does not affect the GPA score calculations
//NOTE: This is a static or class method.
public static double convertGradeToPoints(String someGrade, double creditHours) {
double points = 0;
if (someGrade.equalsIgnoreCase("A")) {
points = 4;
} else if (someGrade.equals("B+")) {
points = 3.5;
} else if (someGrade.equals("B")) {
points = 3;
} else if (someGrade.equals("C+")) { //What is the difference between "equals()" and "equalsIgnoreCase()"?
points = 2.5;
}else if (someGrade.equals("C")) {
points = 2;
}else if (someGrade.equals("B+")) {
points = 1.5;
} else if (someGrade.equals("D")) {
points = 1;
}
return points*creditHours;
}
public static double convertGradesToGPA(String[] grades, double[] credHours) {
double totalGradePoints =0, totalCreditHours=0;
for (int i = 0; i < grades.length; i++){
if(grades[i]!=null){
totalGradePoints += convertGradeToPoints(grades[i], credHours[i]);
totalCreditHours += credHours[i];
}
}
return totalGradePoints/totalCreditHours;
}
public void addCourseCodesAndGrades(String[][] aFewCoursesAndGrades){
//TASK 2: Modify this implementation so that duplicate courses cannot
//be added, e.g. the course code "CSE 233" should only appear ONCE
int counter = 0;
while(courseCodesAndGrades[counter][0]!=null){
counter++;
}
for(int y = 0; y < aFewCoursesAndGrades.length; y++){
if(courseCodesAndGrades[counter][0]!=null) {
counter++;
} else {
courseCodesAndGrades[counter][0]= aFewCoursesAndGrades[y][0];
courseCodesAndGrades[counter][1]= aFewCoursesAndGrades[y][1];
counter++;
}
}
} // end of addCourseCodesAndGrades()method
public void addCredits(double[] aFewCredits){
int counter = 0;
while(creditHours[counter]!=0){
counter++;
}
for (int x = 0; x < aFewCredits.length; x++){
if(creditHours[counter]==0 ){
creditHours[counter] = aFewCredits[x];
counter++;
} else {
counter++;
}
}
}//end of addCredits method
public String getAllCoursesAndGrades() {
String allCoursesAndGrades = "";
for(int y = 0; y < courseCodesAndGrades.length; y++){
if(courseCodesAndGrades[y][0]!=null){
allCoursesAndGrades += courseCodesAndGrades[y][0] + ", " + courseCodesAndGrades[y][1] + "; ";
}
}
return allCoursesAndGrades;
} //end of getAllCoursesAndGrades() method
private void calculateGPA() {
String[] allGrades = new String[courseCodesAndGrades.length];
double[] credHrs = new double[creditHours.length];
for(int z = 0; z < courseCodesAndGrades.length; z++){
if(courseCodesAndGrades[z][1]!=null){
allGrades[z]= courseCodesAndGrades[z][1];
credHrs[z] =creditHours[z];
} // end of if statement
} // end of for loop
cGPA = convertGradesToGPA(allGrades, credHrs);
}
public double getCGPA(){
calculateGPA();
return cGPA;
} // of getCGPA() method
}// end of Transcript class