所以,我有一个涵盖很多内容的任务。我需要:
- 有一个底部有两个按钮的 GUI。两个按钮都从我的临时文件夹中获取一个单独的文件。
- 我必须使用我们过去使用的方法(collectGrades 和averageGrade),并在单击第二个按钮时将该数据输出到中间的文本字段框
我需要从studentscores.txt(这是一个int字段和一个double字段)中获取数据并将它们放入StringBuilder中,然后将stringBuilder输出到jTextframe上。
private void jbtReadFileActionPerformed2(ActionEvent evt) {
    try {
        File inFile = new File("c:/temp/studentscores.txt");
        Scanner input = new Scanner(inFile);
        String fileContents = "";
        while(input.hasNext()) {
        students.add(new Student(input.nextInt(),input.nextLine()));
    } // end while
    for(int o = 0;o < students.size();o++) {
    students.get(o).setGrades(collectGrades(students.get(o).getStuId()));   
    jtxtAfileContents.setText("%-10d %-20s %.2f\n", students.get(o).getStuId(),     students.get(o).getStuName(), averageGrade(students.get(o).getGrades())););
我怎样才能让它工作?
import java.awt.*        
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;     
import javax.swing.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
    public class P_Supplemental_11 extends JFrame {
    JPanel jpnl1 = new JPanel();
    JPanel southPanel = new JPanel();
    JButton jbtReadFile1 = new JButton("Get Student Names");
    JButton jbtReadFile2 = new JButton("Get Student Grades");
    JTextField jtxtFilePath = new JTextField();
    JLabel jlblDesc = new JLabel("Click a button to open each file!");
    JTextArea jtxtAfileContents = new JTextArea();
    P_Supplemental_11() {
    this.setLayout(new BorderLayout(5, 10));
    jpnl1.setLayout(new GridLayout(2, 2));
    jpnl1.add(jlblDesc);
    //jpnl1.add(jtxtFilePath);
    southPanel.add(jbtReadFile1);
    southPanel.add(jbtReadFile2);
    add(southPanel, BorderLayout.SOUTH);
    add(jpnl1, BorderLayout.NORTH);
    add(jtxtAfileContents, BorderLayout.CENTER);
    jbtReadFile1.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
         jbtReadFileActionPerformed(evt);
     }
    });
    jbtReadFile2.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
         jbtReadFileActionPerformed2(evt);
     }
    });
    } // end constructor
    class Student {
    private int stuId;
    private String stuName;
    private ArrayList<Double> grades;
    Student(int idIn, String nameIn) {
        this.stuId = idIn;
        this.stuName = nameIn;
       } // end student class
        public int getStuId() {
            return stuId;
        }
        public void setStuId(int stuId) {
            this.stuId = stuId;
        }
         String getStuName() {
            return stuName;
        }
        public void setStuName(String stuName) {
            this.stuName = stuName;
        }
        public ArrayList<Double> getGrades() {
            return grades;
        }
        public void setGrades(ArrayList grades) {
            this.grades = grades;
        }
   } // end class Student
    ArrayList<Student> students = new ArrayList();
    private void jbtReadFileActionPerformed(ActionEvent evt) {
    try {
        File inFile = new File("c:/temp/studentnames.txt");
        Scanner input = new Scanner(inFile);
        String fileContents = "";
        while(input.hasNext()) {
            fileContents+= input.nextLine() + "\n";
        } // end while
        jtxtAfileContents.setText(fileContents);
        input.close();
    } // end action method for jbtReadFile button
    catch (FileNotFoundException ex) {
        Logger.getLogger(P_Supplemental_11.class.getName()).log(Level.SEVERE, null, ex);
    }
   }
   private void jbtReadFileActionPerformed2(ActionEvent evt) {
    try {
        File inFile = new File("c:/temp/studentscores.txt");
        Scanner input = new Scanner(inFile);
        String fileContents = "";
        while(input.hasNext()) {
        students.add(new Student(input.nextInt(),input.nextLine()));
    } // end while
    for(int o = 0;o < students.size();o++) {
    students.get(o).setGrades(collectGrades(students.get(o).getStuId()));   
    jtxtAfileContents.setText("%-10d %-20s %.2f\n", students.get(o).getStuId(),     students.get(o).getStuName(), averageGrade(students.get(o).getGrades())););
        /*String fileContents = "";
        while(input.hasNext()) {
            fileContents+= input.nextLine() + "\n";
        } // end while
        input.close();
        jtxtAfileContents.setText(collectGrades.getStuName);
        */
    }
    } // end action method for jbtReadFile button
    catch (FileNotFoundException ex) {
        Logger.getLogger(P_Supplemental_11.class.getName()).log(Level.SEVERE, null, ex);
    }
}
public static ArrayList <Double> collectGrades(int inId) {
    ArrayList <Double> outGrade = new ArrayList();
    try {
        File inFile = new File("c:/temp/studentScores.txt");
           Scanner input = new Scanner(inFile);
           while(input.hasNext()) {
           int tmpInt = input.nextInt();
           double tmpDbl = input.nextDouble();
           if(tmpInt == inId) {
               outGrade.add(tmpDbl);
           } // end if
           } // end while
    } catch (FileNotFoundException ex) {
        Logger.getLogger(P_Supplemental_11.class.getName()).log(Level.SEVERE, null, ex);
    } // end catch
    return outGrade;
} // end method
public static double averageGrade(ArrayList <Double> gradeIn) {
    double outAvg = 0.0;
    for(int y = 0; y < gradeIn.size();y++) {
    outAvg+= gradeIn.get(y);
} // end for
return outAvg/gradeIn.size();
}
public static void main(String[] args) {
    P_Supplemental_11 frame = new P_Supplemental_11();
    frame.setTitle("P_Supplemenetal_10");
    frame.setSize(410, 520);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
} // end main
} // end public class P_Supp_11
