943,769 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1332
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 22nd, 2008
0

jpanel swicthing problem....

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Light Poster
rude04 is offline Offline
26 posts
since Jun 2008
Dec 22nd, 2008
0

Re: jpanel swicthing problem....

It is not such a difficult task.
Create a constructor something like this
Java Syntax (Toggle Plain Text)
  1. 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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Dec 22nd, 2008
0

Re: jpanel swicthing problem....

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..^^
Reputation Points: 10
Solved Threads: 0
Light Poster
rude04 is offline Offline
26 posts
since Jun 2008
Dec 22nd, 2008
0

Re: jpanel swicthing problem....

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?
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Dec 22nd, 2008
0

Re: jpanel swicthing problem....

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,


Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5.  
  6. public class questionradionbutton extends JPanel implements ActionListener{
  7. private JLabel jcomp1;
  8. private JLabel jcomp2;
  9. private JButton jcomp3;
  10. private JLabel jcomp4;
  11. private JRadioButton jcomp5;
  12. private JRadioButton jcomp6;
  13. private JRadioButton jcomp7;
  14. private int score;
  15.  
  16.  
  17. public questionradionbutton() {
  18. //construct components
  19. JSeparator line1 = new JSeparator (JSeparator.HORIZONTAL);
  20. ButtonGroup group = new ButtonGroup();
  21. //int score;
  22.  
  23. jcomp1 = new JLabel ("Question No. 1");
  24. jcomp2 = new JLabel ("1/10");
  25. jcomp3 = new JButton ("NEXT");
  26. jcomp4 = new JLabel ("Question number 1...");
  27. jcomp5 = new JRadioButton ("right answer");
  28. jcomp6 = new JRadioButton ("wrong answer");
  29. jcomp7 = new JRadioButton ("wrong answer");
  30.  
  31.  
  32. //adjust size and set layout
  33. setPreferredSize (new Dimension (661, 558));
  34. setLayout (null);
  35.  
  36.  
  37. //add components
  38. add (jcomp1);
  39. add (jcomp2);
  40. add (jcomp3);
  41. add (jcomp4);
  42. group.add (jcomp5);
  43. group.add (jcomp6);
  44. group.add (jcomp7);
  45. add (jcomp5);
  46. add (jcomp6);
  47. add (jcomp7);
  48. add (line1);
  49.  
  50.  
  51.  
  52. //set component bounds (only needed by Absolute Positioning)
  53. jcomp1.setBounds (20, 20, 100, 25);
  54. jcomp2.setBounds (610, 20, 35, 20);
  55. jcomp3.setBounds (550, 515, 100, 25);
  56. jcomp4.setBounds (240, 90, 200, 25);
  57. jcomp5.setBounds (165, 270, 100, 25);
  58. jcomp6.setBounds (165, 330, 100, 25);
  59. jcomp7.setBounds (165, 390, 215, 25);
  60.  
  61.  
  62.  
  63.  
  64. jcomp3.addActionListener(this);
  65.  
  66. }
  67.  
  68. public void actionPerformed(ActionEvent r){
  69. if(jcomp5.isSelected()){
  70. score++;
  71. //move to next question
  72.  
  73. }
  74.  
  75.  
  76. }
  77.  
  78.  
  79. public static void main (String[] args) {
  80. JFrame frame = new JFrame ("questionradionbutton");
  81. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  82. frame.getContentPane().add (new questionradionbutton());
  83. frame.pack();
  84. frame.setVisible (true);
  85. frame.setLocation (250,100);
  86. frame.setResizable(false);
  87. }
  88. }
Reputation Points: 10
Solved Threads: 0
Light Poster
rude04 is offline Offline
26 posts
since Jun 2008
Dec 22nd, 2008
0

Re: jpanel swicthing problem....

Click to Expand / Collapse  Quote originally posted by peter_budo ...
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
Reputation Points: 10
Solved Threads: 0
Light Poster
rude04 is offline Offline
26 posts
since Jun 2008
Dec 22nd, 2008
0

Re: jpanel swicthing problem....

The questions and the answers will be hard coded or read from file or database?
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Dec 22nd, 2008
0

Re: jpanel swicthing problem....

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..
Reputation Points: 10
Solved Threads: 0
Light Poster
rude04 is offline Offline
26 posts
since Jun 2008
Dec 22nd, 2008
1

Re: jpanel swicthing problem....

Test class as suggested
java Syntax (Toggle Plain Text)
  1. public class Test
  2. {
  3. private String question;
  4. private String optionA;
  5. private String optionB;
  6. private String optionC;
  7. private String answer;
  8.  
  9. public Test(){}
  10.  
  11. public Test(String quest, String oA, String oB, String oC, String ans)
  12. {
  13. setQuestion(quest);
  14. setOptionA(oA);
  15. setOptionB(oB);
  16. setOptionC(oC);
  17. setAnswer(ans);
  18. }
  19.  
  20. private void setQuestion(String str){ question = str;}
  21. public String getQuestion(){ return question;}
  22.  
  23. private void setOptionA(String str){ optionA = str;}
  24. public String getOptionA(){ return optionA;}
  25.  
  26. private void setOptionB(String str){ optionB = str;}
  27. public String getOptionB(){ return optionB;}
  28.  
  29. private void setOptionC(String str){ optionC = str;}
  30. public String getOptionC(){ return optionC;}
  31.  
  32. private void setAnswer(String str){ answer = str;}
  33. public String getAnswer(){ return answer;}
  34. }

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)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5.  
  6. public class SwingQuestionnaire extends JPanel implements ActionListener{
  7. private JLabel titleLabel;
  8. private JLabel questionNumLabel;
  9. private JButton nextButton;
  10. private JLabel questionLabel;
  11. private JRadioButton jrbA;
  12. private JRadioButton jrbB;
  13. private JRadioButton jrbC;
  14. private int score;
  15. private int qNum = 0;
  16. private Test[] test = new Test[3];
  17.  
  18.  
  19. public SwingQuestionnaire() {
  20. //construct components
  21. JSeparator line1 = new JSeparator (JSeparator.HORIZONTAL);
  22. ButtonGroup group = new ButtonGroup();
  23. //int score;
  24. initializeTest();
  25. titleLabel = new JLabel ();
  26. questionNumLabel = new JLabel();
  27.  
  28. questionLabel = new JLabel ();
  29. jrbA = new JRadioButton ();
  30. jrbB = new JRadioButton ();
  31. jrbC = new JRadioButton ();
  32. nextButton = new JButton ("NEXT");
  33. setTest();
  34.  
  35. //adjust size and set layout
  36. setPreferredSize (new Dimension (661, 558));
  37. setLayout (null);
  38.  
  39.  
  40. //add components
  41. add (titleLabel);
  42. add (questionNumLabel);
  43. add (nextButton);
  44. add (questionLabel);
  45. group.add (jrbA);
  46. group.add (jrbB);
  47. group.add (jrbC);
  48. add (jrbA);
  49. add (jrbB);
  50. add (jrbC);
  51. add (line1);
  52.  
  53. //set component bounds (only needed by Absolute Positioning)
  54. titleLabel.setBounds (20, 20, 100, 25);
  55. questionNumLabel.setBounds (610, 20, 35, 20);
  56. nextButton.setBounds (550, 515, 100, 25);
  57. questionLabel.setBounds (240, 90, 200, 25);
  58. jrbA.setBounds (165, 270, 100, 25);
  59. jrbB.setBounds (165, 330, 100, 25);
  60. jrbC.setBounds (165, 390, 215, 25);
  61.  
  62. nextButton.addActionListener(this);
  63. }
  64.  
  65. public void actionPerformed(ActionEvent r){
  66.  
  67. if(r.getSource() == nextButton)
  68. {
  69. validateAnswer();
  70. setTest();
  71. }
  72. }
  73.  
  74.  
  75. public static void main (String[] args) {
  76. JFrame frame = new JFrame ("Questionnaire");
  77. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  78. frame.getContentPane().add (new SwingQuestionnaire());
  79. frame.pack();
  80. frame.setVisible (true);
  81. frame.setLocation (250,100);
  82. frame.setResizable(false);
  83. }
  84.  
  85. private void setTest()
  86. {
  87. titleLabel.setText("Question No. " + (qNum+1));
  88. questionNumLabel.setText((qNum+1)+"/3");
  89.  
  90. questionLabel.setText(test[qNum].getQuestion());
  91. jrbA.setText(test[qNum].getOptionA());
  92. jrbB.setText(test[qNum].getOptionB());
  93. jrbC.setText(test[qNum].getOptionC());
  94. qNum++;
  95. if(qNum == 3)
  96. {
  97. nextButton.setText("SUBMIT");
  98. }
  99. }
  100.  
  101. private void validateAnswer()
  102. {
  103. //provide code for validation
  104. }
  105.  
  106. private void initializeTest()
  107. {
  108. test[0] = new Test("Question 1 to ask", "I like A", "I like B", "I like C", "I like B");
  109. test[1] = new Test("Question 2 to ask", "I like A", "I like B", "I like C", "I like A");
  110. test[2] = new Test("Question 3 to ask", "I like A", "I like B", "I like C", "I like C");
  111. }
  112. }

Your job now is to provide/fill questions, answer options and "the" answer, plus implement validateAnswer() method
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Dec 22nd, 2008
0

Re: jpanel swicthing problem....

WOW !!that was fast!..THANK YOU VERY MUCH!!!
I'll review each part of the code..thanks peter_ budo
Reputation Points: 10
Solved Threads: 0
Light Poster
rude04 is offline Offline
26 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: JOptionPanes
Next Thread in Java Forum Timeline: Hi My dear friends, Pls help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC