Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 3rd, 2013, 06:50 PM
jumpman8947 jumpman8947 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 5 jumpman8947 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 46 sec
Reputation Power: 0
Simple java gui 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();

}

}

Reply With Quote
  #2  
Old February 4th, 2013, 06:46 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Simple java gui quiz

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap