| | |
creating a JOptionPane quiz
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2007
Posts: 57
Reputation:
Solved Threads: 2
This is the hardest thing I have tried yet. I am not adding ti as part of the main programme I am doing at the moment cus otherwise it would be too hard to debug. So I have created this as a separate file. I am trying to get a Joptionpane to show ask a question with 2 radio buttons to select the answer. IF the correct answer is shown then another JOptionpane will show saying correct. I also have with this a way to record correct questions you will understand better from the code. once the correct or incorrect box for selecting the wrong answer shows then when the user presses okay it goes to the next question. I am only starting with two questions to keep it simple then once thats mastered I will create 40. anyway here is my code. so anything I am doing wrong or need to do please show me. my biggest problem is an error saying incompatible types. it is cus I am trying to make an equal between a radiobutton and a string but I dont know how else to do it.
import javax.swing.*;
public class QuizFourBib
{
public static void main (String args[])
{
int res = 0;
JRadioButton dav = new JRadioButton("David");
JRadioButton saul = new JRadioButton("saul");
String ques1 = JOptionPane.showInputDialog(null,"who killed goliath?"+dav+saul,"question1",JOptionPane.QUESTION_MESSAGE);
if (ques1 == dav)
{
res=res+1;
}
else if (ques1 == saul)
{
res=res+0;
JOptionPane.showMessageDialog(null,"hard luck","",JOptionPane.INFORMATION_MESSAGE);
}
JRadioButton egypt = new JRadioButton("egypt");
JRadioButton samaria = new JRadioButton("samaria");
String ques2 = JOptionPane.showInputDialog(null,"where was moses born?"+egypt+samaria,"question1",JOptionPane.QUESTION_MESSAGE);
if (ques2 == egypt)
{
res=res+1;
}
else if (ques2 == samaria)
{
res=res+0;
JOptionPane.showMessageDialog(null,"hard luck","",JOptionPane.INFORMATION_MESSAGE);
}
System.out.println(res);
}
}
import javax.swing.*;
public class QuizFourBib
{
public static void main (String args[])
{
int res = 0;
JRadioButton dav = new JRadioButton("David");
JRadioButton saul = new JRadioButton("saul");
String ques1 = JOptionPane.showInputDialog(null,"who killed goliath?"+dav+saul,"question1",JOptionPane.QUESTION_MESSAGE);
if (ques1 == dav)
{
res=res+1;
}
else if (ques1 == saul)
{
res=res+0;
JOptionPane.showMessageDialog(null,"hard luck","",JOptionPane.INFORMATION_MESSAGE);
}
JRadioButton egypt = new JRadioButton("egypt");
JRadioButton samaria = new JRadioButton("samaria");
String ques2 = JOptionPane.showInputDialog(null,"where was moses born?"+egypt+samaria,"question1",JOptionPane.QUESTION_MESSAGE);
if (ques2 == egypt)
{
res=res+1;
}
else if (ques2 == samaria)
{
res=res+0;
JOptionPane.showMessageDialog(null,"hard luck","",JOptionPane.INFORMATION_MESSAGE);
}
System.out.println(res);
}
}
Well, you can't quite work with a custom input dialog like that. There is a way to do it with an Object array for responses, but if you want radio buttons you need to create your own custom dialog. Just for the heck of it, I wrote up one way you could put this all together with JDialogs. Of course, you could probably do it a bit easier with a simple panel. Perhaps this will give you some ideas to work from (the simple BoxLayout is pretty awful)
java Syntax (Toggle Plain Text)
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import javax.swing.*; public class QuizFourBib extends JDialog { List<Question> questions = new ArrayList<Question>(); int score=0; ButtonGroup group = new ButtonGroup(); JButton btnOk = new JButton("Ok"); public QuizFourBib(){ super(); setModal(true); setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS)); btnOk.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); } }); questions.add(new Question("Who killed Goliath?", new String[]{"David", "Saul"}, "David")); questions.add(new Question("Where was Moses born?", new String[]{"Egypt","Samaria"}, "Egypt")); } public int startQuiz(){ int score=0; for (Question q : questions){ displayQuestion(q); if (group.getSelection().getActionCommand().equals(q.getCorrectAnswer())){ score++; } } dispose(); return score; } private void displayQuestion(Question q){ getContentPane().removeAll(); for (Enumeration buttonGroup=group.getElements(); buttonGroup.hasMoreElements(); ){ group.remove((AbstractButton)buttonGroup.nextElement()); } JLabel questionText = new JLabel(q.getQuestion()); getContentPane().add(questionText); for (String answer : q.getAnswers()){ JRadioButton radio = new JRadioButton(answer); radio.setActionCommand(answer); group.add(radio); getContentPane().add(radio); } getContentPane().add(btnOk); pack(); setVisible(true); } public static void main(String args[]) { QuizFourBib quiz = new QuizFourBib(); int score = quiz.startQuiz(); JOptionPane.showMessageDialog(null,"Your score: "+score,"",JOptionPane.INFORMATION_MESSAGE); } class Question { String question=""; String[] answers; String correctAnswer; public Question(String question, String[] possibleAnswers, String correctAnswer){ this.question = question; this.answers = possibleAnswers; this.correctAnswer = correctAnswer; } public String getQuestion() { return question; } public String[] getAnswers() { return answers; } public String getCorrectAnswer() { return correctAnswer; } } }
•
•
Join Date: Jul 2007
Posts: 57
Reputation:
Solved Threads: 2
wow. you are good at java. I tried the code and what I did was I added lines where you put the questions and was able to add new questions very simply. There are some things in that which I dont quite understand. some of the code you posted I have never seen before. So thnx cus it means I can learn some new stuff.
Also why are you so specific with the stuff u import? I am in the habbit of just ending them with .* cus it covers everything you need then and saves lines of code. is that bad practise?
Also why are you so specific with the stuff u import? I am in the habbit of just ending them with .* cus it covers everything you need then and saves lines of code. is that bad practise?
Quick addition on that code. Where the answer is compared, a null pointer exception is possible if no answer is chosen when you click ok. To fix it, change to this line If you are using many classes from a package, it can be just fine to use .* in most cases. If however one of your class names appears in multiple packages that you import with .*, like say List which is present in java.awt and java.util then it will be ambiguous and the compiler won't like it.
Java Syntax (Toggle Plain Text)
if (group.getSelection()!=null && group.getSelection().getActionCommand().equals(q.getCorrectAnswer())){
•
•
•
•
Also why are you so specific with the stuff u import? I am in the habbit of just ending them with .* cus it covers everything you need then and saves lines of code. is that bad practise?
![]() |
Similar Threads
- Help in Creating a Forum (Web Hosting Deals)
- Creating a Boot Disk for an NTFS or FAT Partition (Windows tips 'n' tweaks)
- Creating an OS in visual basic 6.0 (Visual Basic 4 / 5 / 6)
- Creating a disk image (OS X)
- Error message when creating PDF (OS X)
- Creating a Address Book using Random Access Files (Visual Basic 4 / 5 / 6)
- Creating and destroying objects (C++)
Other Threads in the Java Forum
- Previous Thread: ArrayList get()
- Next Thread: Array in Dice Program
Views: 2182 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api appinventor apple applet application arguments array arrays automation bi binary blackberry bluetooth chat chooser class classes client code compile compiler component converter database draw eclipse error event exception file fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaprojects jetbrains jmf jni jpanel jtable julia learningresources linux list login loop map method methods mobile myregfun netbeans newbie notdisplaying number object oracle page print problem program programming project qt recursion scanner screen server set size sms socket sort sql string swing system test threads time transfer tree variablebinding windows xor






