Results 1 to 2 of 2
Thread: simple gui java quiz
- 02-04-2013, 01:57 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 10
- Rep Power
- 0
simple gui java quiz
I'm trying to make a simple java gui quiz game. I'm making this game so I can input information to help me study for various subjects. I want this program just to ask about 20 questions multiple choice, and at the end tell you how many you got right and your percentage. Everything is going well except for the fact that I can figure out how to make the program go to the next question. I can get it to show the first question and then say if its right or wrong but it wont go on to the next question. Here's the code thanks in advance.
PS. I know I didn't do the if, else if for the second question I just want to make sure the second question shows up first. Also I know that there might be an easier way then inputting all the questions and answers like that but I don't mind, because as I manually input the questions and answers I get a chance to learn them as well.
import javax.swing.*;
import java.awt.event.*;
public class quiz2 extends JFrame
{
private JButton anwser1;
private JButton anwser2;
private JButton anwser3;
private JButton anwser4;
private JPanel panel;
private JLabel messageLabel;
private JLabel messageLabel2;
private final int WINDOW_WIDTH = 300;
private final int WINDOW_HEIGHT = 120;
int c = 0;
public quiz2()
{
setTitle("Event Object Demonstration");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
messageLabel = new JLabel("What program language is this written in");
anwser1 = new JButton("C++");
anwser2 = new JButton("Java");
anwser3 = new JButton("Unix");
anwser4 = new JButton("Pearl");
anwser1.addActionListener(new ButtonListener());
anwser2.addActionListener(new ButtonListener());
anwser3.addActionListener(new ButtonListener());
anwser4.addActionListener(new ButtonListener());
panel = new JPanel();
panel.add(messageLabel);
panel.add(anwser1);
panel.add(anwser2);
panel.add(anwser3);
panel.add(anwser4);
add(panel);
setVisible(true);
setTitle("Event Object Demonstration");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
messageLabel = new JLabel("What is 2 + 3");
anwser1 = new JButton("5");
anwser2 = new JButton("19");
anwser3 = new JButton("1");
anwser4 = new JButton("6");
anwser1.addActionListener(new ButtonListener());
anwser2.addActionListener(new ButtonListener());
anwser3.addActionListener(new ButtonListener());
anwser4.addActionListener(new ButtonListener());
panel = new JPanel();
panel.add(messageLabel);
panel.add(anwser1);
panel.add(anwser2);
panel.add(anwser3);
panel.add(anwser4);
add(panel);
setVisible(true);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
if (actionCommand.equals("C++"))
{
JOptionPane.showMessageDialog(null, "Wrong");
}
else if (actionCommand.equals("Java"))
{
JOptionPane.showMessageDialog(null, "Correct");
c++;
}
else if (actionCommand.equals("Unix"))
{
JOptionPane.showMessageDialog(null, "Wrong");
}
else if (actionCommand.equals("Pearl"))
{
JOptionPane.showMessageDialog(null, "Wrong");
}
JOptionPane.showMessageDialog(null, c + "Correct out of 1" + 100 * c/1 + "%" );
JOptionPane.showMessageDialog(null, 100 * c/1 + "%");
}
}
public static void main(String[] args)
{
quiz2 eow = new quiz2();
}
}
- 02-04-2013, 04:10 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Re: simple gui java quiz
It seems you've hard-coded the GUI to display the information for a single question, and hard-coded the check on the answers. Do you really want to be doing this for all the questions?
If not, I would recommend trying to create a "Question" class that holds the Question, Possible Answers, and the correct Answer. Use an instance of this class to populate the information on the GUI, preferably with a "setQuestion(Question question)" method so that you can then do this with other questions and avoid the hard-coding. Then after that, just create the Question instances you want to use and loop through them, displaying them and checking the answer. You should probably also only use one instance of the ButtonListener class for all 4 answer buttons.
Similar Threads
-
HELP - Java quiz application won't run
By MD1993 in forum New To JavaReplies: 4Last Post: 04-10-2012, 03:13 PM -
A multiple choice java interview quiz : Kindly provide your suggestions
By fredfel2002 in forum New To JavaReplies: 4Last Post: 01-20-2012, 09:43 AM -
Please help with java quiz (should be easy to help)
By karoloydi in forum New To JavaReplies: 1Last Post: 11-29-2011, 02:59 AM -
JAVA Quiz
By sara1988 in forum New To JavaReplies: 4Last Post: 09-14-2011, 05:20 AM -
Swing java quiz
By htuy in forum New To JavaReplies: 7Last Post: 07-08-2011, 06:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks