0

尝试运行我的程序。按添加主题(填写数据)。然后显示。然后添加另一个主题。然后显示。

在显示选项卡上,它显示 2 个主题和 1 个空主题。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;

@SuppressWarnings ({"unchecked" , "rawtypes"})

public class StudentGradeCalc extends JFrame implements ActionListener {

//String setName [];
int x = 0;
int count = 0;
int w = 0;
int [] addSize = {50,10,20,30,40,100};
private JPanel panelInfo = new JPanel(new GridLayout(7,0) );
private JLabel sy = new JLabel ("School Year : ");
private JLabel sem = new JLabel ("Sem/Term: ");
private JLabel Name = new JLabel ("Name : ");
private JLabel Course = new JLabel ("Course : ");
private JLabel Year = new JLabel ("Year : ");
private JLabel college = new JLabel ("College : ");
private JLabel id = new JLabel ("I.D Number : ");

private JTextField syear = new JTextField ("");
private JTextField semnum = new JTextField("");
private JTextField name = new JTextField ("");
private JTextField course = new JTextField ("");
private JTextField year = new JTextField ("");
private JTextField scs = new JTextField("");
private JTextField idnum = new JTextField("");





private JPanel fakebuttons = new JPanel (new GridLayout (4,0,10,10));


private JButton addSubject = new JButton("Add Subject..");
private JButton deleteSubject = new JButton("Delete Subject..");
private JButton editSubject = new JButton("Edit Subject..");

private JButton totalLoad = new JButton("Total Load : ");
private JButton GPA = new JButton ("GPA : ");

private JTextField units_textfield = new JTextField(3);
private JTextField gpa_textfield = new JTextField (7);

private JButton displayInfo = new JButton("Display Data");

private JPanel addPanel = new JPanel(new GridLayout(3,2,20,10));
        private JLabel subjectName = new JLabel("Subject: ");
        private JTextField subject [] ;

        private JLabel gradeLabel = new JLabel("Grade : ");
        private JLabel unitsLabel = new JLabel ("Units : ");

        private JButton addButton = new JButton ("Add");


        String grade [] = {"1.00","1.25","1.50","1.75","2.00","2.25","2.50","2.75","3.00","5.0", "DRP", "INC", "WDRW"};
        private JComboBox comboGrade [];

        String unit [] = {"1","2","3","4","5","6"};
        private JComboBox comboUnit[];

  private JButton done = new JButton ("Done"); 
  private JButton d_subj,d_unit,d_grade;

 // private JTextField displaySubject = new JTextField();
  // private JTextField displayUnit = new JTextField ();
  //private JTextField displayGrade = new JTextField();

  private JTextField displaySubject[];
  private JTextField displayUnit[];
 private JTextField displayGrade [];

 private JLabel t_units = new JLabel ("Total Load : ");
 private JTextField totalUnits = new JTextField ();
 private JLabel gpa = new JLabel ("GPA : ");
  private JTextField gpaTxt = new JTextField ();

  private JLabel err = new JLabel ("No Entry Found");

private JLabel display1 = new JLabel ();
private JLabel display2 = new JLabel ();
private JLabel displayID = new JLabel ();

final String getName [] = new String [10];
final int getUnit [] = new int [10];
 final String getGrade [] = new String [10];

public StudentGradeCalc()
{
    Border border = BorderFactory.createLineBorder(null); 
    setLayout(null);

    panelInfo.setBounds(5,5,280,150);
    //panelInfo.setBorder(border);
    panelInfo.add(Name);
    panelInfo.add(name);
    panelInfo.add(Course);
    panelInfo.add(course);
    panelInfo.add(Year);
    panelInfo.add(year);
    panelInfo.add(sy);
    panelInfo.add(syear);
    panelInfo.add(sem);
    panelInfo.add(semnum);
    panelInfo.add(college);
    panelInfo.add(scs);
    panelInfo.add(id);
    panelInfo.add(idnum);



    add(panelInfo);

    fakebuttons.setBounds(5,160,280,160);

    fakebuttons.add(addSubject);
    fakebuttons.add(deleteSubject);
    fakebuttons.add(editSubject);
    fakebuttons.add(displayInfo);
    add(fakebuttons);

    //ADD SUBJECT BUTTON
    addSubject.addActionListener(new ActionListener (){
      public void actionPerformed (ActionEvent cags){
        final JFrame addFrame = new JFrame ();


        addFrame.setVisible(true);
        addFrame.setSize (300,190);
        addFrame.setResizable(false);
        addFrame.setLocationRelativeTo(null);
        addFrame.setTitle("Add Subject");

            //addFrame.setLayout(new GridLayout (3,2,20,10));
              addFrame.setLayout(null);

            subject = new JTextField [] {
                new JTextField (),
                new JTextField (),
                new JTextField (),
                new JTextField (),
                new JTextField (),
                new JTextField (),
                new JTextField (),
                new JTextField (),
                new JTextField (),


            };



            subjectName.setBounds (10,10,120,20);
            subject[x].setBounds (75,10,170,20);
            addFrame.add(subjectName);
            addFrame.add(subject[x]);


            gradeLabel.setBounds (10,40,100,20);
            addFrame.add(gradeLabel);
            comboGrade = new JComboBox [] {
                new JComboBox (grade),
                new JComboBox (grade),
                new JComboBox (grade),
                new JComboBox (grade),
                new JComboBox (grade),
                new JComboBox (grade),
                new JComboBox (grade),
                new JComboBox (grade),
                new JComboBox (grade),
                new JComboBox (grade),

            };

            comboGrade[x].setBounds(150,40,95,20);
            addFrame.add(comboGrade[x]);

           unitsLabel.setBounds (10, 70, 95, 20);
            addFrame.add(unitsLabel);
            comboUnit = new JComboBox [] {
                new JComboBox(unit),
                new JComboBox(unit),
                new JComboBox(unit),
                new JComboBox(unit),
                new JComboBox(unit),
                new JComboBox(unit),
                new JComboBox(unit),

            };
           comboUnit[x].setBounds(150,70,95,20);
            addFrame.add(comboUnit[x]);

            addButton.setBounds (120,120,60,30);
           addFrame.add(addButton);



       w++;



            addButton.addActionListener(new ActionListener () {
                public void actionPerformed(ActionEvent k){
                    //JOptionPane.showMessageDialog(null, "Subject "+subject[x].getText()+ " has been added", "Add Subject",    JOptionPane.INFORMATION_MESSAGE);


                    getName [x] = subject[x].getText();
                    getGrade[x] = String.valueOf((String)comboGrade[x].getSelectedItem());
                    getUnit [x] = Integer.valueOf((String)comboUnit[x].getSelectedItem());
          x++;  
                    addFrame.dispose();

                }


            });



      }


     });




    //DISPLAY DATA BUTTON 
     displayInfo.addActionListener(new ActionListener (){
         public void actionPerformed(ActionEvent m){


       final String name_course_year = "Name : "+name.getText()+ "          Course : " +course.getText()+"- "+year.getText()+ "    ID : "+idnum.getText() ;
       final String ubos = "College : "+scs.getText()+ "          SY : "+syear.getText()+ "     Term : "+semnum.getText() ;
       //String nametanga []  = {name.getText() , };

         if (name.getText().equals("")|| course.getText().equals("")||year.getText ().equals("") || idnum.getText().equals("")||syear.getText ().equals("") || scs.getText().equals("")|| semnum.getText().equals(""))
             {
             JOptionPane.showMessageDialog(null, "Complete the Information!", "ERROR", JOptionPane.WARNING_MESSAGE);
             }   

         else 
         {


        final JFrame display = new JFrame ();

         display.setVisible(true);
        //display.setSize (400,200);
        //display.setSize (400,down+130); 
        display.setResizable(false);
        display.setLocationRelativeTo(null);
        display.setTitle("Student Info");
        display.setLayout(null);


        display1.setBounds (5,5,390,20);
        display2.setBounds (5,25,390,20);
        display1.setText(name_course_year);
        display2.setText(ubos);
        display.add(display1);
        display.add(display2);





        d_subj = new JButton ("Subject");
        d_unit = new JButton ("Unit");
        d_grade = new JButton ("Grade");

        d_subj.setBounds(10,50,180,20);
        d_unit.setBounds (200,50,90,20);
        d_grade.setBounds(300,50,90,20);
        display.add(d_subj);
        display.add(d_unit);
        display.add(d_grade);

        displaySubject = new JTextField [] {
            new JTextField(),
            new JTextField(),
            new JTextField(),
            new JTextField(),
            new JTextField(),
            new JTextField(),
            new JTextField(),

        };
        displayUnit = new JTextField [] {
            new JTextField(),new JTextField(),new JTextField(),
            new JTextField(),new JTextField(),new JTextField(),
            new JTextField(),new JTextField(),

        };
        displayGrade = new JTextField [] {
            new JTextField(),new JTextField(),new JTextField(),
            new JTextField(),new JTextField(),new JTextField(),
            new JTextField(),new JTextField(),
        };

        int y = 30;
        int down = 80;
        int z = 0;

        for (int i = 0; i < x; i++)
        {

        displaySubject[i].setBounds(10,down,180,y);
        displayUnit[i].setBounds(200,down,90,y);
        displayGrade[i].setBounds(300,down,90,y);


        displaySubject[i].setText(getName[i]);
        displayUnit[i].setText(""+getUnit[i]);
        displayGrade[i].setText(getGrade[i]);


        display.add(displaySubject[i]);
        display.add(displayUnit[i]);
        display.add(displayGrade[i]);

        displaySubject[i].setEditable(false);
        displayUnit[i].setEditable(false);
        displayGrade[i].setEditable(false);


        down+=y+10;

        //i++;
       // z++;
        }
           display.setSize (400,down+130); 


        t_units.setBounds (100,down,70,25);
        totalUnits.setBounds(180,down,50,25);
        gpa.setBounds(260,down,50,25);
        gpaTxt.setBounds(310,down,80,25);

        display.add(t_units);
        display.add(totalUnits);
        display.add(gpa);
        display.add(gpaTxt); 



        gpaTxt.setEditable(false);
        totalUnits.setEditable(false);

        done.setBounds (170,down+50,70,30);
        display.add(done);

        done.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent done){
                display.dispose();
            }
        });

          }

         }

     });




    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize (300,355);
setResizable(false);
setLocationRelativeTo(null);
setTitle("Student Grade Calculator"); 

}


public  void actionPerformed (ActionEvent vahn)
{
   System.exit(1);
}



public static void main(String[] args) {
    new StudentGradeCalc();
}

}

我不知道在哪里可以找到我的程序出现此错误的原因。

4

1 回答 1

0

我发现你的问题在这里addButton.addActionListener()。您总是向按钮​​实例添加一个新的 ActionListener,因此,每次按下addSubject按钮时,您也会向addButton添加一个新的 Listener 。以后不要这样做,以组件构造的方法将您的侦听器添加到组件中。

于 2013-10-29T18:20:35.647 回答