Results 1 to 17 of 17
  1. #1
    CSE114 is offline Member
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Question Please help me with GUI

    Hi, everyone
    I am writing a program which reads test questions from a file, and then displays the questions with GUI, and the program also has to listen to the test taker's answer and then score the exam.

    The program has five classes

    Java Code:
    public class Question
    {
      this class defines constructors, and score exams, display questions, display test, etc
    }
    Java Code:
    public class MultipleChoiceQuestion extends Question
    {
      }
    public class TrueFalseQuestion extends Question
    {
    }
    Java Code:
    public class Test
    {
      in this class, I create an ArrayList of type Question to hold the questions read from file,
    the ArrayList class is private
    }
    Java Code:
    public class QuizDriver
    {
      This is the main class
    }
    I tried the following two ways to display the questions
    1. I tried to get the questions from the ArrayList of the Test class, but as it's of type private, I can not get access to it, can you tell me how to get the questions from the ArrayList, please? I don't want to read the questions directly from the file again(I did it before in the Test class), and if I did that, I would not know what is the total number of questions the test is going to display, and I cannot choose to display questions randomly either.

    2. I tried to create the GUI in the Test class, and it turns out that there is no syntax error, but there is nothing being diplayed when I run the program

    The GUI I have written in the Test class is as follow, I want to know, is it mandatory that one has to create GUI in the main class? Thank you very much!

    Java Code:
    public void sampleTest ()
            {
                JFrame mainWindow = new JFrame("This is a test");
                mainWindow.setSize(800, 600);
            
                // Centering the window
                Dimension frameSize = mainWindow.getSize();
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            
                mainWindow.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
            
                // Add a test name
                Container contentPane = mainWindow.getContentPane();
                JLabel testName = new JLabel("Sample Test", JLabel.CENTER);
                contentPane.add(testName, BorderLayout.NORTH);
                
                // Read questions from the Question arrayList
                for (int i = 0; i < questions.size(); i++)
                {
                    Scanner sc = new Scanner (questions.get(i).getQuestion());
                    JPanel centerPanel = new JPanel();
                    centerPanel.setLayout(new GridLayout(questions.size(), 1));
    
    if (questions.get(i).getQuestionType().equals("TF"))
                    {
                        // Create a new Panel to hold a true false question, and then add this panel to the centerPanel later
                        JPanel trueFalsePanel = new JPanel();
                        trueFalsePanel.setLayout(new GridLayout(3, 1));
                        
                        String questionText = sc.nextLine();
                        // Add content to the text fields
                        JTextField field1 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + ")");
                        field1.setEditable(false);
                        field1.setEnabled(true);
                        trueFalsePanel.add(field1);
    
    ........................... other JTextFields
    }
    if (questions.get(i).getQuestionType().equals("MC"))
                    {
                        // Create a new Panel to hold a multiple choice question, and then add this panel to the centerPanel later
                        JPanel multipleChoicePanel = new JPanel();
                        multipleChoicePanel.setLayout(new GridLayout(7,1));
                        
                        String questionText = sc.nextLine();
                        String answerChoiceA = sc.nextLine();
                        String answerChoiceB = sc.nextLine();
                        String answerChoiceC = sc.nextLine();
                        String answerChoiceD = sc.nextLine();
                        
                        JTextField field4 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + ")");
                        field4.setEditable(false);
                        field4.setEnabled(true);
                        multipleChoicePanel.add(field4);
                        ............. other TextFields
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I tried to get the questions from the ArrayList of the Test class, but as it's of type private, I can not get access to it, can you tell me how to get the questions from the ArrayList, please?
    The usual way is to use a get method to return the list values. Or, if security is a concern, you can make up a new array/list of Question, copy the values of the list into it and return the array/list. This is sometimes referred to as making a defensive copy.
    Java Code:
    class ReadsFileIntoList {
        private List<Question> questions = new ArrayList<Question>();
    
        public List<Question> getQuestions() {
            return questions;
        }
    }
    is it mandatory that one has to create GUI in the main class?
    No. You have creative license in class design.

  3. #3
    CSE114 is offline Member
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Question

    Thank you very much

    Now I have written the following code to display the test with GUI
    I supposed the program should have displayed all the questions it reads from the input file, but it turns out to be just printing the last question of the input file. Can you tell me where I have made the mistake, please? Thank you very much!

    Java Code:
    public void sampleTest ()
            {
                JFrame mainWindow = new JFrame("This is a test");
                mainWindow.setSize(800, 600);
            
                // Centering the window
                Dimension frameSize = mainWindow.getSize();
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            
                mainWindow.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
            
                // Add a test name
                Container contentPane = mainWindow.getContentPane();
                JLabel testName = new JLabel("Sample Test", JLabel.CENTER);
                contentPane.add(testName, BorderLayout.NORTH);
                
                // Read questions from the Question arrayList
                for (int i = 0; i < questions.size(); i++)
                {
                    Scanner sc = new Scanner (questions.get(i).getQuestion());
                    JPanel centerPanel = new JPanel();
                    centerPanel.setLayout(new GridLayout(questions.size(), 1));
                    
                    if (questions.get(i).getQuestionType().equals("TF"))
                    {
                        // Create a new Panel to hold a true false question, and then add this panel to the centerPanel later
                        JPanel trueFalsePanel = new JPanel();
                        trueFalsePanel.setLayout(new GridLayout(3, 1));
                        
                        String questionText = sc.nextLine();
                        // Add content to the text fields
                        JTextField field1 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
     
                        field1.setEditable(false);
                        field1.setEnabled(true);
                        trueFalsePanel.add(field1);
                        
                        JTextField field2 = new JTextField ("" + questionText);
                        field2.setEditable(false);
                        field2.setEnabled(true);
                        trueFalsePanel.add(field2);
                        
                        JTextField field3 = new JTextField ("Your answer");
                        field3.setEditable(true);
                        field3.setEnabled(true);
                        trueFalsePanel.add(field3);
                        
                        centerPanel.add(trueFalsePanel); // Add the trueFalsePanel to the centerpanel, and then later add the centerPanel to the center of the JFrame
                    }
                    
                    if (questions.get(i).getQuestionType().equals("MC"))
                    {
                        // Create a new Panel to hold a multiple choice question, and then add this panel to the centerPanel later
                        JPanel multipleChoicePanel = new JPanel();
                        multipleChoicePanel.setLayout(new GridLayout(7,1));
                        
                        String questionText = sc.nextLine();
                        String answerChoiceA = sc.nextLine();
                        String answerChoiceB = sc.nextLine();
                        String answerChoiceC = sc.nextLine();
                        String answerChoiceD = sc.nextLine();
                        
                        JTextField field4 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
                        field4.setEditable(false);
                        field4.setEnabled(true);
                        multipleChoicePanel.add(field4);
                        
                        JTextField field5 = new JTextField ("" + questionText);
                        field5.setEditable(false);
                        field5.setEnabled(true);
                        multipleChoicePanel.add(field5);
                        
                        JTextField field6 = new JTextField ("" + answerChoiceA);
                        field6.setEditable(false);
                        field6.setEnabled(true);
                        multipleChoicePanel.add(field6);
                        
                        JTextField field7 = new JTextField ("" + answerChoiceB);
                        field7.setEditable(false);
                        field7.setEnabled(true);
                        multipleChoicePanel.add(field7);
                        
                        JTextField field8 = new JTextField ("" + answerChoiceC);
                        field8.setEditable(false);
                        field8.setEnabled(true);
                        multipleChoicePanel.add(field8);
                        
                        JTextField field9 = new JTextField ("" + answerChoiceD);
                        field9.setEditable(false);
                        field9.setEnabled(true);
                        multipleChoicePanel.add(field9);
                        
                        JTextField field10 = new JTextField ("Your answer");
                        field10.setEditable(true);
                        field10.setEnabled(true);
                        multipleChoicePanel.add(field10);
                        
                        centerPanel.add(multipleChoicePanel);
                    }
                    contentPane.add(centerPanel, BorderLayout.CENTER);
                }
                mainWindow.setVisible(true);
            }

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    // Read questions from the Question arrayList
    for (int i = 0; i < questions.size(); i++)
    {
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(questions.size(), 1));
        ...
        if (questions.get(i).getQuestionType().equals("TF"))
            centerPanel.add(trueFalsePanel);
        if (questions.get(i).getQuestionType().equals("MC"))
            centerPanel.add(multipleChoicePanel);
        ...
        // When you add a component to a section of a BorderLayout which
        // is occupied, the component that previously occupied that section
        // is removed and the new component is added. So the last component
        // added will show up in the gui.
        contentPane.add(centerPanel, BorderLayout.CENTER);
    }
    Try moving the centerPanel and the contentPane.add call outside of the for loop:
    Java Code:
    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new GridLayout(questions.size(), 1));
    // Read questions from the Question arrayList
    for (int i = 0; i < questions.size(); i++)
    {
        if (questions.get(i).getQuestionType().equals("TF"))
            centerPanel.add(trueFalsePanel);
        if (questions.get(i).getQuestionType().equals("MC"))
            centerPanel.add(multipleChoicePanel);
    }
    contentPane.add(centerPanel, BorderLayout.CENTER);

  5. #5
    CSE114 is offline Member
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Question

    Thank you so much!

    With your help, now the program can display all the questions.

    But there is still a problem,
    For the mutiple choice problems, it seems that there is not enough space to display them, so that both the question texts and answer choices of mutiple choices problems are very small. Can I do something to add a scroll bar in the side of the window so that all the questions can be displayed in normal size?
    Thank you again for your very sincere help!

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    add a scroll bar ... so that all the questions can be displayed in normal size
    Java Code:
    contentPane.add(new JScrollPane(centerPanel), BorderLayout.CENTER);

  7. #7
    CSE114 is offline Member
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Question

    Thank you so much

    Now I try to keep track of the test taker's answers, so I need to use a listener, I do this as follow, but the compiler throws me back an error which says:
    "not a statement"
    for the following line of code

    Java Code:
    ActionListener 1 = new ActionListener()
    The modified method is as follow now

    Java Code:
    public void sampleTest ()
            {
                JFrame mainWindow = new JFrame("This is a test");
                mainWindow.setSize(800, 600);
            
                // Centering the window
                Dimension frameSize = mainWindow.getSize();
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            
                mainWindow.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
            
                // Add a test name
                Container contentPane = mainWindow.getContentPane();
                JLabel testName = new JLabel("Sample Test", JLabel.CENTER);
                contentPane.add(testName, BorderLayout.NORTH);
                
                JPanel centerPanel = new JPanel();
                centerPanel.setLayout(new GridLayout(questions.size(), 1));
                
                // Read questions from the Question arrayList
                for (int i = 0; i < questions.size(); i++)
                {
                    Scanner sc = new Scanner (questions.get(i).getQuestion());
                    
                    
                    JTextField field1; 
                    JTextField field2;
                    JTextField field3;
                    JTextField field4;
                    JTextField field5;
                    JTextField field6;
                    JTextField field7;
                    JTextField field8;
                    JTextField field9;
                    JTextField field10;
                    if (questions.get(i).getQuestionType().equals("TF"))
                    {
                        // Create a new Panel to hold a true false question, and then add this panel to the centerPanel later
                        JPanel trueFalsePanel = new JPanel();
                        trueFalsePanel.setLayout(new GridLayout(3, 1));
                        
                        String questionText = sc.nextLine();
                        // Add content to the text fields
                        field1 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
     
                        field1.setEditable(false);
                        field1.setEnabled(true);
                        trueFalsePanel.add(field1);
                        
                        field2 = new JTextField ("" + questionText);
                        field2.setEditable(false);
                        field2.setEnabled(true);
                        trueFalsePanel.add(field2);
                        
                        field3 = new JTextField ("Your answer");
                        field3.setEditable(true);
                        field3.setEnabled(true);
                        trueFalsePanel.add(field3);
                        
                        centerPanel.add(trueFalsePanel); // Add the trueFalsePanel to the centerpanel, and then later add the centerPanel to the center of the JFrame
                    }
                    
                    if (questions.get(i).getQuestionType().equals("MC"))
                    {
                        // Create a new Panel to hold a multiple choice question, and then add this panel to the centerPanel later
                        JPanel multipleChoicePanel = new JPanel();
                        multipleChoicePanel.setLayout(new GridLayout(7,1));
                        
                        String questionText = sc.nextLine();
                        String answerChoiceA = sc.nextLine();
                        String answerChoiceB = sc.nextLine();
                        String answerChoiceC = sc.nextLine();
                        String answerChoiceD = sc.nextLine();
                        
                        field4 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
                        field4.setEditable(false);
                        field4.setEnabled(true);
                        multipleChoicePanel.add(field4);
                        
                        field5 = new JTextField ("" + questionText);
                        field5.setEditable(false);
                        field5.setEnabled(true);
                        multipleChoicePanel.add(field5);
                        
                        field6 = new JTextField ("" + answerChoiceA);
                        field6.setEditable(false);
                        field6.setEnabled(true);
                        multipleChoicePanel.add(field6);
                        
                        field7 = new JTextField ("" + answerChoiceB);
                        field7.setEditable(false);
                        field7.setEnabled(true);
                        multipleChoicePanel.add(field7);
                        
                        field8 = new JTextField ("" + answerChoiceC);
                        field8.setEditable(false);
                        field8.setEnabled(true);
                        multipleChoicePanel.add(field8);
                        
                        field9 = new JTextField ("" + answerChoiceD);
                        field9.setEditable(false);
                        field9.setEnabled(true);
                        multipleChoicePanel.add(field9);
                        
                        field10 = new JTextField ("Your answer");
                        field10.setEditable(true);
                        field10.setEnabled(true);
                        multipleChoicePanel.add(field10);
                        
                        centerPanel.add(multipleChoicePanel);
                    }
                    
                    [B]ActionListener 1 = new ActionListener() // The compiler says this is not a statement[/B]
                    {
                        public void actionPerformed(ActionEvent e)
                        {
                            if (questions.get(i).getQuestionType().equals("TF"))
                            {
                                String userAnswer = field3.getText();
                                if (userAnswer.equals("")
                                {
                                    String userAnswer = null;
                                }
                                answer(i, userAnswer);
                            }
                            
                            if (questions.get(i).getQuestionType().equals("MC"))
                            {
                                String uswerAnswer = field10.getText();
                                if (userAnswer.equals(""))
                                {
                                    String userAnswer = null;
                                }
                                answer(i, userAnswer);
                            }
                        }
                    };
                    field3.addActionListener(1); // Can I use the same listener to listen to two different text fields? field3 and field10
                    field10.addActionListener(1);
                }
                contentPane.add(centerPanel, BorderLayout.CENTER);
                contentPane.add(new JScrollPane(centerPanel), BorderLayout.CENTER);
                mainWindow.setVisible(true);
            }
    Thank you very much

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    C:\jexp>javac traversepoints.java
    traversepoints.java:11: <identifier> expected
        ActionListener 1 = new ActionListener() {
                      ^
    traversepoints.java:11: invalid method declaration; return type required
        ActionListener 1 = new ActionListener() {
                               ^
    traversepoints.java:12: illegal start of expression
            public void actionPerformed(ActionEvent e) {
            ^
    3 errors
    Java Code:
    // change the identifier/variable name
    ActionListener 1 = new ActionListener()  // digit one
    // to this
    ActionListener l = new ActionListener()  // lowerCase L

  9. #9
    CSE114 is offline Member
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Question

    Hi, hardwired
    I don't understand why I have to use this, it's a specified word in java, can I use it as a variable name?

    And I also don't understand why I have to change the L of ActionListener to lowercase. In our book, it's uppercase.

    I did the modification according to your instruction, but the compiler gives me back the same error message:
    which reads the ActionListener this = new ActionListener() is
    not a statement

    I wonder that, it is that I have put the listener in a wrong place of the program or that I made a mistake in creating the listener leads to the error?

    I tried to write the listener at both the end of the for loop and the end of each if loop, but every time the compiler gives me the same error message. I write the listener according to what our book does, it's exactly the same(except the digit 1), I cannot figure out the syntax error.

    Can you tell me where I have made the mistake, please?
    Thank you very much for your help!

  10. #10
    CSE114 is offline Member
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Question

    Hi,
    I run the program in NetBean and it gives me a different error message, the code is as follow now

    Java Code:
    public void sampleTest ()
            {
                JFrame mainWindow = new JFrame("This is a test");
                mainWindow.setSize(800, 600);
            
                // Centering the window
                Dimension frameSize = mainWindow.getSize();
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            
                mainWindow.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
            
                // Add a test name
                Container contentPane = mainWindow.getContentPane();
                JLabel testName = new JLabel("Sample Test", JLabel.CENTER);
                contentPane.add(testName, BorderLayout.NORTH);
                
                JPanel centerPanel = new JPanel();
                centerPanel.setLayout(new GridLayout(questions.size(), 1));
                
                // Read questions from the Question arrayList
                for (int i = 0; i < questions.size(); i++)
                {
                    Scanner sc = new Scanner (questions.get(i).getQuestion());
                    
                    
                    JTextField field1; 
                    JTextField field2;
                    JTextField field3;
                    JTextField field4;
                    JTextField field5;
                    JTextField field6;
                    JTextField field7;
                    JTextField field8;
                    JTextField field9;
                    JTextField field10;
                    if (questions.get(i).getQuestionType().equals("TF"))
                    {
                        // Create a new Panel to hold a true false question, and then add this panel to the centerPanel later
                        JPanel trueFalsePanel = new JPanel();
                        trueFalsePanel.setLayout(new GridLayout(3, 1));
                        
                        String questionText = sc.nextLine();
                        // Add content to the text fields
                        field1 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
     
                        field1.setEditable(false);
                        field1.setEnabled(true);
                        trueFalsePanel.add(field1);
                        
                        field2 = new JTextField ("" + questionText);
                        field2.setEditable(false);
                        field2.setEnabled(true);
                        trueFalsePanel.add(field2);
                        
                        field3 = new JTextField ("Your answer");
                        field3.setEditable(true);
                        field3.setEnabled(true);
                        trueFalsePanel.add(field3);
                        
                        String userAnswer = field3.getText();
                        
                        centerPanel.add(trueFalsePanel); // Add the trueFalsePanel to the centerpanel, and then later add the centerPanel to the center of the JFrame
                        ActionListener listener1 = new ActionListener(){
                            public void actionPerformed(ActionEvent e){
                                if (userAnswer.equals(""))// [B]The compiler says userAnswer is local variable and is reached within inner class, needs to be declared as final[/B]                            {
                                    userAnswer = null;
                                }
                                answer(i, userAnswer);
                                
                            }
                        };
                        field3.addActionListener(listener1);
                    }
                    
                    if (questions.get(i).getQuestionType().equals("MC"))
                    {
                        // Create a new Panel to hold a multiple choice question, and then add this panel to the centerPanel later
                        JPanel multipleChoicePanel = new JPanel();
                        multipleChoicePanel.setLayout(new GridLayout(7,1));
                        
                        String questionText = sc.nextLine();
                        String answerChoiceA = sc.nextLine();
                        String answerChoiceB = sc.nextLine();
                        String answerChoiceC = sc.nextLine();
                        String answerChoiceD = sc.nextLine();
                        
                        field4 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
                        field4.setEditable(false);
                        field4.setEnabled(true);
                        multipleChoicePanel.add(field4);
                        
                        field5 = new JTextField ("" + questionText);
                        field5.setEditable(false);
                        field5.setEnabled(true);
                        multipleChoicePanel.add(field5);
                        
                        field6 = new JTextField ("" + answerChoiceA);
                        field6.setEditable(false);
                        field6.setEnabled(true);
                        multipleChoicePanel.add(field6);
                        
                        field7 = new JTextField ("" + answerChoiceB);
                        field7.setEditable(false);
                        field7.setEnabled(true);
                        multipleChoicePanel.add(field7);
                        
                        field8 = new JTextField ("" + answerChoiceC);
                        field8.setEditable(false);
                        field8.setEnabled(true);
                        multipleChoicePanel.add(field8);
                        
                        field9 = new JTextField ("" + answerChoiceD);
                        field9.setEditable(false);
                        field9.setEnabled(true);
                        multipleChoicePanel.add(field9);
                        
                        field10 = new JTextField ("Your answer");
                        field10.setEditable(true);
                        field10.setEnabled(true);
                        multipleChoicePanel.add(field10);
                        
                        centerPanel.add(multipleChoicePanel);
                    }
                    
                   
                }
                contentPane.add(centerPanel, BorderLayout.CENTER);
                contentPane.add(new JScrollPane(centerPanel), BorderLayout.CENTER);
                mainWindow.setVisible(true);
            }
    The compiler says I need to declare field3 as final. But since its value changes as user types in new content, so I cannot declare it as final, can you tell me what I should do here, please?
    Thank you very much!

  11. #11
    CSE114 is offline Member
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Question

    Now I have solved this problem, but I have also encounter a new one

    I try to display the questions one by one, and use the previous and forward buttons to move back and forward in the test, I want the program reads the user answer before showing the next question.

    My program now shows all the questions in a single time, and althought I have added a listener to the answer text field, the program still keeps displaying all the questions in a single time. Should I add a button as a signal that tells the program when it is allowed to display the next question?
    And I also want to know, if I want to establish a back and forward function for the GUI, should I modify the for loop in the GUI? Because I think with a for loop, I can only display a next question, but cannot display a previous question.
    I am sorry for asking so many questions at a single time.
    Thank you very much for your help!

    The code is as follow now

    Java Code:
    public void sampleTest ()
            {
                JFrame mainWindow = new JFrame("This is a test");
                mainWindow.setSize(800, 600);
            
                // Centering the window
                Dimension frameSize = mainWindow.getSize();
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            
                mainWindow.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
            
                // Add a test name
                Container contentPane = mainWindow.getContentPane();
                JLabel testTitle = new JLabel("Sample Test", JLabel.CENTER);
                contentPane.add(testTitle, BorderLayout.NORTH);
                
                JPanel centerPanel = new JPanel();
                centerPanel.setLayout(new GridLayout(questions.size(), 1));
                
                // Read questions from the Question arrayList
                for (i = 0; i < questions.size(); i++)
                {
                    
        
                    Scanner sc = new Scanner (questions.get(i).getQuestion());
                   
                    if (questions.get(i).getQuestionType().equals("TF"))
                    {
                        // Create a new Panel to hold a true false question, and then add this panel to the centerPanel later
                        JPanel trueFalsePanel = new JPanel();
                        trueFalsePanel.setLayout(new GridLayout(3, 1));
                        
                        String questionText = sc.nextLine();
                        // fill the text fields
                        field1 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
     
                        field1.setEditable(false);
                        field1.setEnabled(true);
                        trueFalsePanel.add(field1);
                        
                        field2 = new JTextField ("" + questionText);
                        field2.setEditable(false);
                        field2.setEnabled(true);
                        trueFalsePanel.add(field2);
                        
                        field3 = new JTextField ("Your answer");
                        field3.setEditable(true);
                        field3.setEnabled(true);
                        trueFalsePanel.add(field3);
                        
                        
                        centerPanel.add(trueFalsePanel); // Add the trueFalsePanel to the centerpanel, and then later add the centerPanel to the center of the JFrame
                        ActionListener listener1 = new ActionListener(){
                            public void actionPerformed(ActionEvent e){
                                userAnswer = field3.getText();
                                if (userAnswer.equals(""))
                                {
                                    userAnswer = null;
                                }
                                answer(i, userAnswer);
                                
                            }
                        };
                        field3.addActionListener(listener1);
                    }
                    
                    if (questions.get(i).getQuestionType().equals("MC"))
                    {
                        // Create a new Panel to hold a multiple choice question, and then add this panel to the centerPanel later
                        JPanel multipleChoicePanel = new JPanel();
                        multipleChoicePanel.setLayout(new GridLayout(7,1));
                        
                        String questionText = sc.nextLine();
                        String answerChoiceA = sc.nextLine();
                        String answerChoiceB = sc.nextLine();
                        String answerChoiceC = sc.nextLine();
                        String answerChoiceD = sc.nextLine();
                        
                        field4 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
                        field4.setEditable(false);
                        field4.setEnabled(true);
                        multipleChoicePanel.add(field4);
                        
                        field5 = new JTextField ("" + questionText);
                        field5.setEditable(false);
                        field5.setEnabled(true);
                        multipleChoicePanel.add(field5);
                        
                        field6 = new JTextField ("" + answerChoiceA);
                        field6.setEditable(false);
                        field6.setEnabled(true);
                        multipleChoicePanel.add(field6);
                        
                        field7 = new JTextField ("" + answerChoiceB);
                        field7.setEditable(false);
                        field7.setEnabled(true);
                        multipleChoicePanel.add(field7);
                        
                        field8 = new JTextField ("" + answerChoiceC);
                        field8.setEditable(false);
                        field8.setEnabled(true);
                        multipleChoicePanel.add(field8);
                        
                        field9 = new JTextField ("" + answerChoiceD);
                        field9.setEditable(false);
                        field9.setEnabled(true);
                        multipleChoicePanel.add(field9);
                        
                        field10 = new JTextField ("Your answer");
                        field10.setEditable(true);
                        field10.setEnabled(true);
                        multipleChoicePanel.add(field10);
                        
                        centerPanel.add(multipleChoicePanel);
                    }
                    
                   
                }
                contentPane.add(centerPanel, BorderLayout.CENTER);
                contentPane.add(new JScrollPane(centerPanel), BorderLayout.CENTER);
                mainWindow.setVisible(true);
            }
        }

  12. #12
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Check out the CardLayout as one option.
    Another option is to set up your gui to show only one question (vis-a-vis adding all in panels at construction). Change the question according to user selection with the forward/back buttons.

  13. #13
    CSE114 is offline Member
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Question

    hardwired wrote

    Another option is to set up your gui to show only one question (vis-a-vis adding all in panels at construction). Change the question according to user selection with the forward/back buttons.

    I don't understand, can you tell me what is vis-a-vis ?
    Thank you very much

  14. #14
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    vis-a-vis: face-to-face.
    One option as opposed to or versus another.

  15. #15
    CSE114 is offline Member
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Question

    Hi, hardwired

    I choose not to use CardLayout, because I don't understand it,
    Now I try to use the following method to display the test

    First, I add all the components to the mainWindow,
    and then, I try to use the setText method to change the text in the question Text Field, does this method work?

    I encounter a problem here. When I am declaring listeners, Eclipse tells me that the type new listener should implement ActionPerformed(ActionEvent e)
    But all those four listeners are in the same loop, in the same level, I don't understand why there is a problem for the last listener of those four,

    Can you explain it to me? Thank you very much!

    The exact error message is
    The type new ActionListener(){} must implement the inherited abstract method
    ActionListener.actionPerformed(ActionEvent)


    And the code

    Java Code:
    public void sampleTest ()
            {
            	JFrame mainWindow = new JFrame("This is a test");
            	mainWindow.setSize(800,600);
            	
            	// Centering the mainWindow
            	Dimension frameSize = mainWindow.getSize();
            	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            	mainWindow.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
            	
            	// Add a test title
            	Container contentPane = mainWindow.getContentPane();
            	JLabel testTitle = new JLabel("Sample Test", JLabel.CENTER);
            	contentPane.add(testTitle, BorderLayout.NORTH);
            	
            	// This panel holds one question each time, it holds the questionPanel and answerPanel
            	JPanel centerPanel = new JPanel();
            	centerPanel.setLayout(new GridLayout(2,1));
            	
            	// This panel tells the test takers their final scores
            	JPanel southPanel = new JPanel();
            	southPanel.setLayout(new GridLayout(3,1));
            	
            	// This panel holds a question and will be added to centerPanel, grid (1,1)
            	JPanel questionPanel = new JPanel();
            	questionPanel.setLayout(new GridLayout(5,1));
            	
            	// This panel allows the test takers to enter their answers, hit submit button, previous, and next buttons
            	JPanel answerPanel = new JPanel();
            	answerPanel.setLayout(new GridLayout(3,1));
            	
            	// Set up a white text field first, and then use this text field to hold a question, use the setText() method
            	// in the for loop to switch to a new question as the user hits the previous or next button, so that
            	// the program can display only one question each time
            	questionField = new JTextField();
            	questionField.setEditable(false);
            	questionField.setEnabled(true);
            	questionPanel.add(questionField);
            	
            	// Add answer choice A to the questionPanel, for true or false question, it's the true choice
            	choiceField1 = new JTextField();
            	choiceField1.setEditable(false);
            	choiceField1.setEnabled(true);
            	questionPanel.add(choiceField1);
            	
            	// Add answer choice B, for true or false question, it's choice false
            	choiceField2 = new JTextField();
            	choiceField2.setEditable(false);
            	choiceField2.setEnabled(true);
            	questionPanel.add(choiceField2);
            	
            	// Add answer choice C, for true or false question, it's blank
            	choiceField3 = new JTextField();
            	choiceField3.setEditable(false);
            	choiceField3.setEnabled(true);
            	questionPanel.add(choiceField3);
            	
            	// Add answer choice D, for true or false question, it's blank
            	choiceField4 = new JTextField();
            	choiceField4.setEditable(false);
            	choiceField4.setEnabled(true);
            	questionPanel.add(choiceField4);
            	
            	// Add the questionPanel to the centerPanel, grid(1,1)
            	centerPanel.add(questionPanel);
            	
            	// Here the users type in their answers
            	answerField = new JTextField("Your answer");
            	answerField.setEditable(true);
            	answerField.setEnabled(true);
            	answerPanel.add(answerField);
            	
            	// Create a submit button
            	submit = new JButton("Submit Answer");
            	answerPanel.add(submit);
            	
            	// Use a new panel to hold the previous and forward buttons
            	JPanel pf = new JPanel();
            	pf.setLayout(new GridLayout(1,2));
            	
            	// The previous button
            	previousButton = new JButton("Previous");
            	pf.add(previousButton);
            	
            	// The forward button
            	forwardButton = new JButton("Next");
            	pf.add(forwardButton);
            	
            	// Add the pfPanel to the answerPanel
            	answerPanel.add(pf);
            	
            	// Add answerPanel to centerPanel
            	centerPanel.add(answerPanel);
            	// Add the centerPanel to main window
            	contentPane.add(centerPanel, BorderLayout.CENTER);
            	
            	// Tell the test takers their grades
            	scoreField = new JTextField();
            	scoreField.setEditable(false);
            	scoreField.setEnabled(true);
            	southPanel.add(scoreField);
            	
            	// A user can hit the finish button to end the test
            	finishButton = new JButton("Finish");
            	southPanel.add(finishButton);
            	
            	contentPane.add(southPanel, BorderLayout.SOUTH);
            	
            	// Set up a for loop or while loop to make the program display questions one by one
            	currentQ = 0;
            	while (currentQ < questions.size())
            	{
            		queText = questions.get(currentQ).getQuestion();
            		queType = questions.get(currentQ).getQuestionType();
            		Scanner sc = new Scanner(queText);
            		if (queType.equals("TF"))
            		{
            			// Read in question to the Text Fields
            			questionField.setText(sc.nextLine());
            			choiceField1.setText(sc.nextLine());
            			choiceField2.setText(sc.nextLine());
            			choiceField3.setText("");
            			choiceField4.setText("");
            		}	
            		if (queType.equals("MC"))
            		{
            			questionField.setText(sc.nextLine());
            			choiceField1.setText(sc.nextLine());
            			choiceField2.setText(sc.nextLine());
            			choiceField3.setText(sc.nextLine());
            			choiceField4.setText(sc.nextLine());
            		}
            			// Create listeners for the answerField, previous, and next buttons
            			ActionListener answerListener = new ActionListener(){
            				public void actionPerformed(ActionEvent e1){
            					userAnswer = answerField.getText();
            				}
            			};
            			answerField.addActionListener(answerListener);
            			
            			// Submit listener
            			ActionListener submitListener = new ActionListener(){
            				public void actionPerformed(ActionEvent e2){
            					answer(currentQ, userAnswer);
            				}
            			};
            			submit.addActionListener(submitListener);
            			
            			// Previous button ActionListener
            			ActionListener previousListener = new ActionListener(){
            				public void actionPerformed(ActionEvent e3){
            					if (currentQ > 0)
            					{
            						currentQ--;
            						queText = questions.get(currentQ).getQuestion();
            						queType = questions.get(currentQ).getQuestionType();
            						Scanner preSc = new Scanner(queText);
            						if (queType.equals("TF"))
            						{
                						questionField.setText(preSc.nextLine());
                	        			choiceField1.setText(preSc.nextLine());
                	        			choiceField2.setText(preSc.nextLine());
                	        			choiceField3.setText("");
                	        			choiceField4.setText("");
                	        			answerField.setText("Your answer");
            						}
            						if (queType.equals("MC"))
            						{
            							questionField.setText(preSc.nextLine());
                	        			choiceField1.setText(preSc.nextLine());
                	        			choiceField2.setText(preSc.nextLine());
                	        			choiceField3.setText(preSc.nextLine());
                	        			choiceField4.setText(preSc.nextLine());
                	        			answerField.setText("Your answer");
            						}
            					}
            				}
            			};
            			previousButton.addActionListener(previousListener);
            			
            			// Next button ActionListener
            			ActionListener forwardListener = new ActionListener(){
            				public void ActionPerformed(ActionEvent e4){
            					if (currentQ < questions.size())
            					{
            						currentQ++;
            						
            						queText = questions.get(currentQ).getQuestion();
            						queType = questions.get(currentQ).getQuestionType();
            						Scanner forSc = new Scanner(queText);
            						if (queType.equals("TF"))
            						{
                						questionField.setText(forSc.nextLine());
                	        			choiceField1.setText(forSc.nextLine());
                	        			choiceField2.setText(forSc.nextLine());
                	        			choiceField3.setText("");
                	        			choiceField4.setText("");
                	        			answerField.setText("Your answer");
            						}
            						if (queType.equals("MC"))
            						{
            							questionField.setText(forSc.nextLine());
                	        			choiceField1.setText(forSc.nextLine());
                	        			choiceField2.setText(forSc.nextLine());
                	        			choiceField3.setText(forSc.nextLine());
                	        			choiceField4.setText(forSc.nextLine());
                	        			answerField.setText("Your answer");
            						}
            					}
            				}
            			};
            			forwardButton.addActionListener(forwardListener);
            	}
            	
            	// The finish button listener
            	ActionListener finishListener = new ActionListener(){
            		public void ActionPerformed(ActionEvent e5){
            			scoreExam(true);
            			scoreField.setText("Your final score is " + getScore() + " points");
            		}
            	};
            	finishButton.addActionListener(finishListener);
            	
            	mainWindow.setVisible(true);
            	
            }

  16. #16
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    // This empty-brace declaration
    new ActionListener(){} 
    // should be
    new ActionListener() {
        public void actionListener() {
            // nothing to do yet
        }
    };
    Implementing an interface requires that all of the methods defined in the interface be implemented; empty (do-nothing) implementations of any of the methods is okay.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ATestApp
    {
        String[] questions =
        {
            "What's the diameter of the earth?",
            "What's the distance from earth to the sun?",
            "Where's the deepest part of the Pacific ocean?",
            "How many countries have English as their native language?"
        };
        JLabel questionLabel;
        int index = 0;
    
        private JLabel getQuestionLabel()
        {
            questionLabel = new JLabel(questions[0], JLabel.CENTER);
            return questionLabel;
        }
    
        private JPanel getButtonPanel()
        {
            String[] ids = { "back", "next" };
            ActionListener al = new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    String ac = e.getActionCommand();
                    if(ac.equals("BACK"))
                    {
                        index--;
                        if(index < 0)
                            index = questions.length-1;
                    }
                    if(ac.equals("NEXT"))
                    {
                        index++;
                        if(index > questions.length-1)
                            index = 0;
                    }
                    questionLabel.setText(questions[index]);
                    questionLabel.repaint();
                }
            };
            JPanel panel = new JPanel();
            for(int i = 0; i < ids.length; i++)
            {
                JButton button = new JButton(ids[i]);
                button.setActionCommand(ids[i].toUpperCase());
                button.addActionListener(al);
                panel.add(button);
            }
            return panel;
        }
    
        public static void main(String[] args)
        {
            JFrame mainWindow = new JFrame("This is a test");
            mainWindow.setSize(800,600);
           
            // Centering the mainWindow
           	mainWindow.setLocationRelativeTo(null);
            	
            // Add a test title
            Container contentPane = mainWindow.getContentPane();
            JLabel testTitle = new JLabel("Sample Test", JLabel.CENTER);
            contentPane.add(testTitle, BorderLayout.NORTH);
    
            ATestApp test = new ATestApp();
            contentPane.add(test.getQuestionLabel());
            contentPane.add(test.getButtonPanel(), "Last");
            mainWindow.setVisible(true);
        }
    }

  17. #17
    CSE114 is offline Member
    Join Date
    May 2008
    Posts
    10
    Rep Power
    0

    Smile

    Hi, hardwired
    I really appreciate your nice help, thank you so much, you code gives me many instructions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •