Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Fubarable

Thread: GUI help?

  1. #1
    tom2zip is offline Member
    Join Date
    Nov 2011
    Posts
    11
    Rep Power
    0

    Default GUI help?

    i'm currently making a quiz program, but i'm having problem with running this class
    i have no idea what's wrong with it, it was working fine a few days ago
    can anyone help?
    Java Code:
    public class PageQuestion extends JFrame
    {
    	private JPanel contentPane;
    	private JTextField txtQuestion;
    	public static List<PairMaker> pairs = PairMaker.pairs;
    	public static boolean doWordsOnly,doDefOnly,doPairs;
    	public static PageQuestion pageQuestion;
    	public static int score;
    	
    	public static void main(String[] args)
    	{
    		EventQueue.invokeLater(new Runnable()
    		{
    			public void run()
    			{
    				try
    				{
    					pageQuestion = new PageQuestion();
    					pageQuestion.setVisible(true);
    				}
    				catch (Exception e)
    				{
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    	
    	public PageQuestion() throws FileNotFoundException, IOException
    	{
    		Load.loading();
    		
    		
    		
    		final List<String> quizWordArr = new ArrayList<String>();
    		final List<String> quizDefArr = new ArrayList<String>();
    		List<Integer> tmpIndxArr = new ArrayList<Integer>();
    		//These rndmWordArr will be used for random-options
    		List<String> rndmWordArr = new ArrayList<String>();
    		List<String> rndmDefArr = new ArrayList<String>();
    		int currentIndx;
    		
    		Random rndmCorrect = new Random();
    		int indxCorrect;
    		
    		//Use this Array for creating a randomized quiz array
    		for(int a=0;a<=pairs.size()-1;a++)
    		{
    			tmpIndxArr.add(a);
    		}
    		Collections.shuffle(tmpIndxArr);
    		
    		//initializes words array and definition array
    		for(int a=0;a<=pairs.size()-1;a++)
    		{
    			quizWordArr.add(pairs.get(tmpIndxArr.get(a)).word);
    			quizDefArr.add(pairs.get(tmpIndxArr.get(a)).definition);
    		}
    		
    		//Will use one of them or both according to the choice the user makes
    		rndmWordArr = quizWordArr;
    		rndmDefArr = quizDefArr;
    		
    		//sets the property of the window
    		setTitle("Quiz");
    		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		setBounds(100, 100, 414, 241);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		//Sets a question to which the user have to answer
    		txtQuestion = new JTextField();
    		txtQuestion.setFont(new Font("Tahoma", Font.PLAIN, 18));
    		txtQuestion.setBounds(10, 39, 332, 28);
    		contentPane.add(txtQuestion);
    		txtQuestion.setColumns(10);
    		
    		//creates option-buttons
    		final ButtonGroup options = new ButtonGroup();
    		
    		//The first option
    		final JLabel lblOpt1 = new JLabel();
    		lblOpt1.setBounds(34, 84, 354, 23);
    		contentPane.add(lblOpt1);
    		final JRadioButton rbOpt1 = new JRadioButton("");
    		rbOpt1.setBounds(10, 84, 21, 23);
    		options.add(rbOpt1);
    		contentPane.add(rbOpt1);
    		
    		//The second option
    		final JLabel lblOpt2 = new JLabel();
    		lblOpt2.setBounds(34, 110, 354, 23);
    		contentPane.add(lblOpt2);
    		final JRadioButton rbOpt2 = new JRadioButton("");
    		rbOpt2.setBounds(10, 110, 21, 23);
    		options.add(rbOpt2);
    		contentPane.add(rbOpt2);
    		
    		//The third option
    		final JLabel lblOpt3 = new JLabel();
    		lblOpt3.setBounds(34, 136, 354, 23);
    		contentPane.add(lblOpt3);
    		final JRadioButton rbOpt3 = new JRadioButton("");
    		rbOpt3.setBounds(10, 136, 21, 23);
    		options.add(rbOpt3);
    		contentPane.add(rbOpt3);
    		
    		
    		//The fourth option
    		final JLabel lblOpt4 = new JLabel();
    		lblOpt4.setBounds(34, 162, 354, 23);
    		contentPane.add(lblOpt4);
    		
    		final JRadioButton rbOpt4 = new JRadioButton("");
    		rbOpt4.setBounds(10, 162, 21, 23);
    		options.add(rbOpt4);
    		contentPane.add(rbOpt4);
    		
    		//When clicked, the window will change to "Result" and show statistics on the quiz the user just have taken
    		JButton btnEnd = new JButton("End");
    		btnEnd.setBounds(299, 162, 89, 23);
    		contentPane.add(btnEnd);
    		btnEnd.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e)
    			{
    				try
    				{
    					setVisible(false);
    					new PageList().setVisible(true);
    				} catch (IOException e1)
    				{
    					e1.printStackTrace();
    				}
    			}
    		});
    		
    		while(!btnEnd.isSelected())
    		{
    			for(currentIndx=0;currentIndx<=pairs.size()-1;currentIndx++)
    			{
    				indxCorrect = rndmCorrect.nextInt(4);
    				final String correctAnswer = quizDefArr.get(currentIndx);
    				if(indxCorrect==0)
    				{
    					lblOpt1.setText(correctAnswer);
    					lblOpt2.setText(rndmDefArr.get(1));
    					lblOpt3.setText(rndmDefArr.get(2));
    					lblOpt4.setText(rndmDefArr.get(3));
    				}
    				else if(indxCorrect==1)
    				{
    					lblOpt1.setText(rndmDefArr.get(1));
    					lblOpt2.setText(correctAnswer);
    					lblOpt3.setText(rndmDefArr.get(2));
    					lblOpt4.setText(rndmDefArr.get(3));
    				}
    				else if(indxCorrect==2)
    				{
    					lblOpt1.setText(rndmDefArr.get(1));
    					lblOpt2.setText(rndmDefArr.get(2));
    					lblOpt3.setText(correctAnswer);
    					lblOpt4.setText(rndmDefArr.get(3));
    				}
    				else if(indxCorrect==3)
    				{
    					lblOpt1.setText(rndmDefArr.get(1));
    					lblOpt2.setText(rndmDefArr.get(2));
    					lblOpt3.setText(rndmDefArr.get(3));
    					lblOpt4.setText(correctAnswer);
    				}
    				Collections.shuffle(rndmDefArr);
    				
    				//Determines whether the option is correct or not and moves to the next page
    				rbOpt1.addActionListener(new ActionListener()
    				{
    					public void actionPerformed(ActionEvent e)
    					{
    						if(lblOpt1.getText()==correctAnswer)
    						{
    							score+=10;
    						}
    						else
    						{
    							score-=15;
    						}
    					}
    				});
    				
    				rbOpt2.addActionListener(new ActionListener()
    				{
    					public void actionPerformed(ActionEvent e)
    					{
    						if(lblOpt2.getText()==correctAnswer)
    						{
    							score+=10;
    						}
    						else
    						{
    							score-=15;
    						}
    					}
    				});
    				
    				rbOpt3.addActionListener(new ActionListener()
    				{
    					public void actionPerformed(ActionEvent e)
    					{
    						if(lblOpt3.getText()==correctAnswer)
    						{
    							score+=10;
    						}
    						else
    						{
    							score-=15;
    						}
    					}
    				});
    				
    				rbOpt4.addActionListener(new ActionListener()
    				{
    					public void actionPerformed(ActionEvent e)
    					{
    						if(lblOpt4.getText()==correctAnswer)
    						{
    							score+=10;
    						}
    						else
    						{
    							score-=15;
    						}
    					}
    				});
    			}
    			
    			if(currentIndx>=pairs.size()-1)
    			{
    				currentIndx=0;
    				Collections.shuffle(tmpIndxArr);
    			}
    		}
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: GUI help?

    A friendly suggestion: You may wish to reply to answers in your other threads before asking a new question.
    DarrylBurke likes this.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •