/* * MCSwing.java * * Created on October 10, 2006, 9:31 PM */ package multchoicenetbeans; /** * * @author Andy */ public class MCSwing extends javax.swing.JFrame { /** Creates new form MCSwing */ public MCSwing() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // //GEN-BEGIN:initComponents private void initComponents() { lblQuestion = new javax.swing.JLabel(); btnA = new javax.swing.JButton(); btnB = new javax.swing.JButton(); btnC = new javax.swing.JButton(); btnPrev = new javax.swing.JButton(); btnNext = new javax.swing.JButton(); lblCounter = new javax.swing.JLabel(); lblFeedback = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Holy Grail Quiz"); setBackground(new java.awt.Color(255, 255, 153)); lblQuestion.setBackground(new java.awt.Color(204, 255, 102)); lblQuestion.setFont(new java.awt.Font("Tahoma", 0, 18)); lblQuestion.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); lblQuestion.setText("What is your name?"); btnA.setFont(new java.awt.Font("Tahoma", 0, 18)); btnA.setText("A) Arthur"); btnA.setActionCommand("A"); btnA.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { checkAns(evt); } }); btnB.setFont(new java.awt.Font("Tahoma", 0, 18)); btnB.setText("B) Roger"); btnB.setActionCommand("B"); btnB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { checkAns(evt); } }); btnC.setFont(new java.awt.Font("Tahoma", 0, 18)); btnC.setText("C) Brave Sir Robin"); btnC.setActionCommand("C"); btnC.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { checkAns(evt); } }); btnPrev.setFont(new java.awt.Font("Tahoma", 0, 18)); btnPrev.setText("<=="); btnPrev.setActionCommand("prev"); btnPrev.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { dec(evt); } }); btnNext.setFont(new java.awt.Font("Tahoma", 0, 18)); btnNext.setText("==>"); btnNext.setActionCommand("next"); btnNext.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { inc(evt); } }); lblCounter.setFont(new java.awt.Font("Tahoma", 0, 18)); lblCounter.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); lblCounter.setText("0"); lblFeedback.setFont(new java.awt.Font("Tahoma", 0, 18)); lblFeedback.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); lblFeedback.setText("Please click a button"); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(btnPrev, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 117, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(112, 112, 112) .add(lblCounter, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 114, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 115, Short.MAX_VALUE) .add(btnNext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 117, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(layout.createSequentialGroup() .addContainerGap() .add(lblQuestion, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 333, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .add(layout.createSequentialGroup() .add(125, 125, 125) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) .add(org.jdesktop.layout.GroupLayout.LEADING, btnC, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE) .add(btnB, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE) .add(btnA, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE) .add(org.jdesktop.layout.GroupLayout.LEADING, lblFeedback, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 333, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))) .add(117, 117, 117)) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(42, 42, 42) .add(lblQuestion) .add(42, 42, 42) .add(btnA) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(btnB) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(btnC) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(lblFeedback) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 19, Short.MAX_VALUE) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) .add(btnNext) .add(btnPrev) .add(lblCounter)) .addContainerGap()) ); pack(); }// //GEN-END:initComponents private void dec(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dec // Decrement the counter counter--; if (counter < 0){ counter = 0; } showQuestion(); }//GEN-LAST:event_dec private void inc(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inc // Increment the counter counter++; if (counter >=3){ counter = 2; } showQuestion(); }//GEN-LAST:event_inc private void checkAns(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkAns // Check the answer String guess = evt.getActionCommand(); if (guess.equals(correct)){ lblFeedback.setText("Correct!"); } else { lblFeedback.setText("Please try again"); } // end if }//GEN-LAST:event_checkAns private void showQuestion(){ //displays the current question lblCounter.setText(String.valueOf(counter)); switch(counter){ case 0: lblQuestion.setText("What is your name?"); btnA.setText("A) Arthur, King of the Britons"); btnB.setText("B) Roger the Shrubber"); btnC.setText("C) Robin the Brave"); correct = "A"; break; case 1: lblQuestion.setText("What is your quest?"); btnA.setText("A) Peace and Harmony"); btnB.setText("B) Peas and Hominy"); btnC.setText("C) The Holy Grail"); correct = "C"; break; case 2: lblQuestion.setText("What the velocity of an unladen swallow?"); btnA.setText("A) What are you talking about?"); btnB.setText("B) African or European?"); btnC.setText("C) 42 times per second"); correct = "B"; break; } // end switch lblFeedback.setText("Please click a button"); } // end showQuestion /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MCSwing().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnA; private javax.swing.JButton btnB; private javax.swing.JButton btnC; private javax.swing.JButton btnNext; private javax.swing.JButton btnPrev; private javax.swing.JLabel lblCounter; private javax.swing.JLabel lblFeedback; private javax.swing.JLabel lblQuestion; // End of variables declaration//GEN-END:variables //my own instance variables private int counter = 0; private String correct = "A"; }