2012-11-09 90 views
1

所以,我有一個任務涵蓋了很多。我需要:通過方法和GUI進行輸入

  • 有一個GUI,底部有兩個按鈕。兩個按鈕都從我的臨時文件夾中獲取單獨的文件。
  • 我必須使用我們過去使用的方法(collectGrades和averageGrade)和輸出單擊第二個按鈕

當數據在中間的文本字段框我需要從數據studentscores.txt(這是一個int字段和一個雙字段),並將它們放入一個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 
+0

這裏,而不是有太多的代碼,你認爲錯誤是足夠的特異性。儘量縮小一點。 – awolfe91

+0

什麼不適合你。 PLZ更具體:) –

+0

我編輯了具有特定問題和代碼的頂部的文件。 – Dhunt90

回答

2

我把你的代碼運行起來了,你的意思是?

Press the button

代碼:

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class Student extends JFrame { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame frame = new JFrame("Splashing"); 
       StudentView view = new StudentView(); 
       frame.getContentPane().add(view); 
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
       frame.setMinimumSize(new Dimension(800, 450)); 
       frame.setLocationRelativeTo(null); // Center 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    static class StudentView extends JPanel { 
     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(); 

     StudentView() { 
      this.setLayout(new BorderLayout(5, 10)); 
      jpnl1.setLayout(new GridLayout(2, 2)); 
      jpnl1.add(jlblDesc); 
      //jpnl1.add(jtxtFilePath); 
      jbtReadFile2.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        SwingUtilities.invokeLater(new Runnable() { 
         @Override 
         public void run() { 
          jtxtAfileContents.setText(collectGrades()); 
         } 
        }); 
       } 
      }); 
      southPanel.add(jbtReadFile1); 
      southPanel.add(jbtReadFile2); 
      add(southPanel, BorderLayout.SOUTH); 
      add(jpnl1, BorderLayout.NORTH); 
      add(jtxtAfileContents, BorderLayout.CENTER); 
     } 

     private String collectGrades() { 
      StringBuilder builder = new StringBuilder("Hello World"); 
      builder.append("More info"); 
      return builder.toString(); 
     } 
    } 
} 
+0

我已完成GUI - 我只需要能夠點擊「獲取學生成績」並通過我的collectGrades方法解析並將其傳回中心的輸出。 – Dhunt90

+0

所以你已經添加了一個actionListner到你的'jbtReadFile2'?這應該調用從文件讀取的方法。然後輸入你的'collectGrades',然後更新'jtxtAfileContents'。哪部分不起作用? – Skjalg

+0

好吧,我們更新了作業。這是另一項任務的延伸 - 我們可以通過任何方式與之聊天,這樣我就可以更深入地解釋了? – Dhunt90