-3

我需要别人的帮助。我收到此错误,即文件无法解析为类型。这是在底部,它说 File file = new File(filename); 不知道为什么。请有人帮忙。

谢谢

import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*; //step 1

    public class QuizPanel extends JPanel{
      private ArrayList<String>fileData = new ArrayList<String>();  
      private ArrayList<String>oneData = new ArrayList<String>(); 
      private ArrayList<String>twoData = new ArrayList<String>(); 

      private JButton submitButton;
      private JButton nextButton;
      private JButton levelOneButton;
      private JButton levelTwoButton;

      private JLabel question;
      private JLabel correctLabel = new JLabel("");
      private JLabel correctAnswer = new JLabel("");
      private JLabel ended = new JLabel("");
      private JLabel responce = new JLabel("");

     private JRadioButton [] options = { new JRadioButton (""), new JRadioButton (""), new JRadioButton (""), new JRadioButton (""),};
             ButtonGroup optionsGroup = new ButtonGroup ();

    private int questionNumber = 0;
    private int score = 0;

    //Border Layout Panels
      private JPanel northPanel = new JPanel();
      private JPanel eastPanel = new JPanel();
      private JPanel southPanel = new JPanel();
      private JPanel westPanel = new JPanel();
      private JPanel centerPanel = new JPanel();



      // constructor
      public QuizPanel(ArrayList<String>fileData){
        EventListener listener = new EventListener();//step 3
        this.fileData = fileData;

        question = new JLabel();

        submitButton = new JButton("Check Answer");
        submitButton.addActionListener(listener);//step 4

        nextButton = new JButton("Next Question");
        nextButton.addActionListener(listener);

        levelOneButton = new JButton("Level One");
        levelOneButton.addActionListener(listener);

        levelTwoButton = new JButton("Level Two");
        levelTwoButton.addActionListener(listener);

        for (JRadioButton each:options){
        optionsGroup.add(each);
        each.addActionListener(listener);
        centerPanel.add(each);
        }


        setLayout(new BorderLayout());

        //North
        northPanel.setPreferredSize(new Dimension(400,65));
        northPanel.setBackground(Color.black);
        northPanel.add(question);
        question.setVisible(false);
        question.setFont(new Font("Calibri",Font.BOLD,20));
        question.setForeground(Color.white);
        add(northPanel,BorderLayout.NORTH);

        //East
         eastPanel.setPreferredSize(new Dimension(100,200));
         eastPanel.setBackground(Color.green);
         eastPanel.add(submitButton);
         eastPanel.add(nextButton);
         nextButton.setVisible(false);
         add(eastPanel,BorderLayout.EAST);

        //South
         southPanel.setPreferredSize(new Dimension(400,65));
         southPanel.setBackground(Color.black);
         correctLabel.setFont(new Font("Calibri",Font.BOLD,20));
         correctLabel.setForeground(Color.white);
         southPanel.add(correctLabel);
         correctLabel.setVisible(false);

         ended.setFont(new Font("Calibri",Font.BOLD,20));
         ended.setForeground(Color.white);
         southPanel.add(ended);
         ended.setVisible(false);
         add(southPanel,BorderLayout.SOUTH);

        //West
        westPanel.setPreferredSize(new Dimension(100,200));
        westPanel.setBackground(Color.green);
        responce.setFont(new Font("Calibri",Font.BOLD,20));
        westPanel.add(responce);
        responce.setVisible(false);
        westPanel.add(levelOneButton);
        westPanel.add(levelTwoButton);
        add(westPanel,BorderLayout.WEST);

        //Center
         centerPanel.setBackground(Color.green);
         centerPanel.setVisible(false);
         add(centerPanel,BorderLayout.CENTER);



        //set up outer panel colour and size
        setPreferredSize(new Dimension(700,350));
        setBackground(Color.green);

        showQuestion(0);// show first question
      }

      //method for show Questions
      public void showQuestion(int questionNumber){
        String newQuestion = fileData.get(questionNumber);  
        System.out.println(newQuestion);

        String [] allQuestions = newQuestion.split("/");
        question.setText(allQuestions[0]);

        for(int i = 1; i < allQuestions.length - 1; i++){
          options[i-1].setText(allQuestions[i]);
        }    
         correctAnswer.setText(allQuestions[allQuestions.length-1]);
      } 


        // step 2 write an inner class
     private class EventListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
         String level = " ";

        if(e.getSource() == levelOneButton){
          String levelOne = "levelOne.txt";
          readFile(levelOne, oneData);
          fileData = oneData;
          showQuestion(0);
         } else if (e.getSource() == levelTwoButton){
           String levelTwo = "levelTwo.txt";
           readFile(levelTwo, twoData);
           fileData = twoData;
           showQuestion(0);
         }


         int clicked = 0;
         boolean answered = false;

         if(e.getSource() == submitButton){
           for(int i = 0; i < options.length; i++){
             if(options[i].isSelected()){
               clicked = i;
               answered = true;
           }
         }
           if(answered){
             if(correctAnswer.getText().equals(options[clicked].getText())){
               correctLabel.setText("Correct");
               nextButton.setVisible(true);
               correctLabel.setVisible(true);
               submitButton.setVisible(false);
               score++;
             }else{
               submitButton.setVisible(false);
               correctLabel.setVisible(true);
               nextButton.setVisible(true);
         String newQuestion = fileData.get(questionNumber); 
        String [] allQuestions = newQuestion.split("/");
        correctLabel.setText("Incorrect, Correct answer is " + allQuestions[5]);

         }  
        } 

         }else if (e.getSource() == nextButton){
           submitButton.setVisible(true);
           nextButton.setVisible(false);
           correctLabel.setVisible(false);

           if(questionNumber < fileData.size() - 1){ //next question Button
             questionNumber++;
             optionsGroup.clearSelection();
             correctAnswer.setText("");
             showQuestion(questionNumber); 

           }else{            

             submitButton.setVisible(false);
             nextButton.setVisible(false);
             question.setVisible(false);
             centerPanel.setVisible(false);
             ended.setVisible(true);
             ended.setText("Your score is " + score + "/3");
             responce.setVisible(true);
             responce.setText("Thank you for taking the quiz, Your score is below.");
         }
        }

       }

     }

      public static void readFile(String filename, ArrayList<String> fileData){
       File file = new File(filename);

       try{
          //instanitate a Scanner object to read from the object
          Scanner fileScan = new Scanner(file);
          //while there is another line yet to be read
          while(fileScan.hasNextLine()){
            fileData.add(fileScan.nextLine());
            //should manage exceptions - the String might not convert into a valid integer
          }
         }catch (FileNotFoundException e){
          System.out.println("File not found");
          System.exit(1); //exit from program if file not found
        }
      }
    }
4

2 回答 2

1

您缺少import顶部的声明。

import java.io.*;
于 2013-10-23T20:31:43.510 回答
1

你想试试这个:-

import java.io.*;

你错过了import声明。

于 2013-10-23T20:32:21.187 回答