2013-12-09 118 views
0

我想用此GUI進行的操作是讓用戶瀏覽一個txt文件,然後閱讀它。當用戶點擊「讀取文件」按鈕時,應該提示他們選擇一個文件,然後它將讀取文件並設置各種值。我遇到了一些問題。首先,我使用掃描儀嘗試設置值,但我不確定這是最佳方式。其次,我不確定文件是否被讀取。我可以打開瀏覽文件對話框,但之後它會凍結,我必須重新啓動eclipse。如何使用JFileChooser讀取文件並通過讀取設置值?使用JFileChooser打開文件,讀取文件並設置值

import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.util.Scanner; 

    public class Display extends JFrame implements ActionListener { 
     private static final int FRAME_WIDTH = 400; 
     private static final int FRAME_HEIGHT = 350; 

     private static final int FRAME_X_ORIGIN = 100; 
     private static final int FRAME_Y_ORIGIN = 75; 

     private JFrame mainFrame; 
     private JCheckBox avgHSCheckBox; 
     private JCheckBox avgTSCheckBox; 
     private JCheckBox homeworkSDCheckBox; 
     private JCheckBox testSDCheckBox; 
     private JRadioButton firstClass; 
     private JRadioButton secondClass; 
     private JRadioButton thirdClass; 
     private JRadioButton fourthClass; 
     private JButton readFileButton; 
     private JButton exitButton; 
     private JButton statsButton; 
     private JButton clearButton; 

     static int numberOfClasses = 3; 
     static int numberOfAssignments = 6; 
     static int numberOfStudents; 
     static int numberOfLabs = 15; 
     static int totalHomeworkScore = 0; 
     static int totalTestScore = 0; 
     static String classYear; 
     static String semester; 

     public static void main(String[] args) { 
      Display frame = new Display(); 
      frame.setVisible(true); 

     } 

     public Display() { 

      setSize(FRAME_WIDTH, FRAME_HEIGHT); 
      setResizable(false); 
      setTitle("CSCE155A Course Offering Viewer"); 
      setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); 
      setDefaultCloseOperation(EXIT_ON_CLOSE); 
      setLayout(new BorderLayout()); 

      // header 
      JPanel header = new JPanel(); 
      header.setLayout(new GridLayout(2, 1)); 
      header.setSize(350, 75); 
      header.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      header.add(new JLabel("CSCE155A Course Offering Viewer")); 
      header.add(new JLabel("First Last")); 
      add(header, BorderLayout.NORTH); 

      // select stats 
      JPanel statsSelect = new JPanel(); 
      statsSelect.setLayout(new GridLayout(5, 1)); 
      statsSelect.setSize(100, 100); 

      statsSelect.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      statsSelect.add(new JLabel("Please Select: ")); 
      avgHSCheckBox = new JCheckBox("View Average Homework Score"); 
      statsSelect.add(avgHSCheckBox); 
      avgTSCheckBox = new JCheckBox("View Average Test Score"); 
      statsSelect.add(avgTSCheckBox); 
      homeworkSDCheckBox = new JCheckBox("View SD for Homework Scores"); 
      statsSelect.add(homeworkSDCheckBox); 
      testSDCheckBox = new JCheckBox("View SD for Test Scores"); 
      statsSelect.add(testSDCheckBox); 
      add(statsSelect, BorderLayout.WEST); 

      // Course offerings 

      JPanel courseOfferings = new JPanel(); 
      courseOfferings.setLayout(new GridLayout(5, 1)); 
      courseOfferings.setSize(100, 100); 
      courseOfferings.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      courseOfferings.add(new JLabel("Please Select: ")); 
      firstClass = new JRadioButton(semester + classYear); 
      courseOfferings.add(firstClass); 
      secondClass = new JRadioButton(semester + classYear); 
      courseOfferings.add(secondClass); 
      thirdClass = new JRadioButton(semester + classYear); 
      courseOfferings.add(thirdClass); 
      fourthClass = new JRadioButton(semester + classYear); 
      courseOfferings.add(fourthClass); 
      add(courseOfferings, BorderLayout.EAST); 
      // statistics 

      JPanel statistics = new JPanel(); 
      statistics.setLayout(new GridLayout(5, 1)); 
      statistics.setSize(200, 150); 
      statistics.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      statistics.add(new JLabel("Statistics:")); 
      add(statistics, BorderLayout.CENTER); 

      // buttons 
      JPanel buttons = new JPanel(); 
      buttons.setLayout(new FlowLayout()); 
      buttons.setSize(200, 150); 
      buttons.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
      readFileButton = new JButton("Read File"); 
      buttons.add(readFileButton); 
      exitButton = new JButton("Exit"); 
      buttons.add(exitButton); 
      statsButton = new JButton("Stats"); 
      buttons.add(statsButton); 
      clearButton = new JButton("Clear"); 
      buttons.add(clearButton); 
      add(buttons, BorderLayout.SOUTH); 

      avgHSCheckBox.addActionListener(this); 
      avgTSCheckBox.addActionListener(this); 
      homeworkSDCheckBox.addActionListener(this); 
      testSDCheckBox.addActionListener(this); 
      firstClass.addActionListener(this); 
      secondClass.addActionListener(this); 
      thirdClass.addActionListener(this); 
      fourthClass.addActionListener(this); 
      readFileButton.addActionListener(this); 
      exitButton.addActionListener(this); 
      statsButton.addActionListener(this); 
      clearButton.addActionListener(this); 

      setDefaultCloseOperation(EXIT_ON_CLOSE); 

     } 

     public void actionPerformed(ActionEvent event) { 
      Scanner scanner = new Scanner(System.in); 
      CourseOffering myCourseOffering = new CourseOffering(); 
      ComputeStatistics myComputeStatistics = new ComputeStatistics(); 
      JButton clickedButton = (JButton) event.getSource(); 
      String buttonText = clickedButton.getText(); 
      if (buttonText.equals("Read File")) { 
       // Create a file chooser 
       String filename = File.separator; 
       JFileChooser fc = new JFileChooser(new File(filename)); 
       fc.showOpenDialog(this); 
       File selFile = fc.getSelectedFile(); 

       numberOfClasses = scanner.nextInt(); 
       for (int i = 0; i < numberOfClasses; i++) { 

        myCourseOffering.setDescription(scanner.next()); // CSCE 
        myCourseOffering.setDescription(scanner.next()); // 155A 
        myCourseOffering.setDescription(scanner.next()); // - 
        myCourseOffering.setDescription(scanner.next()); // Semester 
        semester = myCourseOffering.getDescription(); 
        myCourseOffering.setDescription(scanner.next()); // Year 
        classYear = myCourseOffering.getDescription(); 

        numberOfStudents = scanner.nextInt(); // Number Of Students 
        myCourseOffering.students = new Student[numberOfStudents]; 

        for (int j = 0; j < numberOfStudents; j++) { 
         myCourseOffering.students[j] = new Student(); 
         totalTestScore = 0; 
         totalHomeworkScore = 0; 
         // Sets first and last name 
         myCourseOffering.students[j].setFirstName(scanner.next()); 
         myCourseOffering.students[j].setLastName(scanner.next()); 

         // call assignscores array 
         int[] assignScores = new int[numberOfAssignments]; 

         for (int k = 0; k < numberOfAssignments; k++) { 
          assignScores[k] = scanner.nextInt(); 
          totalHomeworkScore += assignScores[k]; 
         } 
         myCourseOffering.students[j] 
           .setAssignmentScores(assignScores); 

         int[] labScores = new int[numberOfLabs]; 
         for (int l = 0; l < numberOfLabs; l++) { 
          labScores[l] = scanner.nextInt(); 
          totalHomeworkScore += labScores[l]; 
         } 
         myCourseOffering.students[j].setLabScores(labScores); 

         myCourseOffering.students[j].setMidTerm1(scanner.nextInt()); 
         totalTestScore += myCourseOffering.students[j] 
           .getMidterm1(); 

         myCourseOffering.students[j].setMidterm2(scanner.nextInt()); 
         totalTestScore += myCourseOffering.students[j] 
           .getMidterm2(); 

         myCourseOffering.students[j] 
           .setFinalExam(scanner.nextInt()); 
         totalTestScore += myCourseOffering.students[j] 
           .getFinalExam(); 

         myCourseOffering.students[j] 
           .setQuizScore(scanner.nextInt()); 
         totalTestScore += myCourseOffering.students[j] 
           .getQuizScore(); 

         myCourseOffering.students[j].setAttendanceScore(scanner 
           .nextInt()); 
         totalHomeworkScore += myCourseOffering.students[j] 
           .getAttendanceScore(); 

         myCourseOffering.students[j].setPatScore(scanner.nextInt()); 
         totalTestScore += myCourseOffering.students[j] 
           .getPatScore(); 

         myCourseOffering.students[j].setZyanteScore(scanner 
           .nextInt()); 
         totalTestScore += myCourseOffering.students[j] 
           .getZyanteScore(); 

         myCourseOffering.students[j] 
           .setTotalHomeworkScore(totalHomeworkScore); 
         myCourseOffering.students[j] 
           .setTotalTestScore(totalTestScore); 

        } 
       } 

      } 
     } 
    } 

回答

1

您創建的掃描儀使用System.in。您需要創建一個可以在文件上運行的掃描程序:Scanner scanner = new Scanner(selFile);

此外,只是供參考,這是一個殘酷的方式來做ActionListeners。我知道大量的例子都是這樣做的,包括來自Sun/Oracle的例子,但它太糟糕了。爲每個按鈕添加單獨的監聽器,以便您不會創建一個負責任何事情的巨大監聽器。

+0

你是什麼意思單獨的聽衆?你的意思是用什麼而不是「這個」? – user2120893

+0

是的。每個ActionListener應該只響應一件事。如果動作相對簡短,可以添加一個匿名ActionListener:'okButton.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.out.println(「OK button」);}}) ;'。如果涉及的更多,則應該創建一個單獨的類,您可以將其放入正在使用的類中:'private class EditButtonAction implements ActionListener {...}',然後'editButton.addActionListener(new EditButtonAction() );'。 – MattPutnam

相關問題