Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java cardlayout question?

Options
  • 10-04-2008 1:34pm
    #1
    Registered Users Posts: 8,023 ✭✭✭


    Hey all. I'm trying to create a program which uses a cardlayout to display multiple choice quiz questions which are in turn in their own JPanel and put together using a gridlayout. I've spent the last fews days on this and I'm stuck on trying to get the questionspanel to work with the multichoiceframe. I can get the first question to appear fine but I can't get the buttons to work properly. I've tried all sorts of arrays and for loops but nothing seems to work. Could anyone give me some pointers?
    Heres my code so far (sorry but its a bit messy because I've been making this program with samples from other smaller programs)
    import java.applet.Applet;  //needed for: extends Applet
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class MultiChoiceFrame extends JFrame implements ActionListener 
    
    {
    
      
      private int currentCard = 1;
      private JPanel cardPanel;
      private CardLayout cl;
    
      public MultiChoiceFrame() {
     
        
        setTitle("Multiple Choice Question");
        cl = new CardLayout();
    
        setLayout( new BorderLayout() );
        
        JPanel buttonPanel = new JPanel();
        cardPanel = new JPanel();
        JButton firstBtn = new JButton("First");
        JButton nextBtn = new JButton("Next");
        JButton previousBtn = new JButton("Previous");
        JButton lastBtn = new JButton("Last");
        buttonPanel.add(firstBtn);
        buttonPanel.add(nextBtn);
        buttonPanel.add(previousBtn);
        buttonPanel.add(lastBtn);
        add( BorderLayout.NORTH, buttonPanel );
    
        
        JPanel results = new JPanel();
        results.add( new Label ("You scored ") );
        add( BorderLayout.SOUTH, results );
        
        
        // Questions Panel
        
        QuestionsPanel questionspanel = new QuestionsPanel();
      
        add( BorderLayout.CENTER, questionspanel );
    
      
      
    //  firstBtn.addActionListener(new ActionListener() {
    //    public void actionPerformed(ActionEvent arg0) {
    //      cl.first(questionspanel);
    //      currentCard = QuestionsPanel.question[0];
    //    }
    //  });
    //  
    //  lastBtn.addActionListener(new ActionListener() {
    //    public void actionPerformed(ActionEvent arg0) {
    //      cl.last(cardPanel);
    //      currentCard = 4;
    //    }
    //  });
    //  
    //  nextBtn.addActionListener(new ActionListener() {
    //    public void actionPerformed(ActionEvent arg0) {
    //      if (currentCard < 4) {
    //        currentCard += 1;
    //        cl.show(cardPanel, "" + (currentCard));
    //      }
    //    }
    //  });
    //  
    //  previousBtn.addActionListener(new ActionListener() {
    //    public void actionPerformed(ActionEvent arg0) {
    //      if (currentCard > 1) {
    //        currentCard -= 1;
    //        cl.show(cardPanel, "" + (currentCard));
    //      }
    //    }
    //  });
    //  
      }
    //  
    //  
      public void actionPerformed(ActionEvent e) 
     {
        
     }
      
    } //class MultiChoiceFrame
    
    public class Question
    {
      private String category;
      private String question;
      private String answer1;
      private String answer2;
      private String answer3;
      private String answer4 ; 
      private int correctAnswer;
      private boolean isCorrect=false;
      
      public int answer;
      
    
      public Question (String cat, String qst, String ans1, String ans2, String ans3, String ans4, int correctAns)
      {
        setCategory( cat );
        setQuestion( qst );
        setAnswer1( ans1 );
        setAnswer2( ans2 );
        setAnswer3( ans3 );
        setAnswer4( ans4 );
        setCorrectAnswer( correctAns );
        
      }  
    
    
      public void setCategory( String cat )
      {
      category = cat;
      }
      
      public String getCategory()
      {
      return category;
      }
       
       
      public void setQuestion( String qst )
      {
      question = qst;
      }
      
      public String getQuestion()
      {
      return question;
      }
       
       
      public void setAnswer1( String ans1 )
      {
      answer1 = ans1;
      }
      
       public String getAnswer1()
      {
      return answer1;
      }
       
       
      public void setAnswer2( String ans2 )
      {
      answer2 = ans2;
      }
      
       public String getAnswer2()
      {
      return answer2;
      }
       
       
       public void setAnswer3( String ans3 )
      {
      answer3 = ans3;
      }
      
      public String getAnswer3()
      {
      return answer3;
      }
       
       
       public void setAnswer4( String ans4 )
      {
      answer4 = ans4;
      }
      
      public String getAnswer4()
      {
      return answer4;
      }
       
       
      public void setCorrectAnswer( int correctans )
      {
      correctAnswer = correctans;
      }
      
      public int getCorrectAnswer()
      {
      return correctAnswer;
      }
       
       
    //  public String toString()
    //  {
    //  return getCategory() + " " + getQuestion()+ " " + getAnswer1() + " " 
    //     + getAnswer2() + " " + getAnswer3() + " " + getAnswer4() + " " 
    //     + getCorrectAnswer();
    //   
    //  }
       
    //  public String outputQuestion()
    //  {
    //    String str = "Category is " + getCategory() + "\n" + getQuestion() + "\n";
    //    str+= "1. " + getAnswer1() + " ";
    //    str+= "2. " + getAnswer2() + " ";
    //    str+= "3. " + getAnswer3() + " ";
    //    str+= "4. " + getAnswer4() + "\n\n";
    //    str+= "Please select your answer by typing in 1, 2, 3 or 4 and pressing enter:";
    //    
    //    return str;   
    //  }
       
      public void isAnsweredCorrectly(int ans)
      {
        if (getCorrectAnswer() == ans)
           isCorrect = true;
        else
           isCorrect = false;  
      }  
      
       
      public boolean isCorrect()
      {
        return isCorrect;   
      } 
     
       
      public int answer(int ans)
      {
        answer = ans;
        
        return answer;
      }
         
    }
    
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class QuestionsPanel extends JPanel implements ActionListener 
    {
      
       private JPanel panel;
       private JLabel category;
       private JLabel quest;
       private JLabel dummy;
       private JRadioButton a1JRadioButton; // selects answer 1
       private JRadioButton a2JRadioButton; // selects answer 2
       private JRadioButton a3JRadioButton; // selects answer 3
       private JRadioButton a4JRadioButton; // selects answer 4
       //private ButtonGroup radioGroup; // buttongroup to hold radio buttons
       private GridLayout gridLayout; // first gridlayout
       
       // no-argument constructor
       public QuestionsPanel()
       {
           
                 
         Question question[] = new Question[5];
         
         question[0] = new Question("Sport" , "What team won the 2007 FA Cup?" , "Man Utd" , "Leeds" , "Liverpool" , "Chelsea" , 4 );
         question[1] = new Question("Sport" , "Who won the 2007 Irish golf open?" , "Padraig Harrington" , "Tiger Woods" , "Sergio Garcia" , "Darren Clarke" , 1 );
         question[2] = new Question("Entertainment" , "Who won the 2008 Oscar for Best Actor?" , "Tommy Lee-Jones" , "Daniel Day-Lewis" , "George Clooney" , "Viggo Mortensen" , 2 );
         question[3] = new Question("Geography" , "What is the capital of Australia?" , "Sydney" , "Melbourne" , "Perth" , "Canberra" , 4 );
         question[4] = new Question("Science" , "What is the molecular formula for water?" , "Nacl" , "H2O" , "H30" , "N20" , 2 );
    
         
         gridLayout = new GridLayout( 7, 1 ); // 7 rows by 1 column;
         setLayout( gridLayout ); // set JPanel layout
        
         
    //     for (int i=0; i <= 4; i++)
    //     {
         JLabel category = new JLabel("Category is " + question[0].getCategory());
         JLabel quest = new JLabel("" + question[0].getQuestion());
         JLabel dummy = new JLabel("");
         
         a1JRadioButton = new JRadioButton( "1. " + question[0].getAnswer1(), false );
         a2JRadioButton = new JRadioButton( "2. " + question[0].getAnswer2(), false );
         a3JRadioButton = new JRadioButton( "3. " + question[0].getAnswer3(), false );
         a4JRadioButton = new JRadioButton( "4. " + question[0].getAnswer4(), false );
    
         add( category );
         add( quest );
         add( dummy );
          
         add( a1JRadioButton );
         add( a2JRadioButton );
         add( a3JRadioButton ); 
         add( a4JRadioButton );
    //     }          
       }
    
       public void actionPerformed( ActionEvent event )
       {}
       
    } 
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MultiChoiceFrameTest
      
    {
      
      public static void main(String args[])
      {
      
        MultiChoiceFrame multichoiceframe = new MultiChoiceFrame();
        multichoiceframe.setSize( 500, 500);                        
        multichoiceframe.setVisible(true);
        multichoiceframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
        multichoiceframe.addWindowListener //Register an anonymous class as a listener.
        (      
             new WindowAdapter() 
             {
                public void windowClosing( WindowEvent e ) 
                {  
                   System.exit( 0 );
                }
             }
        );
           
      } 
      
    }
    


Advertisement