Computer Science Canada Score Table |
Author: | spiderman [ Fri Apr 20, 2007 4:07 pm ] |
Post subject: | Score Table |
Does anyone know how I can get the score to count up right, not the way I did it! Like the person answers all the questions, clicks score button and it tells the score??? //import statements import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.*; public class Quiz extends JApplet { JTextField nameTextField; // Holds on to the text field for entering name JLabel msgLabel; // Holds on to the output label int event = 0; // event counter JButton GetAnswer; //button for getting the answer JRadioButton question1Button, // All the Radio button options question2Button, question3Button, question4Button, question5Button, question6Button, question7Button, question8Button, question9Button, question10Button, question11Button, question12Button; int score = 0; //counts score JLabel Score = new JLabel (""); public void init () { JPanel mainPanel = makeGUI (); setContentPane (mainPanel); } // init method /** * AnswerListener class. **/ public class AnswerListener implements ActionListener { /** * Calculate if the person got the right asnwer or not **/ public void actionPerformed (ActionEvent e) { String name = nameTextField.getText (); if (question1Button.isSelected ()) msgLabel.setText ("Correct "); else if (question2Button.isSelected ()) msgLabel.setText ("Wrong "); else if (question3Button.isSelected ()) msgLabel.setText ("Wrong "); else if (question4Button.isSelected ()) msgLabel.setText ("Wrong "); else if (question5Button.isSelected ()) msgLabel.setText ("Wrong "); else if (question6Button.isSelected ()) msgLabel.setText ("Correct "); else if (question7Button.isSelected ()) msgLabel.setText ("Wrong "); else if (question8Button.isSelected ()) msgLabel.setText ("Correct "); else if (question9Button.isSelected ()) msgLabel.setText ("Wrong "); else if (question10Button.isSelected ()) msgLabel.setText ("Wrong "); else if (question11Button.isSelected ()) msgLabel.setText ("Correct "); else if (question12Button.isSelected ()) msgLabel.setText ("Wrong "); } } public class Correct implements ActionListener { public void actionPerformed (ActionEvent e) { score++; } } public class False implements ActionListener { public void actionPerformed (ActionEvent e) { score--; } } /** * Creates the main GUI layout in a JPanel. * * @return A new JPanel with the layout for the JApplet's GUI. **/ public JPanel makeGUI () { JPanel newPanel = new JPanel (); // headingPanel components JPanel headingPanel = new JPanel (); JLabel heading = new JLabel ("Simpson's Quiz!"); heading.setFont (new Font ("SansSerif", Font.BOLD, 24)); // headingPanel layout headingPanel.setLayout (new BoxLayout (headingPanel, BoxLayout.X_AXIS)); headingPanel.add (Box.createHorizontalGlue ()); headingPanel.add (heading); headingPanel.add (Box.createHorizontalGlue ()); // namePanel components JPanel namePanel = new JPanel (); JLabel nameLabel = new JLabel ("Your Name: "); nameLabel.setForeground (new Color (128, 0, 0)); nameTextField = new JTextField (); nameTextField.setMaximumSize (new Dimension (75, 18)); nameTextField.setPreferredSize (new Dimension (75, 18)); nameTextField.setForeground (new Color (128, 0, 0)); // namePanel layout namePanel.setLayout (new BoxLayout (namePanel, BoxLayout.X_AXIS)); namePanel.add (Box.createHorizontalGlue ()); namePanel.add (nameLabel); namePanel.add (nameTextField); namePanel.add (Box.createHorizontalGlue ()); // controlPanel components JPanel controlPanel = new JPanel (); // radioPanel components (nested panel) JPanel radioPanel = new JPanel (); ButtonGroup bg = new ButtonGroup (); // msgPanel components JPanel msgPanel = new JPanel (); msgLabel = new JLabel ("..."); msgLabel.setBackground (Color.BLACK); msgLabel.setForeground (Color.WHITE); msgLabel.setOpaque (true); msgLabel.setPreferredSize (new Dimension (150, 25)); msgLabel.setMaximumSize (new Dimension (150, 25)); msgLabel.setHorizontalAlignment (JLabel.CENTER); heading.setFont (new Font ("SansSerif", Font.BOLD, 18)); // msgPanel Layout msgPanel.setLayout (new BoxLayout (msgPanel, BoxLayout.X_AXIS)); msgPanel.add (Box.createHorizontalGlue ()); msgPanel.add (msgLabel); msgPanel.add (Box.createHorizontalGlue ()); // newPanel layout newPanel.setLayout (new BoxLayout (newPanel, BoxLayout.Y_AXIS)); newPanel.setBorder (BorderFactory.createLineBorder (Color.BLACK)); newPanel.add (headingPanel); newPanel.add (namePanel); newPanel.add (controlPanel); newPanel.add (msgPanel); newPanel.add (Box.createVerticalGlue ()); newPanel.add (Box.createHorizontalGlue ()); JLabel question1Label = new JLabel ("Is Lisa in the second grade"); newPanel.add (Box.createHorizontalGlue ()); newPanel.add (question1Label); JPanel radioPanel1 = new JPanel (); //Raido Button Anwsers //Anwser 1 question1Button = new JRadioButton ("True"); question1Button.setForeground (new Color (128, 0, 0)); question1Button.addActionListener (new Correct ()); //Anwser 2 question2Button = new JRadioButton ("False"); question2Button.setForeground (new Color (128, 0, 0)); question2Button.addActionListener (new False ()); //Creates button group ButtonGroup bg1 = new ButtonGroup (); bg.add (question1Button); bg.add (question2Button); // Border for True/false question layout radioPanel.setLayout (new BoxLayout (radioPanel, BoxLayout.Y_AXIS)); radioPanel.setBorder (BorderFactory.createLineBorder (Color.BLACK)); radioPanel.add (question1Button); radioPanel.add (question2Button); newPanel.add (radioPanel); //Question 2 newPanel.add (Box.createHorizontalGlue ()); JLabel question2Label = new JLabel ("Check the name of the Simpsons cats"); newPanel.add (Box.createHorizontalGlue ()); newPanel.add (question2Label); JPanel checkBoxPanel = new JPanel (); //Anwser 1 JCheckBox CheckBox1 = new JCheckBox ("Snowball I"); checkBoxPanel.add (CheckBox1); CheckBox1.addActionListener (new AnswerListener ()); //Anwser 2 JCheckBox CheckBox2 = new JCheckBox ("Snowball II"); checkBoxPanel.add (CheckBox2); CheckBox2.addActionListener (new AnswerListener ()); //Anwser 3 JCheckBox CheckBox3 = new JCheckBox ("Snowball III"); checkBoxPanel.add (CheckBox3); CheckBox3.addActionListener (new AnswerListener ()); //Anwser 4 JCheckBox CheckBox4 = new JCheckBox ("Snowball lV"); checkBoxPanel.add (CheckBox4); CheckBox4.addActionListener (new AnswerListener ()); //Anwser 5 JCheckBox CheckBox5 = new JCheckBox ("Snowball V"); checkBoxPanel.add (CheckBox5); CheckBox5.addActionListener (new AnswerListener ()); //Anwser 6 JCheckBox CheckBox6 = new JCheckBox ("Coltrane"); checkBoxPanel.add (CheckBox6); CheckBox6.addActionListener (new AnswerListener ()); //Anwser 7 JCheckBox CheckBox7 = new JCheckBox ("Snuffles"); checkBoxPanel.add (CheckBox7); CheckBox7.addActionListener (new AnswerListener ()); //Checkbox Border checkBoxPanel.setLayout (new BoxLayout (checkBoxPanel, BoxLayout.Y_AXIS)); checkBoxPanel.setBorder (BorderFactory.createLineBorder (Color.BLACK)); newPanel.add (checkBoxPanel); //Question 3 newPanel.add (Box.createHorizontalGlue ()); JLabel question3Label = new JLabel ("When was the Simpsons first aired?"); newPanel.add (Box.createHorizontalGlue ()); newPanel.add (question3Label); JPanel radioPanel2 = new JPanel (); // Anwser 1 question3Button = new JRadioButton ("1984"); question3Button.setForeground (new Color (128, 0, 0)); question3Button.addActionListener (new False ()); // Anwser 2 question4Button = new JRadioButton ("1972"); question4Button.setForeground (new Color (128, 0, 0)); question4Button.addActionListener (new False ()); // Anwser 3 question5Button = new JRadioButton ("1979"); question5Button.setForeground (new Color (128, 0, 0)); question5Button.addActionListener (new False ()); // Anwser 4 question6Button = new JRadioButton ("1989"); question6Button.setForeground (new Color (128, 0, 0)); question6Button.addActionListener (new Correct ()); //Button Group 2 ButtonGroup bg2 = new ButtonGroup (); bg2.add (question3Button); bg2.add (question4Button); bg2.add (question5Button); bg2.add (question6Button); // radioPanel layout radioPanel2.setLayout (new BoxLayout (radioPanel2, BoxLayout.Y_AXIS)); radioPanel2.setBorder (BorderFactory.createLineBorder (Color.BLACK)); radioPanel2.add (question3Button); radioPanel2.add (question4Button); radioPanel2.add (question5Button); radioPanel2.add (question6Button); newPanel.add (radioPanel2); //Question 4 newPanel.add (Box.createHorizontalGlue ()); JLabel question4Label = new JLabel ("Where were the shorts of the Simpsons first shown?"); newPanel.add (Box.createHorizontalGlue ()); newPanel.add (question4Label); JPanel radioPanel3 = new JPanel (); // Anwser 1 question7Button = new JRadioButton ("CNN entertainment Chanel"); question7Button.setForeground (new Color (128, 0, 0)); question7Button.addActionListener (new False ()); // Anwser 2 question8Button = new JRadioButton ("The Tracy Ulman Show"); question8Button.setForeground (new Color (128, 0, 0)); question8Button.addActionListener (new Correct ()); // Anwser 3 question9Button = new JRadioButton ("Saturday Night Live"); question9Button.setForeground (new Color (128, 0, 0)); question9Button.addActionListener (new False ()); // Anwser 4 question10Button = new JRadioButton ("Late Show with David Letterman"); question10Button.setForeground (new Color (128, 0, 0)); question10Button.addActionListener (new False ()); //Button Group 2 ButtonGroup bg3 = new ButtonGroup (); bg3.add (question7Button); bg3.add (question8Button); bg3.add (question9Button); bg3.add (question10Button); // radioPanel layout radioPanel3.setLayout (new BoxLayout (radioPanel3, BoxLayout.Y_AXIS)); radioPanel3.setBorder (BorderFactory.createLineBorder (Color.BLACK)); radioPanel3.add (question7Button); radioPanel3.add (question8Button); radioPanel3.add (question9Button); radioPanel3.add (question10Button); newPanel.add (radioPanel3); newPanel.add (Box.createHorizontalGlue ()); JLabel question5Label = new JLabel ("The Simpsons have had 17 seasons so far."); //Anwser 1 newPanel.add (Box.createHorizontalGlue ()); newPanel.add (question5Label); JPanel radioPanel4 = new JPanel (); question11Button = new JRadioButton ("True"); question11Button.setForeground (new Color (128, 0, 0)); question11Button.addActionListener (new Correct ()); //Anwser 2 question12Button = new JRadioButton ("False"); question12Button.setForeground (new Color (128, 0, 0)); question12Button.addActionListener (new False ()); //Creates button group ButtonGroup bg5 = new ButtonGroup (); bg5.add (question11Button); bg5.add (question12Button); radioPanel4.setLayout (new BoxLayout (radioPanel4, BoxLayout.Y_AXIS)); radioPanel4.setBorder (BorderFactory.createLineBorder (Color.BLACK)); radioPanel4.add (question11Button); radioPanel4.add (question12Button); newPanel.add (radioPanel4); // return the panel JButton GetAnswer = new JButton ("Get Answer!"); newPanel.add (GetAnswer); GetAnswer.addActionListener (new AnswerListener ()); newPanel.add (Score); return newPanel; } } // GUIClassTemplate class |