我正在尝试向TextArea输出我文件中读取的内容。我能够打开JFileChooser,但是,我的程序无法完成将数组中的输出写入TextArea。提前谢谢!
public class GUI extends JFrame implements ActionListener{
ArrayList<Student> students = new ArrayList<Student>();
private JButton openFile;
private TextArea display;
public GUI(){
super("Student Directory");
setLayout(new FlowLayout());
openFile = new JButton("Open File");
openFile.addActionListener(this);
add(openFile);
display = new TextArea();
add(display);
}
public void displayStudents(String[] output){
display.setEditable(false);
for(int i = 0; i<output.length; i++){
display.append(output[i]+"\n");
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == openFile){
final JFileChooser jFileChooser = new JFileChooser();
int returnVal = jFileChooser.showOpenDialog(this);
if(returnVal == jFileChooser.APPROVE_OPTION){
File file = jFileChooser.getSelectedFile();
readFile(file);
}
}
}
private void readFile(File file) {
int currentStudent = 0;
Scanner input;
try {
input = new Scanner(new FileReader(file));
while (input.hasNextLine()) {
String line = input.nextLine();
StringTokenizer st = new StringTokenizer(line, ",");
while (st.hasMoreTokens()) {
String first, last, idNo;
last = st.nextToken();
first = st.nextToken();
idNo = st.nextToken();
Student s = new Student(last, first, idNo);
students.add(s);
line = input.nextLine();
st = new StringTokenizer(line, ",");
while (st.hasMoreTokens()) {
String x = st.nextToken();
if (x.equals("-999")) {
line = input.nextLine();
st = new StringTokenizer(line, ",");
Float totalCredits = Float.parseFloat(st.nextToken());
Float gpa = Float.parseFloat(st.nextToken());
students.get(currentStudent).storeGPA(totalCredits, gpa);
currentStudent++;
} else {
String courseID = x;
float credits = Float.parseFloat(st.nextToken());
String grade = st.nextToken();
CourseList cl = new CourseList(courseID, grade, credits);
s.setCourses(cl);
line = input.nextLine();
st = new StringTokenizer(line, ",");
}
}
}
}
input.close();
currentStudent = 0;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class StudentDir {
static ArrayList<Student> students = new ArrayList<Student>();
static GUI newGUI;
public static void main(String args[]) throws IOException {
GUI newGUI = new GUI();
newGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newGUI.setSize(275,180);
newGUI.setVisible(true);
String[] unsorted = sortedArray.makeArray(students);
String[] sorted = sortedArray.insertionSort(unsorted);
newGUI.displayStudents(unsorted);
newGUI.displayStudents(sorted);
for(int i = 0; i < unsorted.length;i++){
System.out.println(unsorted[i]+" | "+sorted[i]);
}
}
}