Here is my code:-

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Administrator
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.*;
import java.awt.*;

public class MyQuiz {
    int score;
JFrame frame;
String[] questions = {"Who invented the internet?", "Who invented the first space rocket?", "What is the currency of Sweden?", "Which country has the largest telescope?", "Entomology is the study of: ", "Who is the current president of South Africa?", "Who discovered oxygen?", "Which is the largest temple and where is it?", "Where is the tomb of Akbar located?", " Which state or union territory has French as an official language?"};
String[] answers = {"Tim Berners-Lee", "Joseph Godard", "Krona", "Hawaii", "Insects", "Jacob Zuma", "Joseph Priestly", "Angkor Wat In Cambodia", "Sikandra", "Pondicherry"}; 
String[] correctorwrong = new String[10];
    public class startQuizListener implements ActionListener {

        public void actionPerformed(ActionEvent ev) {
            JOptionPane.showMessageDialog(null, "Welcome to the GK quiz. Your score will be\nshown at the end. Please note that this game is\ncase sensitive. So please capitalize the first\nletter of each word. Also note that spaces, hyphens,\ncolons etc are all necessary.", "GK Quiz", 1);
            for (int i = 0; i < questions.length - 1; i++) {
                String ans = JOptionPane.showInputDialog(frame, questions[i], "GK Quiz", 3);
                if (ans.equals(answers[i])) {
                    correctorwrong[i] = "Correct";
                }
                else {
                    correctorwrong[i] = "Wrong";
                }
            }
            JOptionPane.showMessageDialog(frame, "1." + correctorwrong[0] +"\n" + "2." + correctorwrong[1] +"\n" + "3." + correctorwrong[2] +"\n" + "4." + correctorwrong[3] +"\n" + "5." + correctorwrong[4] +"\n" + "6." + correctorwrong[5] +"\n" + "7." + correctorwrong[6] +"\n" + "8." + correctorwrong[7] +"\n" + "9." + correctorwrong[8] +"\n" + "10." + correctorwrong[9]);
            
        }
    }

    
Image im = new ImageIcon("finalstaticlogo.jpg").getImage();
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        MyQuiz mcq = new MyQuiz();
        mcq.go(); 
    }
    public void go() {
        frame = new JFrame("GK Quiz");
        JButton button = new JButton("Start the quiz!");
        button.addActionListener(new startQuizListener());
        MyPanel p = new MyPanel();
        frame.getContentPane().add(p);
	frame.getContentPane().add("South", button);
        frame.setSize(550, 412);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.setResizable(false);
    }
    
    private class MyPanel extends JPanel {

        public void paintComponent(Graphics g) {
            g.drawImage(im, 0, 0, this);
        }
    }
}

When I run the program, I click the button and nothing happens. Why?
PS-The image won't work anyway, just for testing reasons, I didn't put it in the same folder as my class file ;)

Recommended Answers

All 15 Replies

emmm ... what do you mean? just tested your code here and the quiz starts, so what do you mean, nothing happens?

works for me.

I mean(on my computer) that when I click the button, nothing happens. That JOptionPane.showMessageDialog() does not ever happen!

yeah well ... here it does, so there's nothing wrong with the code. maybe you're shifting focus on something and your JOptionPane dialog is hidden behind another screen?

try putting the frame as the parent component in your first showMessageDialog as you did with the showInputDialogs

should I try to give you guys a link where there is a video of what's happening?

you can, but since it's working for us, there isn't a problem with the code we can improve.
normally it should work just fine :/

post the link I'm curious to know whats wrong

Ok, i am doing it, taking some time to upload.

try running your code via the command line using the javac and java commands, or you can put a breakpoint in your buttons actionPerformed method and run in debug mode and see if its actually firing.

I don't know what happened to NetBeans!!! It did not work properly! But in command line, javac and java worked fine! Explanation wanted.

I thought it was an issue with netbeans thats why I suggested using the command line. I think netbeans running it in the same vm as itself and doesn't spawn a new jvm. u might want to think about moving to eclipse or intellij

I doubt that's the problem. I'll run it tonight in NetBeans to check, but if that was 'normal behavior' for NetBeans, I doubt NetBeans would have as much users as they do.

UPDATE: I just tried this code running from within my NetBeans IDE, no problem at all, works the way it should.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.