3 Replies - 7137 Views - Last Post: 03 February 2013 - 09:33 PM Rate Topic: -----

#1 jumpman8947  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 03-February 13

simple java gui quiz

Posted 03 February 2013 - 05:27 PM

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

	}

}


This post has been edited by macosxnerd101: 03 February 2013 - 05:52 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: simple java gui quiz

#2 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: simple java gui quiz

Posted 03 February 2013 - 07:23 PM

You may want to break the various questions up into different tabs. It will look better and will be overall easier to work with here are a few tutorials to get you going:
http://www.dreaminco...abbed-pane-gui/
http://docs.oracle.c...tabbedpane.html
http://www.java-tips...sing-swing.html

This post has been edited by k3y: 03 February 2013 - 07:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1369
  • View blog
  • Posts: 3,019
  • Joined: 05-April 11

Re: simple java gui quiz

Posted 03 February 2013 - 07:37 PM

You need to add an action command to all of your buttons

Suggestions:
Learn how to use arrays. The buttons answer1, 2, 3, and 4 could be placed in an array
Don't change the layout of the component after setting it visible (especially for beginners)
Change what the buttons/labels are showing instead of changing the layout. You can easily change the text of the labels and buttons

Learn OOP and design your own classes! It will be VERY hard for you to handle more than one question with your current code

This post has been edited by CasiOo: 03 February 2013 - 07:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 jumpman8947  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 03-February 13

Re: simple java gui quiz

Posted 03 February 2013 - 09:33 PM

Thanks I will read up on arrays and use them in my program, hopefully I can get this to work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1