import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.ArrayList; import java.util.Random; public class JavaQuizFrame extends javax.swing.JFrame implements java.awt.event.ActionListener { //---------------------------------------------------------------- //---------------- I N S T A N C E V A R I A B L E S ------------ //---------------------------------------------------------------- ArrayList questions = new ArrayList(); //a list of questions int num; //which question we're on int numCor; //number correct int numWrong; //number wrong Question current = null; //this is used to represent which question we are on final Color col = new Color(200, 219, 255); private JRadioButton dummyRB = new JRadioButton(); public JavaQuizFrame() { initComponents(); this.getContentPane().setBackground(col); //Add action listeners startBUT.addActionListener(this); ansBUT.addActionListener(this); //read in the file QuestionList ql = new QuestionList(); questions = ql.loadQuiz("java quiz 1.txt"); //TO DO: Scramble the mix up the questions num = numWrong = numCor = 0; //Set the size of the progress bar donePB.setMaximum(questions.size()); donePB.setValue(0); //Disable the Answer button ansBUT.setEnabled(false); ansRBG.add(dummyRB); } //=============================================================== //A C T I O N P E R F O R M E D... //=============================================================== public void actionPerformed(ActionEvent e) { //Was this the start button if (e.getSource() == startBUT) { //current = (Question) questions.get(0); //old current = questions.get(0); ansBUT.setEnabled(true); num = numCor = numWrong = 0; donePB.setValue(0); } else if (e.getSource() == ansBUT) { //Was this correct int corr = current.correctAns; if (corr == 0 && ansRB1.isSelected() || corr == 1 && ansRB2.isSelected() || corr == 2 && ansRB3.isSelected() || corr == 3 && ansRB4.isSelected()) { numCor++; } else { numWrong++; } //Display the new totals numCorrTF.setText(numCor + ""); numWrongTF.setText(numWrong + ""); num++; donePB.setValue(num); //Are we done the quiz? if (num == questions.size()) { JOptionPane.showMessageDialog(null, "You are finished, score = " + numCor, "Quiz Over", JOptionPane.INFORMATION_MESSAGE); ansBUT.setEnabled(false); } //else current = (Question) questions.get(num); //old else { current = questions.get(num); } } //Set the text of next question //Updated 11/9/2010 -> Grab the ArrayList and for loop in the lines //Clear it questTA.setText(""); for(int i=0; i < current.qText.size(); i++) { questTA.append(current.qText.get(i)+"\n"); } //questTA.setText(current.qText); ansRB1.setText(current.ans[0]); ansRB2.setText(current.ans[1]); ansRB3.setText(current.ans[2]); ansRB4.setText(current.ans[3]); //Reset Radio Buttons dummyRB.setSelected(true); } @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { ansRBG = new javax.swing.ButtonGroup(); titleLBL = new javax.swing.JLabel(); ansRB1 = new javax.swing.JRadioButton(); ansRB2 = new javax.swing.JRadioButton(); ansRB3 = new javax.swing.JRadioButton(); ansRB4 = new javax.swing.JRadioButton(); jScrollPane1 = new javax.swing.JScrollPane(); questTA = new javax.swing.JTextArea(); numCorrTF = new javax.swing.JTextField(); numWrongTF = new javax.swing.JTextField(); startBUT = new javax.swing.JButton(); correctLBL = new javax.swing.JLabel(); wrongLBL = new javax.swing.JLabel(); donePB = new javax.swing.JProgressBar(); ansBUT = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(null); titleLBL.setFont(new java.awt.Font("Arial", 1, 20)); titleLBL.setText("Java Programming Quiz, by "); getContentPane().add(titleLBL); titleLBL.setBounds(20, 11, 431, 70); ansRB1.setBackground(col); ansRBG.add(ansRB1); getContentPane().add(ansRB1); ansRB1.setBounds(20, 290, 400, 28); ansRB2.setBackground(col); ansRBG.add(ansRB2); getContentPane().add(ansRB2); ansRB2.setBounds(20, 320, 400, 28); ansRB3.setBackground(col); ansRBG.add(ansRB3); getContentPane().add(ansRB3); ansRB3.setBounds(20, 350, 400, 28); ansRB4.setBackground(col); ansRBG.add(ansRB4); getContentPane().add(ansRB4); ansRB4.setBounds(20, 380, 400, 28); jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER); questTA.setBackground(col); questTA.setColumns(20); questTA.setEditable(false); questTA.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N questTA.setLineWrap(true); questTA.setRows(5); questTA.setWrapStyleWord(true); questTA.setCaretColor(col); questTA.setSelectedTextColor(new java.awt.Color(0, 0, 0)); questTA.setSelectionColor(col); jScrollPane1.setViewportView(questTA); getContentPane().add(jScrollPane1); jScrollPane1.setBounds(20, 70, 460, 220); numCorrTF.setBackground(java.awt.Color.green); numCorrTF.setFont(new java.awt.Font("Arial", 1, 13)); getContentPane().add(numCorrTF); numCorrTF.setBounds(250, 440, 96, 23); numWrongTF.setBackground(java.awt.Color.red); numWrongTF.setFont(new java.awt.Font("Arial", 1, 13)); getContentPane().add(numWrongTF); numWrongTF.setBounds(250, 480, 96, 23); startBUT.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N startBUT.setText("Start"); getContentPane().add(startBUT); startBUT.setBounds(60, 430, 107, 24); correctLBL.setFont(new java.awt.Font("Arial", 0, 13)); correctLBL.setText("Correct"); getContentPane().add(correctLBL); correctLBL.setBounds(250, 420, 93, 18); wrongLBL.setDisplayedMnemonic('0'); wrongLBL.setFont(new java.awt.Font("Arial", 0, 13)); wrongLBL.setText("Wrong"); getContentPane().add(wrongLBL); wrongLBL.setBounds(250, 460, 93, 18); donePB.setBackground(java.awt.Color.yellow); getContentPane().add(donePB); donePB.setBounds(20, 510, 349, 25); ansBUT.setFont(new java.awt.Font("Arial", 0, 13)); ansBUT.setText("Answer"); getContentPane().add(ansBUT); ansBUT.setBounds(60, 470, 107, 24); pack(); }// //GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton ansBUT; private javax.swing.JRadioButton ansRB1; private javax.swing.JRadioButton ansRB2; private javax.swing.JRadioButton ansRB3; private javax.swing.JRadioButton ansRB4; private javax.swing.ButtonGroup ansRBG; private javax.swing.JLabel correctLBL; private javax.swing.JProgressBar donePB; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextField numCorrTF; private javax.swing.JTextField numWrongTF; private javax.swing.JTextArea questTA; private javax.swing.JButton startBUT; private javax.swing.JLabel titleLBL; private javax.swing.JLabel wrongLBL; // End of variables declaration//GEN-END:variables }