?? quizwizard1.java
字號:
package cdt.projects.tree.wizards.quiz;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import cdt.Frame1;
import cdt.gui.HTMLTextPane;
import cdt.projects.Project;
import cdt.projects.tree.nodes.*;
import cdt.wizard.WizardFrame;
/**
* Initiates a wizard to create a quiz in a project.
*/
public final class QuizWizard1 extends WizardFrame {
/** The JFrame acting as the parent to this wizard. */
private JFrame parent;
/** The Node representing the 'Quiz/Questions' folder. */
private Node oldQuestionsFolder;
/** The Node representing the 'Quiz/Answers' folder. */
private Node oldAnswersFolder;
/** The question that this instance of the QuizWizard represents. */
private int questionNumber;
/** The JPanel associated with the answer feedback. */
private JPanel answerPanel;
/**
* The {@link cdt.gui.HTMLTextPane HTMLTextPane} containing
* the text of the question.
*/
private HTMLTextPane questionText;
/**
* The {@link cdt.gui.HTMLTextPane HTMLTextPane} containing
* the text of a choice.
*/
private HTMLTextPane choiceText;
/**
* The {@link cdt.gui.HTMLTextPane HTMLTextPane} containing
* the text of the feedback to a choice.
*/
private HTMLTextPane feedbackText;
/**
* The JCheckBox indicating whether or not the selected answer is the
* correct answer.
*/
private JCheckBox isCorrectChoice;
/** The JPanel associated with the question text. */
private JPanel questionPanel;
/** The JList containing the choices for the question. */
private JList choicesList;
/**
* The {@link cdt.projects.tree.nodes.Question Question} that this
* frame of the wizard represents.
*/
private Question question;
/**
* Creates the quiz wizard, starting on question number 1.
*
* @param parent The parent JFrame, in this case typically an instance of
* {@link cdt.Frame1 Frame1}.
*/
public QuizWizard1(JFrame parent) {
this(parent, 1);
}
/**
* Creates the quiz wizard.
*
* @param parent The parent JFrame, in this case typically an instance of
* {@link cdt.Frame1 Frame1}.
* @param questionNumber The number of the question that this QuizWizard
* is gathering information for.
*/
private QuizWizard1(JFrame parent, int questionNumber) {
super(parent);
this.parent = parent;
this.questionNumber = questionNumber;
// Finds the 'Quiz/Questions' folder, or creates it if it cannot be found
this.oldQuestionsFolder = (Node)data.get("oldQuestionsFolder");
this.oldAnswersFolder = (Node)data.get("oldAnswersFolder");
final String questionString = "Question #" +this.questionNumber;
// Sets up the question to hold whatever information is already in the project tree
Node questionNode = Node.findChildNode(this.oldQuestionsFolder, questionString);
if(null != questionNode) {
if(questionNode instanceof Question) {
this.question = (Question)questionNode;
} else {
this.question = new Question("Question #" +this.questionNumber);
this.question.setData(questionNode.getData());
}
} else {
// Couldn't find the question in the old questions folder, so make a new one
this.question = new Question("Question #" +this.questionNumber);
this.question.setupDefault(this.questionNumber);
}
// Copies any previous answer information into the answer we store for this question
Node answerNode = Node.findChildNode(this.oldAnswersFolder, questionString);
if(null != answerNode && answerNode instanceof Hold) {
LinkedList answerChildren = answerNode.getChildren();
for(int i = 0; i < answerChildren.size(); ++i) {
Node n = Node.findChildNode(answerNode, "Answer #" +(i+1));
if(null != n) {
this.question.setFeedback(i, n.getData());
}
}
}
// Initializes the wizard
init();
}
/**
* Sets up the layout of this page.
*/
public void customize() {
showPreviousButton(true);
showFinishButton(true);
setTitle("Quiz Wizard");
Dimension dim;
// Sets up a JScrollPane for the question text
this.questionText = new HTMLTextPane(parent, this.question.getQuestion());
dim = new Dimension(360, 120);
this.questionPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(questionText);
scrollPane.setPreferredSize(dim);
scrollPane.setMinimumSize(dim);
scrollPane.setMaximumSize(dim);
scrollPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.lightGray,
Color.lightGray,Color.gray,Color.darkGray));
setupBorder(this.questionPanel, "Question " +this.questionNumber);
this.questionPanel.add(scrollPane);
dim = new Dimension(370, 100);
this.questionPanel.setMaximumSize(dim);
// Sets up the HTMLTextPane for the text for the selected choice
this.choiceText = new HTMLTextPane(parent, "");
this.choiceText.setEditable(false);
this.choiceText.setBackground(Color.lightGray);
dim = new Dimension(360, 120);
JScrollPane scrollPane3 = new JScrollPane(this.choiceText);
scrollPane3.setPreferredSize(dim);
scrollPane3.setMinimumSize(dim);
scrollPane3.setMaximumSize(dim);
Border b = BorderFactory.createLineBorder(Color.darkGray);
b = BorderFactory.createTitledBorder(b, "Answer Text");
((TitledBorder)b).setTitleJustification(TitledBorder.CENTER);
scrollPane3.setBorder(b);
// Sets up an HTMLTextPane for the feedback for the selected choice
this.feedbackText = new HTMLTextPane(parent, "");
this.feedbackText.setEditable(false);
this.feedbackText.setBackground(Color.lightGray);
dim = new Dimension(360, 120);
JScrollPane scrollPane4 = new JScrollPane(this.feedbackText);
scrollPane4.setPreferredSize(dim);
scrollPane4.setMinimumSize(dim);
scrollPane4.setMaximumSize(dim);
b = BorderFactory.createLineBorder(Color.darkGray);
b = BorderFactory.createTitledBorder(b, "Feedback");
((TitledBorder)b).setTitleJustification(TitledBorder.CENTER);
scrollPane4.setBorder(b);
// Sets up a JList of the choices
Object choices[] = this.question.getChoices().toArray();
DefaultListModel listModel = new DefaultListModel();
for(int i = 0; i < choices.length; ++i) {
if(i == question.getCorrectChoice()) {
listModel.addElement("Answer " +(i+1)+ " **");
} else {
listModel.addElement("Answer " +(i+1));
}
}
this.choicesList = new JList(listModel);
this.choicesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.choicesList.addListSelectionListener(new ChoiceSelectionListener());
// Puts the list into a JScrollPane
dim = new Dimension(90, 120);
JScrollPane scrollPane2 = new JScrollPane(this.choicesList);
scrollPane2.setMaximumSize(dim);
scrollPane2.setPreferredSize(dim);
scrollPane2.setMinimumSize(dim);
// Sets up a JCheckBox for whether or not the currently selected choice is correct
isCorrectChoice = new JCheckBox("Is this choice correct?");
isCorrectChoice.addActionListener(new CheckBoxListener(this.choicesList));
isCorrectChoice.setEnabled(false);
// Lays out the answerText, the feedbackText, and the isCorrectChoice
// to be stacked on top of eachother vertically.
JPanel nestedPanel = new JPanel();
nestedPanel.setLayout(new BoxLayout(nestedPanel, BoxLayout.Y_AXIS));
nestedPanel.add(scrollPane3);
nestedPanel.add(Box.createVerticalStrut(5));
nestedPanel.add(scrollPane4);
nestedPanel.add(Box.createVerticalStrut(5));
nestedPanel.add(new JSeparator(JSeparator.HORIZONTAL));
nestedPanel.add(Box.createVerticalStrut(5));
nestedPanel.add(isCorrectChoice);
// Lays out the list to be on the left of the answerText and feedbackText
this.answerPanel = new JPanel();
this.answerPanel.setLayout(new BoxLayout(this.answerPanel, BoxLayout.X_AXIS));
this.answerPanel.add(Box.createHorizontalStrut(10));
this.answerPanel.add(scrollPane2);
this.answerPanel.add(Box.createHorizontalStrut(10));
this.answerPanel.add(new JSeparator(JSeparator.VERTICAL));
this.answerPanel.add(Box.createHorizontalStrut(10));
this.answerPanel.add(nestedPanel);
setupBorder(this.answerPanel, "No Answer Selected");
// Lays out the question section and the answer section to be
// stacked vertically
JPanel panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.add(this.questionPanel);
panel2.add(Box.createVerticalStrut(5));
panel2.add(new JSeparator(JSeparator.HORIZONTAL));
panel2.add(Box.createVerticalStrut(5));
panel2.add(this.answerPanel);
// Adds the entire thing to the wizard panel.
centerPanel.add(panel2, BorderLayout.CENTER);
}
/**
* Sets up an HTMLEditorKit with the appropriate border.
*
* @param c The component to put the border onto.
* @param title The title for the border.
*/
private static void setupBorder(JComponent c, String title) {
TitledBorder b = BorderFactory.createTitledBorder(title);
b.setTitleJustification(TitledBorder.CENTER);
c.setBorder(b);
}
/**
* Saves the question given the values currently in the
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -