Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: simple gui java quiz

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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();
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simple gui java quiz

    Where is the next question? I see one. Where is the next one?
    Do you know how to use arrays?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple gui java quiz

    The first question is what language is this written in, the second is what is 2 + 3. I didn't do the if statements for 2 + 3 yet... I used before but its been a while since I actually had to use them. Thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simple gui java quiz

    Do you know how to use arrays or arraylists? That would be a place to put the questions and answers so the program could have a choice.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple gui java quiz

    I know about arrays but I would have to do some more reading and refresh my memory. so your telling me all I have to do is add arrays to my program and it should work?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: simple gui java quiz

    it should work?
    Sorry, its not that simple. It will require programming to fit logic for using an array into the code.

    Also posted at http://forums.devshed.com/java-help-...ml#post2853319
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Do You Know the Answer to Java Quiz?
    By dblog616 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 13th, 2012, 08:17 PM
  2. Making a Java quiz with Classes
    By diagnonsense in forum Java Theory & Questions
    Replies: 2
    Last Post: March 26th, 2012, 06:53 AM
  3. simple translator GUI ??
    By sciences in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 4th, 2012, 06:18 AM
  4. Free Java quiz .
    By skoiloth in forum Totally Off Topic
    Replies: 11
    Last Post: January 9th, 2012, 11:16 AM
  5. Replies: 3
    Last Post: November 10th, 2011, 07:11 AM