| | |
jpanel swicthing problem....
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
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...
here's a sample picture:
http://i271.photobucket.com/albums/j...g?t=1229954567
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...
here's a sample picture:
http://i271.photobucket.com/albums/j...g?t=1229954567
It is not such a difficult task.
Create a constructor something like this 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
Create a constructor something like this
Java Syntax (Toggle Plain Text)
Test(String question, String answerA, String answerB, String answerC, String correctAnswer)
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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?
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?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
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,
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,
Java Syntax (Toggle Plain Text)
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); } }
The questions and the answers will be hard coded or read from file or database?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jun 2008
Posts: 23
Reputation:
Solved Threads: 0
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..
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
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
Your job now is to provide/fill questions, answer options and "the" answer, plus implement validateAnswer() method
java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Other Threads in the Java Forum
- Previous Thread: Hi i am thinking about using java to write a Program for Cell phones
- Next Thread: Hi My dear friends, Pls help me
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character chat class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel julia learningresources lego linked linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online printf problem program programming project properties qt recursion researchinmotion rotatetext rsa scanner server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working






