Multiple Choice Applet
/*
* MultChoice.java
*
* Created on March 4, 2007, 11:42 PM
*
* Demo custom data class, applet with standalone frame
*/
/**
*
* @author aharris
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MultChoice extends JApplet implements ActionListener{
//simply makes a button and calls up main frame when needed
JButton btnSeeQuiz = new JButton("See Quiz");
public void init(){
Container panel = this.getContentPane();
panel.setLayout(new FlowLayout());
panel.add(btnSeeQuiz);
btnSeeQuiz.addActionListener(this);
} // end init
public void actionPerformed(ActionEvent e){
//create and display the frame
MCFrame theFrame = new MCFrame();
theFrame.pack();
theFrame.setVisible(true);
} // end actionPerformed
}// end MultChoice
class Problem {
String question;
String A;
String B;
String C;
String correct;
}
class MCFrame extends JFrame implements ActionListener{
// the actual Multiple choice frame
JLabel lblQuestion = new JLabel("Question");
JButton btnA = new JButton("Answer A");
JButton btnB = new JButton("Answer B");
JButton btnC = new JButton("Answer C");
JLabel lblOutput = new JLabel("Output");
Panel pnlControls = new Panel();
JButton btnPrev = new JButton("<==");
JLabel lblCounter = new JLabel("Counter");
JButton btnNext =new JButton("==>");
Problem[] problem = new Problem[3];
int probNum = 0;
public MCFrame(){
Container pnlMain = this.getContentPane();
pnlMain.setLayout(new GridLayout(0, 1));
pnlMain.add(lblQuestion);
pnlMain.add(btnA);
pnlMain.add(btnB);
pnlMain.add(btnC);
pnlMain.add(lblOutput);
pnlControls.setLayout(new GridLayout(1, 0));
pnlControls.add(btnPrev);
pnlControls.add(lblCounter);
pnlControls.add(btnNext);
pnlMain.add(pnlControls);
//actionListeners
btnA.addActionListener(this);
btnB.addActionListener(this);
btnC.addActionListener(this);
btnPrev.addActionListener(this);
btnNext.addActionListener(this);
//give better action Commands
btnA.setActionCommand("A");
btnB.setActionCommand("B");
btnC.setActionCommand("C");
//general management
setupProblems();
showProb(probNum);
//cosmetics
lblCounter.setHorizontalAlignment(lblCounter.CENTER);
} // end constructor
public void setupProblems(){
problem[0] = new Problem();
problem[0].question = "What is your name?";
problem[0].A = "Arthur, King of the Britons";
problem[0].B = "Roger the Shrubber";
problem[0].C = "Brave Sir Robin";
problem[0].correct = "A";
problem[1] = new Problem();
problem[1].question = "What is your quest?";
problem[1].A = "I seek the Holy Grail";
problem[1].B = "I seek the perfect pizza topping";
problem[1].C = "I seek a shrubbery";
problem[1].correct = "A";
problem[2] = new Problem();
problem[2].question = "What is your favorite color?";
problem[2].A = "Red";
problem[2].B = "Green";
problem[2].C = "Yellow - no, blue!";
problem[2].correct = "C";
}
public void actionPerformed(ActionEvent e){
// check to see what type of action occurred
String command = e.getActionCommand();
if (command.equals("A")){
checkAns("A");
} else if (command.equals("B")){
checkAns("B");
} else if (command.equals("C")){
checkAns("C");
} else if (command.equals("<==")){
prev();
} else if (command.equals("==>")){
next();
} else {
System.out.println(command);
} // end if
} // end actionPerformed
public void checkAns(String guess){
lblOutput.setText(guess);
String correct = problem[probNum].correct;
if (guess.equals(correct)){
lblOutput.setText("GREAT");
} else {
lblOutput.setText("Try again");
} // end if
} // end checkAns
public void prev(){
//move to the previous question
lblOutput.setText("prev");
probNum--;
if (probNum < 0){
probNum = 0;
} // end if
showProb(probNum);
} // end prev
public void next(){
//move to the previous question
lblOutput.setText("next");
probNum++;
if (probNum >= 3){
probNum = 2;
} // end if
showProb(probNum);
} // end prev
public void showProb(int probNum){
//given a problem number, display that problem
lblQuestion.setText(problem[probNum].question);
btnA.setText(problem[probNum].A);
btnB.setText(problem[probNum].B);
btnC.setText(problem[probNum].C);
lblOutput.setText("please choose");
lblCounter.setText(String.valueOf(probNum));
} // end showProb
} // end MCFrame