can anybody help me with my program tutorial??

my program is like a software tutorial for math,science,physics etc..
it starts with it starts with displaying a pdf file and after that displays 10 questions about the lesson..

the problem is after the first question is answered i need to click a jbutton to move to the next jpanel containing the second question..

i need a code that will show and hide jpanels...i think...:confused:

here's a sample picture:
http://i271.photobucket.com/albums/jj146/rude04/panel.jpg?t=1229954567

It is not such a difficult task.
Create a constructor something like this

Test(String question, String answerA, String answerB, String answerC, String correctAnswer)

Fill in data and put it in Array/ArrayList/Vector (something you familiar with).
Your frame view can be split into three panels CountPanel (showing current position or question taken in the test), QuestionPanel (build of label with question and check boxes in CheckboxGroup)and ButtonPanel (with button) on the first view you will show first data set for question one.
Now you have come up with simple logic that will on button press swap data in in the label and checkbox group of your QuestionPanel with new set, plus it will increment the count

uhmm..im just a newbie in java so i dont really understand some parts of your answer,if you dont mind could you give me a sample code for my prob..and thanks for the reply..^^

Lets make bargain.
You give me code what you have plus maybe how it should approached and I will try to improve it with you.
Do we have deal?

by the way this is the code that i've done so far..
what it does is when the button is clicked it will increment the score if the answer is correct.


now i need a code that will automatically replace the contents with the contents of the next question when the button is clicked,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class questionradionbutton extends JPanel implements ActionListener{
    private JLabel jcomp1;
    private JLabel jcomp2;
    private JButton jcomp3;
    private JLabel jcomp4;
    private JRadioButton jcomp5;
    private JRadioButton jcomp6;
    private JRadioButton jcomp7;
    private int score;


    public questionradionbutton() {
        //construct components
        JSeparator line1 = new JSeparator (JSeparator.HORIZONTAL);
        ButtonGroup group = new ButtonGroup();
        //int score;

        jcomp1 = new JLabel ("Question No. 1");
        jcomp2 = new JLabel ("1/10");
        jcomp3 = new JButton ("NEXT");
        jcomp4 = new JLabel ("Question number 1...");
        jcomp5 = new JRadioButton ("right answer");
        jcomp6 = new JRadioButton ("wrong answer");
        jcomp7 = new JRadioButton ("wrong answer");


        //adjust size and set layout
        setPreferredSize (new Dimension (661, 558));
        setLayout (null);


        //add components
        add (jcomp1);
        add (jcomp2);
        add (jcomp3);
        add (jcomp4);
        group.add (jcomp5);
        group.add (jcomp6);
        group.add (jcomp7);
        add (jcomp5);
		add (jcomp6);
        add (jcomp7);
        add (line1);



        //set component bounds (only needed by Absolute Positioning)
        jcomp1.setBounds (20, 20, 100, 25);
        jcomp2.setBounds (610, 20, 35, 20);
        jcomp3.setBounds (550, 515, 100, 25);
        jcomp4.setBounds (240, 90, 200, 25);
        jcomp5.setBounds (165, 270, 100, 25);
        jcomp6.setBounds (165, 330, 100, 25);
        jcomp7.setBounds (165, 390, 215, 25);




        jcomp3.addActionListener(this);

    }

    public void actionPerformed(ActionEvent r){
		if(jcomp5.isSelected()){
			score++;
			//move to next question
			
			}


		}


    public static void main (String[] args) {
        JFrame frame = new JFrame ("questionradionbutton");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new questionradionbutton());
        frame.pack();
        frame.setVisible (true);
        frame.setLocation (250,100);
        frame.setResizable(false);
    }
}

Lets make bargain.
You give me code what you have plus maybe how it should approached and I will try to improve it with you.
Do we have deal?

i've posted the code..THANKS!!again:)

The questions and the answers will be hard coded or read from file or database?

i dont reaaly know how to use a database yet
but if you mean by hard coded that all the contents from the first to the last question will be in a single program and very long code then yes..i would prefer to use hardcoded...

by the way i'm just just a 2nd yr college student so i dont have that much background in java programming..

Test class as suggested

public class Test
{
	private String question;
	private String optionA;
	private String optionB;
	private String optionC;
	private String answer;
	
	public Test(){}
	
	public Test(String quest, String oA, String oB, String oC, String ans)
	{
		setQuestion(quest);
		setOptionA(oA);
		setOptionB(oB);
		setOptionC(oC);
		setAnswer(ans);
	}
	
	private void setQuestion(String str){ question = str;}
	public String getQuestion(){ return question;}
	
	private void setOptionA(String str){ optionA = str;}
	public String getOptionA(){ return optionA;}
	
	private void setOptionB(String str){ optionB = str;}
	public String getOptionB(){ return optionB;}
	
	private void setOptionC(String str){ optionC = str;}
	public String getOptionC(){ return optionC;}
	
	private void setAnswer(String str){ answer = str;}
	public String getAnswer(){ return answer;}
}

Your re-done code, I made change to name( class name starts with capital letter any every other word in the name should start with capital too hence new class name SwingQuestionnaire) and also renamed some variables as to make it easier to work with it. Original names jcomp1 - jcomp6 are meaningless and difficult to remember which one is what

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class SwingQuestionnaire extends JPanel implements ActionListener{
    private JLabel titleLabel;
    private JLabel questionNumLabel;
    private JButton nextButton;
    private JLabel questionLabel;
    private JRadioButton jrbA;
    private JRadioButton jrbB;
    private JRadioButton jrbC;
    private int score;
    private int qNum = 0;
    private Test[] test = new Test[3];


    public SwingQuestionnaire() {
        //construct components
        JSeparator line1 = new JSeparator (JSeparator.HORIZONTAL);
        ButtonGroup group = new ButtonGroup();
        //int score;
        initializeTest();
        titleLabel = new JLabel ();
        questionNumLabel = new JLabel();
        
        questionLabel = new JLabel ();
        jrbA = new JRadioButton ();
        jrbB = new JRadioButton ();
        jrbC = new JRadioButton ();
        nextButton = new JButton ("NEXT");
        setTest();
        
        //adjust size and set layout
        setPreferredSize (new Dimension (661, 558));
        setLayout (null);


        //add components
        add (titleLabel);
        add (questionNumLabel);
        add (nextButton);
        add (questionLabel);
        group.add (jrbA);
        group.add (jrbB);
        group.add (jrbC);
        add (jrbA);
		add (jrbB);
        add (jrbC);
        add (line1);

        //set component bounds (only needed by Absolute Positioning)
        titleLabel.setBounds (20, 20, 100, 25);
        questionNumLabel.setBounds (610, 20, 35, 20);
        nextButton.setBounds (550, 515, 100, 25);
        questionLabel.setBounds (240, 90, 200, 25);
        jrbA.setBounds (165, 270, 100, 25);
        jrbB.setBounds (165, 330, 100, 25);
        jrbC.setBounds (165, 390, 215, 25);

        nextButton.addActionListener(this);              
    }

    public void actionPerformed(ActionEvent r){
    
	    if(r.getSource() == nextButton)
	    {
	    	validateAnswer();
	    	setTest();
	    }
    }


    public static void main (String[] args) {
        JFrame frame = new JFrame ("Questionnaire");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new SwingQuestionnaire());
        frame.pack();
        frame.setVisible (true);
        frame.setLocation (250,100);
        frame.setResizable(false);
    }
    
    private void setTest()
    {
    	titleLabel.setText("Question No. " + (qNum+1));
        questionNumLabel.setText((qNum+1)+"/3");
        
        questionLabel.setText(test[qNum].getQuestion());
        jrbA.setText(test[qNum].getOptionA());
        jrbB.setText(test[qNum].getOptionB());
        jrbC.setText(test[qNum].getOptionC());
        qNum++;
        if(qNum == 3)
        {
        	nextButton.setText("SUBMIT");
        }        
    }
    
    private void validateAnswer()
    {
    	//provide code for validation
    }
    
    private void initializeTest()
    {
    	test[0] = new Test("Question 1 to ask", "I like A", "I like B", "I like C", "I like B");
    	test[1] = new Test("Question 2 to ask", "I like A", "I like B", "I like C", "I like A");
    	test[2] = new Test("Question 3 to ask", "I like A", "I like B", "I like C", "I like C");
    }
}

Your job now is to provide/fill questions, answer options and "the" answer, plus implement validateAnswer() method

commented: Nice example. +16

WOW !!that was fast!..THANK YOU VERY MUCH!!!:icon_eek:
I'll review each part of the code..thanks peter_ budo:) :cool:

uhhm..i have another question..
whats the simpliest code to display a PDF file inside a JTextArea??

thanks Ezzaral!!^^

i really appreciate the code that you gave me but could you explain to me how this code works and what each part do??thanks..:)

private void setQuestion(String str){ question = str;}
	public String getQuestion(){ return question;}

Test class as suggested

public class Test
{
	private String question;
	private String optionA;
	private String optionB;
	private String optionC;
	private String answer;
	
	public Test(){}
	
	public Test(String quest, String oA, String oB, String oC, String ans)
	{
		setQuestion(quest);
		setOptionA(oA);
		setOptionB(oB);
		setOptionC(oC);
		setAnswer(ans);
	}
	
	private void setQuestion(String str){ question = str;}
	public String getQuestion(){ return question;}
	
	private void setOptionA(String str){ optionA = str;}
	public String getOptionA(){ return optionA;}
	
	private void setOptionB(String str){ optionB = str;}
	public String getOptionB(){ return optionB;}
	
	private void setOptionC(String str){ optionC = str;}
	public String getOptionC(){ return optionC;}
	
	private void setAnswer(String str){ answer = str;}
	public String getAnswer(){ return answer;}
}

These methods are setter and getter as they help you initialize and retrieve value from an Object to which they are related to, hence the prefix of each method

private void setQuestion(String str){ question = str;}
public String getQuestion(){ return question;}

depending on project requirement you will make setter private or public, where getter is usually public.

In original code there is call for initializeTest() method that looks like this

private void initializeTest()
    {
    	test[0] = new Test("Question 1 to ask", "I like A", "I like B", "I like C", "I like B");
    	test[1] = new Test("Question 2 to ask", "I like A", "I like B", "I like C", "I like A");
    	test[2] = new Test("Question 3 to ask", "I like A", "I like B", "I like C", "I like C");
    }

in this method with every call for new Test a new object of type Test is created and because arguments been provided newly created object is automatically initialized with these arguments. The initialization take place in the constructors like in our case in this one

public Test(String quest, String oA, String oB, String oC, String ans)
	{
		setQuestion(quest);
		setOptionA(oA);
		setOptionB(oB);
		setOptionC(oC);
		setAnswer(ans);
	}

where with the help of setter methods we initialize every variable of the Test object. If the setter method would be public instead of private we could do something like this

test[0] = new Test();
test[0].setQuestion("Question 1 to ask");
test[0].setOptionA("I like A");
test[0].setOptionB("I like B");
test[0].setOptionB("I like B");
test[0].setAnswer("I like B");

but this would be time consuming if we want to initialize more one object.

Getter method public String getQuestion(){ return question;} as you see return value of associated variable. These been used here

private void setTest()
    {
    	titleLabel.setText("Question No. " + (qNum+1));
        questionNumLabel.setText((qNum+1)+"/3");
        
        questionLabel.setText(test[qNum].getQuestion());
        jrbA.setText(test[qNum].getOptionA());
        jrbB.setText(test[qNum].getOptionB());
        jrbC.setText(test[qNum].getOptionC());
        qNum++;
        if(qNum == 3)
        {
        	nextButton.setText("SUBMIT");
        }        
    }

More about how classes works, members variable declaration, defining methods and more could be found here

I didn't want to use a code that i didn't understand..
thanks for the explanation peter_budo..:)

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.