Jump to content

Converting JDialog to JFrame code error

- - - - -

  • Please log in to reply
12 replies to this topic

#1
an2kenz_143

an2kenz_143

    Newbie

  • Members
  • PipPip
  • 12 posts
I have a question and answer game...the question is in the form of array.. and extends JDialog but even if i convert into JFrame... too many errors occur.. Is using array possible in JFrame for question and answer game? I dont know how to use 1 frame for all question and choices...like in JDialog that automatically when the next question occurs, the question and the choices automatically can easily validate because of arrays. can anyone fix the debug error? Converting JDialog into JFrame

heres my code...for my question answer using JDialog

package Games;


import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.List;

import javax.swing.*;

 

public class Quiz extends JDialog {

        List<Question> questions = new ArrayList<Question>();

        int score=0;

        ButtonGroup group = new ButtonGroup();

        JButton btnOk = new JButton("Ok");

 

        public Quiz(){

            super();

            setModal(true);

            setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));

            btnOk.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    setVisible(false);

                }

            });

 

            questions.add(new Question("What is Java (in regard to Computer Science) ?", new String[]{"A. Type of Cofee", "B. Object Oriented Programming,","C. Interactive Website","D. None of the Above"}, "B. Object Oriented Programming"));

            questions.add(new Question("What's the difference between an Applet and an application ?", new String[]{"A. An application is only available in Windows","B. Applet can paint words, application cannot","C. Applet are run over the web","D. None of the Above "}, "C. Applet are run over the web"));

            questions.add(new Question("What shortcut we use for importing objects in 'Eclipse' IDE?", new String[]{"A. CTRL + O"," B. ALT + O","C. CTRL + SHIFT + O","D. ALT + SHIFT + O"}, "C. CTRL + SHIFT + O"));

            questions.add(new Question("What shortcut we use to compile & run a source code in 'Eclipse' IDE?", new String[]{"A. ALT + SHIFT + X and J","B. CTRL + SHIFT + J and X","C. CTRL + ALT + J and Q","D.ALT + CTRL + Q"}, "A. ALT + SHIFT + X and J"));

            questions.add(new Question("What is the current version for 'JDK' Kit?", new String[]{"A. JDK 1.4 Update 1","B. JDK 1.5 Update 10","C. JDK 1.6 Update 9","D. JDK 1.6 Update 3"}, "D. JDK 1.6 Update 3"));

            questions.add(new Question("Which option is not an IDE 'Integrated Development Environment'?", new String[]{"A. Eclipse","B. JIDE","C. JCreator","D. NetBeans"}, "B. JIDE"));

            questions.add(new Question("Which option is not a feature of JAVA?", new String[]{"A. Cross Platform","B. Memory Management","C. Easy GUI Creation","D. Object - Oriented Programming"}, "B. Memory Management"));

            questions.add(new Question("What is the name of the component that we use to create dialog box?", new String[]{"A. JtextField","B. JOptionPane","C. JtextPanel","D. JTextArea"}, "B. JOptionPane"));

            questions.add(new Question("What should be the name of the class, if the file name is Hello.java?", new String[]{"A. _HeLLO","B. hello","C. HELLO","D. Hello"}, "D. Hello"));

            questions.add(new Question("Which one is not a method of JOptionPane class?",new String[]{"A. showInputDialog","B. showOptionDialog","C. showInformDialog","D. showMessageDialog"}, "C. showInformDialog"));

            

            

        }

 

        public int startQuiz(){

            int score=0;

            for (Question q : questions){

                displayQuestion(q);

                 if (group.getSelection().getActionCommand().equals(q.getCorrectAnswer()))

                 {

                    score++;

                }

            }

            dispose();

            return score;

        }

 

        private void displayQuestion(Question q){

            getContentPane().removeAll();

            for (Enumeration buttonGroup=group.getElements(); buttonGroup.hasMoreElements(); ){

                group.remove((AbstractButton)buttonGroup.nextElement());

            }

 

            JLabel questionText = new JLabel(q.getQuestion());

            getContentPane().add(questionText);

            for (String answer : q.getAnswers()){

                JRadioButton radio = new JRadioButton(answer);

                radio.setActionCommand(answer);

                group.add(radio);

                getContentPane().add(radio);

            }

            getContentPane().add(btnOk);

            pack();

            setVisible(true);

        }

 

    public static void main(String args[]) {

        Quiz quiz = new Quiz();

        int score = quiz.startQuiz();

 

        JOptionPane.showMessageDialog(null,"Your score: "+score,"",JOptionPane.INFORMATION_MESSAGE); 

    }

 

    class Question {

        String question="";

        String[] answers;

        String correctAnswer;

 

        public Question(String question, String[] possibleAnswers, String correctAnswer){

            this.question = question;

            this.answers = possibleAnswers;

            this.correctAnswer = correctAnswer;

        }

 

        public String getQuestion() {

            return question;

        }

 

        public String[] getAnswers() {

            return answers;

        }

 

        public String getCorrectAnswer() {

            return correctAnswer;

        }

    }

 

}

Edited by ZekeDragon, 12 February 2011 - 07:55 AM.
Please use [CODE] tags (the # button) when posting code.


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Quote

too many errors occur
The only error I see is with setModal. SetModal isn't defined in the JFrame class.
Example:
 setModal( true ); 

Quote

Is using array possible in JFrame for question and answer game?
Using an array structure to create a question/answer game is feasible.

I'd use a JFrame that displayed JDialogs one after another just like your program. It seems like you've properly completed the program though.

#3
an2kenz_143

an2kenz_143

    Newbie

  • Members
  • PipPip
  • 12 posts
wow.... thanks... how about.... in JDialog? is it feasible to use timer? i want my question and answer to have a timer...sample i have 1 mins time... in answering 1 question if the question not answered in time... the question will skip to next question....

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I would think so:
How to Use Swing Timers (The Javaâ„¢ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

#5
an2kenz_143

an2kenz_143

    Newbie

  • Members
  • PipPip
  • 12 posts
Another thanks for the tutorial.... arggg.... i cant convert my JDialog to JFrame... :) can you give me sample threads? how to convert... even if i delete the setModal... how about that.... this is my error if i extends my JFrame.... the group code error...


package Games;


import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.List;

import javax.swing.*;

 

public class Quiz extends JFrame {

        List<Question> questions = new ArrayList<Question>();

        int score=0;

        ButtonGroup group = new ButtonGroup();

        JButton btnOk = new JButton("Ok");

 

        public Quiz(){

            

            setSize(400,450);

            setLayout(null);

            setResizable(false);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

            setLocationRelativeTo(null);

            setVisible(true);

           

            

            setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));

           

            

            setLocationRelativeTo(null);

           

            ImageIcon imgBackG = new ImageIcon(getClass().getResource("awt.jpg"));

    	    JLabel lblBackG = new JLabel(imgBackG);

    	    lblBackG.setBounds(400, 320, 530, 300);

    	    add(lblBackG);

    	    setVisible(true);

    	    

            btnOk.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    setVisible(false);

                }

            });

 

            questions.add(new Question("What is Java (in regard to Computer Science) ?", new String[]{"A. Type of Cofee", "B. Object Oriented Programming,","C. Interactive Website","D. None of the Above"}, "B. Object Oriented Programming"));

            questions.add(new Question("What's the difference between an Applet and an application ?", new String[]{"A. An application is only available in Windows","B. Applet can paint words, application cannot","C. Applet are run over the web","D. None of the Above "}, "C. Applet are run over the web"));

            questions.add(new Question("What shortcut we use for importing objects in 'Eclipse' IDE?", new String[]{"A. CTRL + O"," B. ALT + O","C. CTRL + SHIFT + O","D. ALT + SHIFT + O"}, "C. CTRL + SHIFT + O"));

            questions.add(new Question("What shortcut we use to compile & run a source code in 'Eclipse' IDE?", new String[]{"A. ALT + SHIFT + X and J","B. CTRL + SHIFT + J and X","C. CTRL + ALT + J and Q","D.ALT + CTRL + Q"}, "A. ALT + SHIFT + X and J"));

            questions.add(new Question("What is the current version for 'JDK' Kit?", new String[]{"A. JDK 1.4 Update 1","B. JDK 1.5 Update 10","C. JDK 1.6 Update 9","D. JDK 1.6 Update 3"}, "D. JDK 1.6 Update 3"));

            questions.add(new Question("Which option is not an IDE 'Integrated Development Environment'?", new String[]{"A. Eclipse","B. JIDE","C. JCreator","D. NetBeans"}, "B. JIDE"));

            questions.add(new Question("Which option is not a feature of JAVA?", new String[]{"A. Cross Platform","B. Memory Management","C. Easy GUI Creation","D. Object - Oriented Programming"}, "B. Memory Management"));

            questions.add(new Question("What is the name of the component that we use to create dialog box?", new String[]{"A. JtextField","B. JOptionPane","C. JtextPanel","D. JTextArea"}, "B. JOptionPane"));

            questions.add(new Question("What should be the name of the class, if the file name is Hello.java?", new String[]{"A. _HeLLO","B. hello","C. HELLO","D. Hello"}, "D. Hello"));

            questions.add(new Question("Which one is not a method of JOptionPane class?",new String[]{"A. showInputDialog","B. showOptionDialog","C. showInformDialog","D. showMessageDialog"}, "C. showInformDialog"));

            

            

        }

 

        public int startQuiz(){

            int score=0;

            for (Question q : questions){

                displayQuestion(q);

                 if (group.getSelection().getActionCommand().equals(q.getCorrectAnswer()))

                 {

                    score++;

                }

            }

            dispose();

            return score;

        }

 

        private void displayQuestion(Question q){

            getContentPane().removeAll();

            for (Enumeration buttonGroup=group.getElements(); buttonGroup.hasMoreElements(); ){

                group.remove((AbstractButton)buttonGroup.nextElement());

            }

 

            JLabel questionText = new JLabel(q.getQuestion());

            getContentPane().add(questionText);

            for (String answer : q.getAnswers()){

                JRadioButton radio = new JRadioButton(answer);

                radio.setActionCommand(answer);

                group.add(radio);

                getContentPane().add(radio);

            }

            getContentPane().add(btnOk);

            pack();

            setVisible(true);

        }

 

    public static void main(String args[]) {

        Quiz quiz = new Quiz();

        int score = quiz.startQuiz();

 

        JOptionPane.showMessageDialog(null,"Your score: "+score,"",JOptionPane.INFORMATION_MESSAGE); 

    }

 

    class Question {

        String question="";

        String[] answers;

        String correctAnswer;

 

        public Question(String question, String[] possibleAnswers, String correctAnswer){

            this.question = question;

            this.answers = possibleAnswers;

            this.correctAnswer = correctAnswer;

        }

 

        public String getQuestion() {

            return question;

        }

 

        public String[] getAnswers() {

            return answers;

        }

 

        public String getCorrectAnswer() {

            return correctAnswer;

        }

    }

 

}

Edited by ZekeDragon, 13 February 2011 - 06:58 AM.
Please use [CODE] tags (the # button) when posting code.


#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Post your code in [ CODE][ /CODE] tags (press the # button)


public int startQuiz() {

        int score = 0;

        for (Question q : questions) {

            displayQuestion(q);

            if (group.getSelection().getActionCommand().equals(q.getCorrectAnswer())) {

                score++;

            }

        }

        dispose();

        return score;

    }

After displayQuestion(q) you immediately check if it's correct, before the user was able to do anything.
I assume you had the setModal to help you there, because the code would've waited, but now it doesn't.
So after displaying the question your code must stop.

startQuiz should look like:

public void startQuiz() {

        score = 0;

        nextQuestion();

    }


Your code will start again if the user presses 'OK', in the actionperformed you need some more code like so:

            public void actionPerformed(ActionEvent e) {

                setVisible(false);

                if (group.getSelection().getActionCommand().equals(questions.get(index-1).getCorrectAnswer())) {

                    score++;

                }

                if (!nextQuestion()) {

                    endQuiz();

                }

            }


(In my code i let nextQuestion return a boolean , true if there is a next question, false if there isn't.

#7
an2kenz_143

an2kenz_143

    Newbie

  • Members
  • PipPip
  • 12 posts
i get the logic.... but converting Q and A to JFrame... i dont get it with using 1 frame only... i think i must use multiple frame to set it...but i want to use 1 frame only and the questions and answers also next like in JDialog. In your code ? are extends your JFrame?? look at my first code... it's runnable ...and already done but using JDialog... logic in converting Jdialog to JFrame i dont get it .. can u trace the difference? :(

heres is my latest code as of now


package Games;


import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.List;

import javax.swing.*;

 

public class Quiz extends JFrame implements ActionListener{

        private static final int Question = 0;

		List<Question> questions = new ArrayList<Question>();

        int score=0;

        ButtonGroup group = new ButtonGroup();

        JButton btnOk = new JButton("Ok");

 

        public Quiz(){

            

            setSize(400,450);

            setLayout(null);

            setResizable(false);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

            setLocationRelativeTo(null);

            setVisible(true);

           

            

            setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));

           

            

            setLocationRelativeTo(null);

           

            ImageIcon imgBackG = new ImageIcon(getClass().getResource("awt.jpg"));

    	    JLabel lblBackG = new JLabel(imgBackG);

    	    lblBackG.setBounds(400, 320, 530, 300);

    	    add(lblBackG);

    	    setVisible(true);

    	    

            btnOk.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    setVisible(false);

                }

            });

 

            questions.add(new Question("What is Java (in regard to Computer Science) ?", new String[]{"A. Type of Cofee", "B. Object Oriented Programming,","C. Interactive Website","D. None of the Above"}, "B. Object Oriented Programming"));

            questions.add(new Question("What's the difference between an Applet and an application ?", new String[]{"A. An application is only available in Windows","B. Applet can paint words, application cannot","C. Applet are run over the web","D. None of the Above "}, "C. Applet are run over the web"));

            questions.add(new Question("What shortcut we use for importing objects in 'Eclipse' IDE?", new String[]{"A. CTRL + O"," B. ALT + O","C. CTRL + SHIFT + O","D. ALT + SHIFT + O"}, "C. CTRL + SHIFT + O"));

            questions.add(new Question("What shortcut we use to compile & run a source code in 'Eclipse' IDE?", new String[]{"A. ALT + SHIFT + X and J","B. CTRL + SHIFT + J and X","C. CTRL + ALT + J and Q","D.ALT + CTRL + Q"}, "A. ALT + SHIFT + X and J"));

            questions.add(new Question("What is the current version for 'JDK' Kit?", new String[]{"A. JDK 1.4 Update 1","B. JDK 1.5 Update 10","C. JDK 1.6 Update 9","D. JDK 1.6 Update 3"}, "D. JDK 1.6 Update 3"));

            questions.add(new Question("Which option is not an IDE 'Integrated Development Environment'?", new String[]{"A. Eclipse","B. JIDE","C. JCreator","D. NetBeans"}, "B. JIDE"));

            questions.add(new Question("Which option is not a feature of JAVA?", new String[]{"A. Cross Platform","B. Memory Management","C. Easy GUI Creation","D. Object - Oriented Programming"}, "B. Memory Management"));

            questions.add(new Question("What is the name of the component that we use to create dialog box?", new String[]{"A. JtextField","B. JOptionPane","C. JtextPanel","D. JTextArea"}, "B. JOptionPane"));

            questions.add(new Question("What should be the name of the class, if the file name is Hello.java?", new String[]{"A. _HeLLO","B. hello","C. HELLO","D. Hello"}, "D. Hello"));

            questions.add(new Question("Which one is not a method of JOptionPane class?",new String[]{"A. showInputDialog","B. showOptionDialog","C. showInformDialog","D. showMessageDialog"}, "C. showInformDialog"));

            

            

        }

 

        public int startQuiz() {

            int score = 0;

            nextQuestion();

            for (Question q : questions) {

                displayQuestion(q);

                if (group.getSelection().getActionCommand().equals(q.getCorrectAnswer())) {

                    score++;

                }

            }

            dispose();

            return score;

        }

        

       

        public void actionPerformed(ActionEvent e){

            setVisible(false);

            if (group.getSelection().getActionCommand().equals(questions.get(Question-1).getCorrectAnswer())) {

                score++;

            }

            if (!nextQuestion()) {

                endQuiz();

            }

        }

 

        private void endQuiz() 

        {

        	 JOptionPane.showMessageDialog(null,"Your score: "+score,"",JOptionPane.INFORMATION_MESSAGE); 

			

		}


		private boolean nextQuestion() {


			return true;

		}


		private void displayQuestion(Question q){

            getContentPane().removeAll();

            for (Enumeration buttonGroup=group.getElements(); buttonGroup.hasMoreElements(); ){

                group.remove((AbstractButton)buttonGroup.nextElement());

            }

 

            JLabel questionText = new JLabel(q.getQuestion());

            getContentPane().add(questionText);

            for (String answer : q.getAnswers())

            {

                JRadioButton radio = new JRadioButton(answer);

                radio.setActionCommand(answer);

                group.add(radio);

                getContentPane().add(radio);

            }

            getContentPane().add(btnOk);

            pack();

            setVisible(true);

        }

        

       

    public static void main(String args[]) {

        Quiz quiz = new Quiz();

        int score = quiz.startQuiz();

 

        JOptionPane.showMessageDialog(null,"Your score: "+score,"",JOptionPane.INFORMATION_MESSAGE); 

    }

 

    class Question {

        String question="";

        String[] answers;

        String correctAnswer;

 

        public Question(String question, String[] possibleAnswers, String correctAnswer){

            this.question = question;

            this.answers = possibleAnswers;

            this.correctAnswer = correctAnswer;

        }

 

        public String getQuestion() {

            return question;

        }

 

        public String[] getAnswers() {

            return answers;

        }

 

        public String getCorrectAnswer() {

            return correctAnswer;

        }

    }

 

}







heres my runnable Q and A using JDialog


package Games;


import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.List;

import javax.swing.*;

 

public class Quiz extends JDialog {

        List<Question> questions = new ArrayList<Question>();

        int score=0;

        ButtonGroup group = new ButtonGroup();

        JButton btnOk = new JButton("Ok");

 

        public Quiz(){

            super();

            setModal(true);

            setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));

            btnOk.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    setVisible(false);

                }

            });

 

            questions.add(new Question("What is Java (in regard to Computer Science) ?", new String[]{"A. Type of Cofee", "B. Object Oriented Programming,","C. Interactive Website","D. None of the Above"}, "B. Object Oriented Programming"));

            questions.add(new Question("What's the difference between an Applet and an application ?", new String[]{"A. An application is only available in Windows","B. Applet can paint words, application cannot","C. Applet are run over the web","D. None of the Above "}, "C. Applet are run over the web"));

            questions.add(new Question("What shortcut we use for importing objects in 'Eclipse' IDE?", new String[]{"A. CTRL + O"," B. ALT + O","C. CTRL + SHIFT + O","D. ALT + SHIFT + O"}, "C. CTRL + SHIFT + O"));

            questions.add(new Question("What shortcut we use to compile & run a source code in 'Eclipse' IDE?", new String[]{"A. ALT + SHIFT + X and J","B. CTRL + SHIFT + J and X","C. CTRL + ALT + J and Q","D.ALT + CTRL + Q"}, "A. ALT + SHIFT + X and J"));

            questions.add(new Question("What is the current version for 'JDK' Kit?", new String[]{"A. JDK 1.4 Update 1","B. JDK 1.5 Update 10","C. JDK 1.6 Update 9","D. JDK 1.6 Update 3"}, "D. JDK 1.6 Update 3"));

            questions.add(new Question("Which option is not an IDE 'Integrated Development Environment'?", new String[]{"A. Eclipse","B. JIDE","C. JCreator","D. NetBeans"}, "B. JIDE"));

            questions.add(new Question("Which option is not a feature of JAVA?", new String[]{"A. Cross Platform","B. Memory Management","C. Easy GUI Creation","D. Object - Oriented Programming"}, "B. Memory Management"));

            questions.add(new Question("What is the name of the component that we use to create dialog box?", new String[]{"A. JtextField","B. JOptionPane","C. JtextPanel","D. JTextArea"}, "B. JOptionPane"));

            questions.add(new Question("What should be the name of the class, if the file name is Hello.java?", new String[]{"A. _HeLLO","B. hello","C. HELLO","D. Hello"}, "D. Hello"));

            questions.add(new Question("Which one is not a method of JOptionPane class?",new String[]{"A. showInputDialog","B. showOptionDialog","C. showInformDialog","D. showMessageDialog"}, "C. showInformDialog"));

            

            

        }

 

        public int startQuiz(){

            int score=0;

            for (Question q : questions){

                displayQuestion(q);

                 if (group.getSelection().getActionCommand().equals(q.getCorrectAnswer()))

                 {

                    score++;

                }

            }

            dispose();

            return score;

        }

 

        private void displayQuestion(Question q){

            getContentPane().removeAll();

            for (Enumeration buttonGroup=group.getElements(); buttonGroup.hasMoreElements(); ){

                group.remove((AbstractButton)buttonGroup.nextElement());

            }

 

            JLabel questionText = new JLabel(q.getQuestion());

            getContentPane().add(questionText);

            for (String answer : q.getAnswers()){

                JRadioButton radio = new JRadioButton(answer);

                radio.setActionCommand(answer);

                group.add(radio);

                getContentPane().add(radio);

            }

            getContentPane().add(btnOk);

            pack();

            setVisible(true);

        }

 

    public static void main(String args[]) {

        Quiz quiz = new Quiz();

        int score = quiz.startQuiz();

 

        JOptionPane.showMessageDialog(null,"Your score: "+score,"",JOptionPane.INFORMATION_MESSAGE); 

    }

 

    class Question {

        String question="";

        String[] answers;

        String correctAnswer;

 

        public Question(String question, String[] possibleAnswers, String correctAnswer){

            this.question = question;

            this.answers = possibleAnswers;

            this.correctAnswer = correctAnswer;

        }

 

        public String getQuestion() {

            return question;

        }

 

        public String[] getAnswers() {

            return answers;

        }

 

        public String getCorrectAnswer() {

            return correctAnswer;

        }

    }

 

}







#8
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
If you only want to use 1 JFrame, then you'll need to:
  • Create JFrame
  • Create JRadioButtons/JButtons/JLabels/etc ( the question material )
  • Create a JPanel and add your #2 components to this JPanel
  • Set the contentPane of your Jframe to the JPanel from #3

When the user chooses their option, and hits the JButton to continue to the next question, you'll need to remove all of the contents from the contentPane of the JFrame.

(Currently, the JFrame should be only holding your JPanel[which holds your main content])

Then you'll remove all the components from your JPanel and add your new content/question to the jpanel.

Now add the newly modified JPanel back to your JFrame.

Methods you may need to help you:
Class: JPanel
  • add( ... );
  • setPreferredSize( new Dimension( ... ) );
  • removeAll( ... );
  • revalidate( ... );

Class: JFrame
  • setContentPane( ... );
  • getContentPane().removeAll();

Attached File  jframeHoldingJPanel.png   14.04K   53 downloads

#9
an2kenz_143

an2kenz_143

    Newbie

  • Members
  • PipPip
  • 12 posts
ok i'll try ur thread... i post if i finish my current problem.... thanks a lot

#10
an2kenz_143

an2kenz_143

    Newbie

  • Members
  • PipPip
  • 12 posts
ahmmmmm now i convert it... but multiple frames... i want help... for this program


package Games;


import javax.swing.*;


import java.awt.*;

import java.awt.event.*;


public class QandASample extends JWindow implements ActionListener {

	JWindow window = new JWindow();

	JFrame que1 = new JFrame();

	JFrame que2 = new JFrame();

	JFrame que3 = new JFrame();

	JFrame que4 = new JFrame();

	JFrame que5 = new JFrame();

	JFrame res1 = new JFrame();


	JLabel Q1 = new JLabel("1.What is Java (in regard to Computer Science)?");

	JLabel Q2 = new JLabel(

			"2.What's the difference between an Applet and an application ?");

	JLabel Q3 = new JLabel(

			"3.What shortcut we use for importing objects in 'Eclipse' IDE?");

	JLabel Q4 = new JLabel(

			"4.What shortcut we use to compile & run a source code in 'Eclipse' IDE?");

	JLabel Q5 = new JLabel("5.What is the current version for 'JDK' Kit?");

	JLabel label95 = new JLabel("");

	JLabel label96 = new JLabel("");

	JLabel label97 = new JLabel("");

	JLabel label98 = new JLabel("");

	JLabel label99 = new JLabel("");

	JLabel label934 = new JLabel("");

	JLabel TotalQuestion = new JLabel("");

	JLabel labelCorrect = new JLabel("");

	JLabel labelInCorrect = new JLabel("");

	JLabel TotalCorrect = new JLabel("");


	JRadioButton q1a = new JRadioButton("a. A type of Coffee");

	JRadioButton q1b = new JRadioButton("b. Object Oriented Programming");

	JRadioButton q1c = new JRadioButton("c. InterActive Website");

	JRadioButton q1d = new JRadioButton("d. None Of the Above ");


	JRadioButton q2a = new JRadioButton(

			"a. An application is only available on Windows");

	JRadioButton q2b = new JRadioButton(

			"b. Applets can paint words, applications cannot");

	JRadioButton q2c = new JRadioButton("c. Applets are run over the web");

	JRadioButton q2d = new JRadioButton("d. None Of the Above");


	JRadioButton q3a = new JRadioButton("a. CTRL + O");

	JRadioButton q3b = new JRadioButton("b. ALT + O");

	JRadioButton q3c = new JRadioButton("c. CTRL + SHIFT + O");

	JRadioButton q3d = new JRadioButton("d. ALT + SHIFT + O");


	JRadioButton q4a = new JRadioButton("a. ALT + SHIFT + X and J");

	JRadioButton q4b = new JRadioButton("b. CTRL + SHIFT + J and X");

	JRadioButton q4c = new JRadioButton("c. CTRL + ALT + J and Q");

	JRadioButton q4d = new JRadioButton("d. ALT + CTRL + Q");


	JRadioButton q5a = new JRadioButton("a. JDK 1.4 Update 1");

	JRadioButton q5b = new JRadioButton("b. JDK 1.5 Update 10");

	JRadioButton q5c = new JRadioButton("c. JDK 1.6 Update 9");

	JRadioButton q5d = new JRadioButton("d. JDK 1.6 Update 3");


	JButton q1button = new JButton("NEXT");

	JButton q2button = new JButton("NEXT");

	JButton q3button = new JButton("NEXT");

	JButton q4button = new JButton("NEXT");

	JButton q5button = new JButton("NEXT");

	JButton button3 = new JButton("Click to submit your score");

	ButtonGroup g = new ButtonGroup();


	public QandASample() {


		que1.setSize(600, 550);

		que1.setUndecorated(true);

		que1.setLayout(null);

		que1.setAlwaysOnTop(false);

		que1.setResizable(false);

		que1.setTitle("Question1");

		Q1.setBounds(200, 200, 500, 30);

		que1.add(Q1);

		q1a.setBounds(50, 300, 500, 30);

		que1.add(q1a);

		q1b.setBounds(50, 340, 500, 30);

		que1.add(q1b);

		q1c.setBounds(50, 380, 500, 30);

		que1.add(q1c);

		q1d.setBounds(50, 420, 500, 30);

		que1.add(q1d);

		q1button.setBounds(80, 460, 90, 30);

		que1.add(q1button);

		que1.setLocationRelativeTo(null);


		ImageIcon imgBackG = new ImageIcon(getClass().getResource("arg.jpg"));

		JLabel lblBackG = new JLabel(imgBackG);

		lblBackG.setBounds(0, 0, 600, 600);

		que1.add(lblBackG);

		que1.setVisible(true);


		q1a.addActionListener(this);

		q1b.addActionListener(this);

		q1c.addActionListener(this);

		q1button.addActionListener(this);


		g.add(q1a);

		g.add(q1b);

		g.add(q1c);

		g.add(q1d);


		que2.setUndecorated(true);

		que2.setSize(600, 550);

		que2.setLayout(null);

		que2.setResizable(false);

		que2.setTitle("Question2");

		Q2.setBounds(200, 200, 500, 30);

		que2.add(Q2);

		q2a.setBounds(50, 300, 500, 30);

		que2.add(q2a);

		q2b.setBounds(50, 340, 500, 30);

		que2.add(q2b);

		q2c.setBounds(50, 380, 500, 30);

		que2.add(q2c);

		q2d.setBounds(50, 420, 500, 30);

		que2.add(q2d);

		q2button.setBounds(80, 460, 90, 30);

		que2.add(q2button);

		que2.setLocationRelativeTo(null);


		ImageIcon imageq2 = new ImageIcon(getClass().getResource("arg.jpg"));

		JLabel lblBackq2 = new JLabel(imageq2);

		lblBackq2.setBounds(0, 0, 600, 600);

		que2.add(lblBackq2);

		que2.setVisible(false);


		q2a.addActionListener(this);

		q2b.addActionListener(this);

		q2c.addActionListener(this);

		q2d.addActionListener(this);

		q2button.addActionListener(this);


		g.add(q2a);

		g.add(q2b);

		g.add(q2c);

		g.add(q2d);


		que3.setUndecorated(true);

		que3.setSize(600, 550);

		que3.setLayout(null);

		que3.setResizable(false);

		que3.setTitle("Question 3");

		Q3.setBounds(200, 200, 500, 30);

		que3.add(Q3);

		q3a.setBounds(50, 300, 500, 30);

		que3.add(q3a);

		q3b.setBounds(50, 340, 500, 30);

		que3.add(q3b);

		q3c.setBounds(50, 380, 500, 30);

		que3.add(q3c);

		q3d.setBounds(50, 420, 500, 30);

		que3.add(q3d);

		q3button.setBounds(80, 460, 90, 30);

		que3.add(q3button);

		que3.setLocationRelativeTo(null);


		ImageIcon imageq3 = new ImageIcon(getClass().getResource("arg.jpg"));

		JLabel lblBackq3 = new JLabel(imageq3);

		lblBackq3.setBounds(0, 0, 600, 600);

		que3.add(lblBackq3);

		que3.setVisible(false);


		q3a.addActionListener(this);

		q3b.addActionListener(this);

		q3c.addActionListener(this);

		q3d.addActionListener(this);

		q3button.addActionListener(this);


		g.add(q3a);

		g.add(q3b);

		g.add(q3c);

		g.add(q3d);


		que4.setUndecorated(true);

		que4.setSize(600, 550);

		que4.setLayout(null);

		que4.setResizable(false);

		que4.setTitle("Question4");

		Q4.setBounds(200, 200, 500, 30);

		que4.add(Q4);

		q4a.setBounds(50, 300, 500, 30);

		que4.add(q4a);

		q4b.setBounds(50, 340, 500, 30);

		que4.add(q4b);

		q4c.setBounds(50, 380, 500, 30);

		que4.add(q4c);

		q4d.setBounds(50, 420, 500, 30);

		que4.add(q4d);

		q4button.setBounds(80, 460, 90, 30);

		que4.add(q4button);

		que4.setLocationRelativeTo(null);


		ImageIcon imageq4 = new ImageIcon(getClass().getResource("arg.jpg"));

		JLabel lblBackq4 = new JLabel(imageq4);

		lblBackq4.setBounds(0, 0, 600, 600);

		que4.add(lblBackq4);

		que4.setVisible(false);


		q4a.addActionListener(this);

		q4b.addActionListener(this);

		q4c.addActionListener(this);

		q4d.addActionListener(this);

		q4button.addActionListener(this);


		g.add(q4a);

		g.add(q4b);

		g.add(q4c);

		g.add(q4d);


		que5.setUndecorated(true);

		que5.setSize(600, 550);

		que5.setLayout(null);

		que5.setResizable(false);

		que5.setTitle("Question5");

		Q5.setBounds(200, 200, 500, 30);

		que5.add(Q5);

		q5a.setBounds(50, 300, 500, 30);

		que5.add(q5a);

		q5b.setBounds(50, 340, 500, 30);

		que5.add(q5b);

		q5c.setBounds(50, 380, 500, 30);

		que5.add(q5c);

		q5d.setBounds(50, 420, 500, 30);

		que5.add(q5d);

		q5button.setBounds(80, 460, 90, 30);

		que5.add(q5button);

		que5.setLocationRelativeTo(null);


		ImageIcon imageq5 = new ImageIcon(getClass().getResource("arg.jpg"));

		JLabel lblBackq5 = new JLabel(imageq5);

		lblBackq5.setBounds(0, 0, 600, 600);

		que5.add(lblBackq5);

		que5.setVisible(false);


		q5a.addActionListener(this);

		q5b.addActionListener(this);

		q5c.addActionListener(this);

		q5d.addActionListener(this);

		q5button.addActionListener(this);


		g.add(q5a);

		g.add(q5b);

		g.add(q5c);

		g.add(q5d);


		res1.setSize(600, 600);

		res1.setLayout(null);

		res1.setResizable(false);

		res1.setTitle("Result");

		label99.setBounds(20, 50, 500, 30);

		res1.add(label99);

		label98.setBounds(20, 100, 500, 30);

		res1.add(label98);

		label97.setBounds(20, 150, 500, 30);

		res1.add(label97);

		label95.setBounds(20, 200, 500, 30);

		res1.add(label95);

		TotalQuestion.setBounds(20, 250, 500, 30);

		res1.add(TotalQuestion);

		labelCorrect.setBounds(20, 300, 500, 30);

		res1.add(labelCorrect);

		labelInCorrect.setBounds(20, 350, 500, 30);

		res1.add(labelInCorrect);

		TotalCorrect.setBounds(20, 400, 500, 30);

		res1.add(TotalCorrect);

		label96.setBounds(20, 450, 500, 30);

		res1.add(label96);

		button3.setBounds(30, 500, 400, 30);

		res1.add(button3);

		res1.setLocationRelativeTo(null);


		ImageIcon imageresult = new ImageIcon(getClass().getResource("awt.jpg"));

		JLabel lblresult = new JLabel(imageresult);

		lblresult.setBounds(0, 0, 600, 600);

		res1.add(lblresult);


		que1.setVisible(false);


		button3.setActionCommand("button3");

		button3.addActionListener(this);

		que1.setVisible(true);

		que5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


	}


	public void actionPerformed(ActionEvent e) {

		Object source = e.getSource();


		int que1a = 0, que1b = 0, que1c = 0, que1d = 0;

		int que2a = 0, que2b = 0, que2c = 0, que2d = 0;

		int que3a = 0, que3b = 0, que3c = 0, que3d = 0;

		int que4a = 0, que4b = 0, que4c = 0, que4d = 0;

		int que5a = 0, que5b = 0, que5c = 0, que5d = 0;

		

	

		

		if (q1a.isSelected()) {

			

			que1a++;	

		}

		else if (q1b.isSelected()) {

			que1b++;

		}


		else if (q1c.isSelected()) {


			que1c++;


		} else if (q1d.isSelected()) {


			que1d++;


		}


		if (q2a.isSelected()) {

			que2a++;

		} else if (q2b.isSelected()) {


			que2b++;

		}


		else if (q2c.isSelected()) {


			que2c++;


		} else if (q2d.isSelected()) {


			que2d++;


		}


		if (q3a.isSelected()) {

			que3a++;

		} else if (q3b.isSelected()) {


			que3b++;

		}


		else if (q3c.isSelected()) {


			que3c++;


		} else if (q3d.isSelected()) {


			que3d++;


		}


		if (q4a.isSelected()) {

			que4a++;

		} else if (q4b.isSelected()) {


			que4b++;

		}


		else if (q4c.isSelected()) {


			que4c++;


		} else if (q4d.isSelected()) {


			que4d++;


		}


		if (q5a.isSelected()) {

			que5a++;

		} else if (q5b.isSelected()) {


			que5b++;

		}


		else if (q5c.isSelected()) {


			que5c++;


		} else if (q4d.isSelected()) {


			que5d++;


		}


		int totala = que1a + que2a + que3a + que4a + que5a;

		int totalb = que1b + que2b + que3b + que4b + que5b;

		int totalc = que1c + que2c + que3c + que4c + que5c;

		int totald = que1d + que2d + que3d + que4d + que5d;


		int totaltotal = totala + totalb + totalc + totald;


		if (source == q1button) {

			que1.setVisible(false);

			que2.setVisible(true);

			que3.setVisible(false);

			que4.setVisible(false);

			que5.setVisible(false);

		}


		if (source == q2button) {

			que1.setVisible(false);

			que2.setVisible(false);

			que3.setVisible(true);

			que4.setVisible(false);

			que5.setVisible(false);

		}

		if (source == q3button) {

			que1.setVisible(false);

			que2.setVisible(false);

			que3.setVisible(false);

			que4.setVisible(true);

			que5.setVisible(false);

			res1.setVisible(false);

		}

		if (source == q4button) {

			que1.setVisible(false);

			que2.setVisible(false);

			que3.setVisible(false);

			que4.setVisible(false);

			que5.setVisible(true);

			res1.setVisible(false);

		}


		if (source == q5button) {

			que1.setVisible(false);

			que2.setVisible(false);

			que3.setVisible(false);

			que4.setVisible(false);

			que5.setVisible(false);

			res1.setVisible(true);

		}

		label99.setText("Total number of As selected: " + totala);

		label98.setText("Total number of Bs selected: " + totalb);

		label97.setText("Total number of Cs selected: " + totalc);

		label95.setText("Total number of Ds selected: " + totald);

		TotalQuestion.setText("Total Question :  5" );

		labelCorrect.setText("Total number of Correct");

		labelInCorrect.setText("Total number of InCorrect");

		TotalCorrect.setText("Total Percent : ");


		if (source == button3) {

			label96.setText("Your total Score:" + totaltotal);

			label934.setText("" + totaltotal);


		}


	}


	public static void main(String[] args) {

		new QandASample();

	}

}



can u figure out how can i i declare if the chosen radio button is the correct answer? example a is the answer... how can i declare it to compute all my total? help in my thread

#11
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
The code you have provided is hard to work with.
With what you have, after every time the user presses "next", check to see if the correct answer is selected.
So the first time, when the user presses the "next" button, check to see if q1b.isSelected().
If it is, then they have one answer correct. ( I think b is the correct answer for #1 )
Then you can have an int correct, that keeps track of the number of correct answers.

Do this for each problem.

#12
an2kenz_143

an2kenz_143

    Newbie

  • Members
  • PipPip
  • 12 posts
ahmmmm thanks for the info... so supposed i group the choices.. i used checkbox instead of radio button... now i have a problem again... i declared now the correct answer but... the answer seems to be error.... with my JDialogue that i make... try to test my code.... to know my problem


package Games;


import javax.swing.*;


import java.awt.*;

import java.awt.event.*;


public class QandASample extends JWindow implements ItemListener {

	


	JWindow window = new JWindow();

	JFrame que1 = new JFrame();

	JFrame que2 = new JFrame();

	JFrame que3 = new JFrame();

	JFrame que4 = new JFrame();

	JFrame que5 = new JFrame();

	JFrame res1 = new JFrame();

	Timer timer;

	int count = 20 ;


	JLabel Q1 = new JLabel("1.What is Java (in regard to Computer Science)?");

	JLabel Q2 = new JLabel(

			"2.What's the difference between an Applet and an application ?");

	JLabel Q3 = new JLabel(

			"3.What shortcut we use for importing objects in 'Eclipse' IDE?");

	JLabel Q4 = new JLabel(

			"4.What shortcut we use to compile & run a source code in 'Eclipse' IDE?");

	JLabel Q5 = new JLabel("5.What is the current version for 'JDK' Kit?");

	JLabel label95 = new JLabel("");

	JLabel label96 = new JLabel("");

	JLabel label97 = new JLabel("");

	JLabel label98 = new JLabel("");

	JLabel label99 = new JLabel("");

	JLabel label934 = new JLabel("");

	JLabel TotalQuestion = new JLabel("");

	JLabel labelCorrect = new JLabel("");

	JLabel labelInCorrect = new JLabel("");

	JLabel TotalCorrect = new JLabel("");

	

	CheckboxGroup ques1Grp = new CheckboxGroup();

	Checkbox q1a = new Checkbox("a. A type of Coffee");

	Checkbox q1b = new Checkbox("b. Object Oriented Programming");

	Checkbox q1c = new Checkbox("c. InterActive Website");

	Checkbox q1d = new Checkbox("d. None Of the Above ");


	CheckboxGroup ques2Grp = new CheckboxGroup();

	Checkbox q2a = new Checkbox("a. An application is only available on Windows");

	Checkbox q2b = new Checkbox("b. Applets can paint words, applications cannot");

	Checkbox q2c = new Checkbox("c. Applets are run over the web");

	Checkbox q2d = new Checkbox("d. None Of the Above");

	

	CheckboxGroup ques3Grp = new CheckboxGroup();

	Checkbox q3a = new Checkbox("a. CTRL + O");

	Checkbox q3b = new Checkbox("b. ALT + O");

	Checkbox q3c = new Checkbox("c. CTRL + SHIFT + O");

	Checkbox q3d = new Checkbox("d. ALT + SHIFT + O");


	CheckboxGroup ques4Grp = new CheckboxGroup();

	Checkbox q4a = new Checkbox("a. ALT + SHIFT + X and J");

	Checkbox q4b = new Checkbox("b. CTRL + SHIFT + J and X");

	Checkbox q4c = new Checkbox("c. CTRL + ALT + J and Q");

	Checkbox q4d = new Checkbox("d. ALT + CTRL + Q");

	

	CheckboxGroup ques5Grp = new CheckboxGroup();

	Checkbox q5a = new Checkbox("a. JDK 1.4 Update 1");

	Checkbox q5b = new Checkbox("b. JDK 1.5 Update 10");

	Checkbox q5c = new Checkbox("c. JDK 1.6 Update 9");

	Checkbox q5d = new Checkbox("d. JDK 1.6 Update 3");


	

	JButton button3 = new JButton("Click to submit your score");

	

	


	public QandASample() {


		que1.setSize(600, 550);

		que1.setUndecorated(true);

		que1.setLayout(null);

		que1.setAlwaysOnTop(false);

		que1.setResizable(false);

		que1.setTitle("Question1");

		Q1.setBounds(200, 200, 500, 30);

		que1.add(Q1);

		q1a.setBounds(50, 300, 500, 30);

		que1.add(q1a);

		q1b.setBounds(50, 340, 500, 30);

		que1.add(q1b);

		q1c.setBounds(50, 380, 500, 30);

		que1.add(q1c);

		q1d.setBounds(50, 420, 500, 30);

		que1.add(q1d);

		

		que1.setLocationRelativeTo(null);


		ImageIcon imgBackG = new ImageIcon(getClass().getResource("iwww.jpg"));

		JLabel lblBackG = new JLabel(imgBackG);

		lblBackG.setBounds(0, 0, 600, 600);

		que1.add(lblBackG);

		que1.setVisible(true);


		q1a.addItemListener(this);

		q1b.addItemListener(this);

		q1c.addItemListener(this);

		q1d.addItemListener(this);


		


		que2.setUndecorated(true);

		que2.setSize(600, 550);

		que2.setLayout(null);

		que2.setResizable(false);

		que2.setTitle("Question2");

		Q2.setBounds(200, 200, 500, 30);

		que2.add(Q2);

		q2a.setBounds(50, 300, 500, 30);

		que2.add(q2a);

		q2b.setBounds(50, 340, 500, 30);

		que2.add(q2b);

		q2c.setBounds(50, 380, 500, 30);

		que2.add(q2c);

		q2d.setBounds(50, 420, 500, 30);

		que2.add(q2d);

		

		que2.setLocationRelativeTo(null);


		ImageIcon imageq2 = new ImageIcon(getClass().getResource("iwww.jpg"));

		JLabel lblBackq2 = new JLabel(imageq2);

		lblBackq2.setBounds(0, 0, 600, 600);

		que2.add(lblBackq2);

		que2.setVisible(false);


		q2a.addItemListener(this);

		q2b.addItemListener(this);

		q2c.addItemListener(this);

		q2d.addItemListener(this);

		


		


		que3.setUndecorated(true);

		que3.setSize(600, 550);

		que3.setLayout(null);

		que3.setResizable(false);

		que3.setTitle("Question 3");

		Q3.setBounds(200, 200, 500, 30);

		que3.add(Q3);

		q3a.setBounds(50, 300, 500, 30);

		que3.add(q3a);

		q3b.setBounds(50, 340, 500, 30);

		que3.add(q3b);

		q3c.setBounds(50, 380, 500, 30);

		que3.add(q3c);

		q3d.setBounds(50, 420, 500, 30);

		que3.add(q3d);

		que3.setLocationRelativeTo(null);


		ImageIcon imageq3 = new ImageIcon(getClass().getResource("iwww.jpg"));

		JLabel lblBackq3 = new JLabel(imageq3);

		lblBackq3.setBounds(0, 0, 600, 600);

		que3.add(lblBackq3);

		que3.setVisible(false);


		q3a.addItemListener(this);

		q3b.addItemListener(this);

		q3c.addItemListener(this);

		q3d.addItemListener(this);

		


		que4.setUndecorated(true);

		que4.setSize(600, 550);

		que4.setLayout(null);

		que4.setResizable(false);

		que4.setTitle("Question4");

		Q4.setBounds(200, 200, 500, 30);

		que4.add(Q4);

		q4a.setBounds(50, 300, 500, 30);

		que4.add(q4a);

		q4b.setBounds(50, 340, 500, 30);

		que4.add(q4b);

		q4c.setBounds(50, 380, 500, 30);

		que4.add(q4c);

		q4d.setBounds(50, 420, 500, 30);

		que4.add(q4d);

		

		que4.setLocationRelativeTo(null);


		ImageIcon imageq4 = new ImageIcon(getClass().getResource("iwww.jpg"));

		JLabel lblBackq4 = new JLabel(imageq4);

		lblBackq4.setBounds(0, 0, 600, 600);

		que4.add(lblBackq4);

		que4.setVisible(false);


		q4a.addItemListener(this);

		q4b.addItemListener(this);

		q4c.addItemListener(this);

		q4d.addItemListener(this);

		

		


		que5.setUndecorated(true);

		que5.setSize(600, 550);

		que5.setLayout(null);

		que5.setResizable(false);

		que5.setTitle("Question5");

		Q5.setBounds(200, 200, 500, 30);

		que5.add(Q5);

		q5a.setBounds(50, 300, 500, 30);

		que5.add(q5a);

		q5b.setBounds(50, 340, 500, 30);

		que5.add(q5b);

		q5c.setBounds(50, 380, 500, 30);

		que5.add(q5c);

		q5d.setBounds(50, 420, 500, 30);

		que5.add(q5d);

		

		que5.setLocationRelativeTo(null);


		ImageIcon imageq5 = new ImageIcon(getClass().getResource("iwww.jpg"));

		JLabel lblBackq5 = new JLabel(imageq5);

		lblBackq5.setBounds(0, 0, 600, 600);

		que5.add(lblBackq5);

		que5.setVisible(false);


		q5a.addItemListener(this);

		q5b.addItemListener(this);

		q5c.addItemListener(this);

		q5d.addItemListener(this);

		


		res1.setSize(600, 600);

		res1.setLayout(null);

		res1.setResizable(false);

		res1.setTitle("Result");

		label99.setBounds(20, 50, 500, 30);

		res1.add(label99);

		label98.setBounds(20, 100, 500, 30);

		res1.add(label98);

		label97.setBounds(20, 150, 500, 30);

		res1.add(label97);

		label95.setBounds(20, 200, 500, 30);

		res1.add(label95);

		TotalQuestion.setBounds(20, 250, 500, 30);

		res1.add(TotalQuestion);

		labelCorrect.setBounds(20, 300, 500, 30);

		res1.add(labelCorrect);

		labelInCorrect.setBounds(20, 350, 500, 30);

		res1.add(labelInCorrect);

		TotalCorrect.setBounds(20, 400, 500, 30);

		res1.add(TotalCorrect);

		label96.setBounds(20, 450, 500, 30);

		res1.add(label96);

		button3.setBounds(30, 500, 400, 30);

		res1.add(button3);

		res1.setLocationRelativeTo(null);


		ImageIcon imageresult = new ImageIcon(getClass().getResource("awt.jpg"));

		JLabel lblresult = new JLabel(imageresult);

		lblresult.setBounds(0, 0, 600, 600);

		res1.add(lblresult);


		que1.setVisible(false);

		que1.setVisible(true);

		que5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


	}


	 public void itemStateChanged(ItemEvent e) {

		int numberCorrect = 0;

		int score;

		

		

		Checkbox user1Selection = ques1Grp.getSelectedCheckbox();		

		if (user1Selection == q1b)

		{

			numberCorrect++;

			JOptionPane.showMessageDialog(null,"Correct");

			

		} else

			JOptionPane.showMessageDialog(null,"Sorry! The correct answer is b \n"); // if the

																		


		Checkbox user2Selection = ques2Grp.getSelectedCheckbox();

	

		if (user2Selection == q2b)

		{

			JOptionPane.showMessageDialog(null,"Correct");

			numberCorrect++; 

		} else

			JOptionPane.showMessageDialog(null,"Sorry! The correct answer is b \n"); 

																		


		Checkbox user3Selection = ques3Grp.getSelectedCheckbox();

		

		if (user3Selection == q3c)

		{

			JOptionPane.showMessageDialog(null,"Correct");

			numberCorrect++; 

		} else

			JOptionPane.showMessageDialog(null,"Sorry! The correct answer is a \n"); // if the

																		


		Checkbox user4Selection = ques4Grp.getSelectedCheckbox();

		

		if (user4Selection == q4c)

		{

			JOptionPane.showMessageDialog(null,"Correct");

											

			numberCorrect++;

		} else

			JOptionPane.showMessageDialog(null,"Sorry! The correct answer is c \n"); 


		Checkbox user5Selection = ques5Grp.getSelectedCheckbox();

		

		if (user5Selection == q5d)

		{

			JOptionPane.showMessageDialog(null,"Correct");

			numberCorrect++; // 

		} else

			System.out.println("Sorry! The correct answer is d \n"); 

		JOptionPane.showMessageDialog(null,"\n You had  " + numberCorrect

				+ " question correct.\n");

		score = (numberCorrect * 20);

		if (score >= 60) {

			

			JOptionPane.showMessageDialog(null,"Congratulations,you passed with a score of "

					+ score + "%\n");

		} else {

			System.out.println("Sorry, your grade was  " + score

					+ "% Better luck next time!");


			repaint();

		}

	}



		


	/*	if (source == q1button) {

				

			if (i > 1) {

				i = i + 1;

				

			}

			if (s > 1) {

				s = s + 1;

			}

			numbercorrect++;

			que1.setVisible(false);

			que2.setVisible(true);

			que3.setVisible(false);

			que4.setVisible(false);

			que5.setVisible(false);

		}


		if (source == q2button) {

			if (i > 1) {

				i = i + 1;

			}

			if (s > 1) {

				s = s + 1;

			}

			numbercorrect++;

			que1.setVisible(false);

			que2.setVisible(false);

			que3.setVisible(true);

			que4.setVisible(false);

			que5.setVisible(false);

		}

		if (source == q3button) {

			if (i > 1) {

				i = i + 1;

			}

			if (s > 1) {

				s = s + 1;

			}

			numbercorrect++;

			que1.setVisible(false);

			que2.setVisible(false);

			que3.setVisible(false);

			que4.setVisible(true);

			que5.setVisible(false);

			res1.setVisible(false);

		}

		if (source == q4button) {

			if (i > 1) {

				i = i + 1;

			}

			if (s > 1) {

				s = s + 1;

			}

			numbercorrect++;

			que1.setVisible(false);

			que2.setVisible(false);

			que3.setVisible(false);

			que4.setVisible(false);

			que5.setVisible(true);

			res1.setVisible(false);

		}


		if (source == q5button) {

			if (i > 1) {

				i = i + 1;

			}

			if (s > 1) {

				s = s + 1;

			}

			numbercorrect++;

			que1.setVisible(false);

			que2.setVisible(false);

			que3.setVisible(false);

			que4.setVisible(false);

			que5.setVisible(false);

			res1.setVisible(true);

			

			

		}*/

		

		/*int totala = que1a + que2a + que3a + que4a + que5a;

		int totalb = que1b + que2b + que3b + que4b + que5b;

		int totalc = que1c + que2c + que3c + que4c + que5c;

		int totald = que1d + que2d + que3d + que4d + que5d;

		int totaltotal = totala + totalb + totalc + totald;

		

		

		label99.setText("Total number of As selected: " + totala);

		label98.setText("Total number of Bs selected: " + totalb);

		label97.setText("Total number of Cs selected: " + totalc);

		label95.setText("Total number of Ds selected: " + totald);

		TotalQuestion.setText("Total Question :  5" );

		labelCorrect.setText("Total number of Correct : " + numbercorrect);

		labelInCorrect.setText("Total number of InCorrect");

		TotalCorrect.setText("Total Percent : ");

*/

		

	//	if (source == button3) {

			//label96.setText("Your total Score:" + totaltotal);

			//label934.setText("" + totaltotal);


		


	//}


	public static void main(String[] args) {

		new QandASample();

	}

}

	


thanks again




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users