JFrame
中的考试制作工具,在您完成问题后,会弹出JOptionPane
并询问您是否要重新录制,我点击否,该程序仍在运行。
package quizmaker;
import javax.swing.JOptionPane;
public class QuizMaker extends javax.swing.JFrame {
private static QuestionBuilder Questions = new QuestionBuilder();
private static String[] Question = new String[5];
private static String[] Answer = new String[5];
private static String[] Bogus1 = new String[5];
private static String[] Bogus2 = new String[5];
private static String[] Bogus3 = new String[5];
private static int[] QuestionsIndex = {0, 1, 2, 3, 4};
private static int Index=0, Score=0, QuestionNumber=0;
public QuizMaker() {
initComponents();
goToNext();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setResizable(false);
jLabel1.setText("jLabel1");
jLabel2.setText("jLabel2");
jTextField1.setEditable(false);
jTextField1.setText("jTextField1");
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton2.setText("jButton2");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});
jButton3.setText("jButton3");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton3MouseClicked(evt);
}
});
jButton4.setText("jButton4");
jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton4MouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton4))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String YourAnswer = jButton1.getText();
checkAnswer(YourAnswer);
goToNext();
}
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String YourAnswer = jButton2.getText();
checkAnswer(YourAnswer);
goToNext();
}
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String YourAnswer = jButton3.getText();
checkAnswer(YourAnswer);
goToNext();
}
private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String YourAnswer = jButton4.getText();
checkAnswer(YourAnswer);
goToNext();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(QuizMaker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(QuizMaker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(QuizMaker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(QuizMaker.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
for (int x=0; x<=4; x++) {
QuestionNumber = x + 1;
Question[x] = JOptionPane.showInputDialog(null, "Enter Question # " + QuestionNumber, "Question # " + QuestionNumber);
Answer[x] = JOptionPane.showInputDialog(null, "Enter Answer in Question # " + QuestionNumber, "Answer in Question # " + QuestionNumber);
Bogus1[x] = JOptionPane.showInputDialog(null, "Enter Bogus 1 in Question # " + QuestionNumber, "Bogus 1 in Question # " + QuestionNumber);
Bogus2[x] = JOptionPane.showInputDialog(null, "Enter Bogus 2 in Question # " + QuestionNumber, "Bogus 2 in Question # " + QuestionNumber);
Bogus3[x] = JOptionPane.showInputDialog(null, "Enter Bogus 3 in Question # " + QuestionNumber, "Bogus 3 in Question # " + QuestionNumber);
}
QuestionNumber=1;
for (int x=4; x>=0; x--) {
int y=x+1;
int w=(int)(Math.random()*y);
int Container = QuestionsIndex[w];
QuestionsIndex[w] = QuestionsIndex[x];
QuestionsIndex[x] = Container;
}
Message("Let's start the quiz.");
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new QuizMaker().setVisible(true);
}
});
}
public static void goToNext() {
if (QuestionNumber > 5) {
Message("Your score is " + Score + " out of 5.");
int ConfirmButton = JOptionPane.YES_NO_CANCEL_OPTION;
JOptionPane.showConfirmDialog(null, "Do yo want to retake?", null, ConfirmButton);
if (ConfirmButton == JOptionPane.YES_OPTION) {
Score=0;
QuestionNumber=1;
for (int x=4; x>=0; x--) {
int y=x+1;
int w=(int)(Math.random()*y);
int Container = QuestionsIndex[w];
QuestionsIndex[w] = QuestionsIndex[x];
QuestionsIndex[x] = Container;
}
goToNext();
}
else {
System.exit(0);
}
}
else if (QuestionNumber <= 5) {
jLabel1.setText("Question " + QuestionNumber + " of 5");
jLabel2.setText("Score: " + Score);
Questions.setQuestion(Question[QuestionsIndex[QuestionNumber-1]]);
Questions.setAnswer(Answer[QuestionsIndex[QuestionNumber-1]]);
Questions.setBogus1(Bogus1[QuestionsIndex[QuestionNumber-1]]);
Questions.setBogus2(Bogus2[QuestionsIndex[QuestionNumber-1]]);
Questions.setBogus3(Bogus3[QuestionsIndex[QuestionNumber-1]]);
String[] Choices = new String[4];
Choices[0] = Questions.getAnswer();
Choices[1] = Questions.getBogus1();
Choices[2] = Questions.getBogus2();
Choices[3] = Questions.getBogus3();
for (int x=3; x>=0; x--) {
int y=x+1;
int w=(int)(Math.random()*y);
String Container = Choices[w];
Choices[w] = Choices[x];
Choices[x] = Container;
}
jTextField1.setText(Questions.getQuestion());
jButton1.setText(Choices[0]);
jButton2.setText(Choices[1]);
jButton3.setText(Choices[2]);
jButton4.setText(Choices[3]);
QuestionNumber++;
}
}
public static void checkAnswer(String YourAnswer) {
if (YourAnswer.equals(Questions.getAnswer())) {
Message("Correct!");
Score++;
}
else {
Message("Wrong!");
}
}
public static void Message(Object Message) {
JOptionPane.showMessageDialog(null, Message);
}
// Variables declaration - do not modify
private static javax.swing.JButton jButton1;
private static javax.swing.JButton jButton2;
private static javax.swing.JButton jButton3;
private static javax.swing.JButton jButton4;
private static javax.swing.JLabel jLabel1;
private static javax.swing.JLabel jLabel2;
private static javax.swing.JTextField jTextField1;
// End of variables declaration
}
答案 0 :(得分:4)
用户选择的选项是showConfirmDialog(...)
方法的返回值。你必须做这样的事情:
int selected = JOptionPane.showConfirmDialog(null, "Do yo want to retake?", null, JOptionPane.YES_NO_CANCEL_OPTION);
if(selected == JOptionPane.YES_OPTION){
...
}