Results 1 to 2 of 2
  1. #1
    soccerplayr is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Default Int not reading correctly for GUI

    Currently I am a student in college taking a computer science course (I am a CompSci major) and I am having trouble with one of my projects

    I am trying to get the program to display at the end of the program "You got " + right + " out of 10!", but I keep getting 0 out of 10. The Int right is not working properly. I have tried moving it around but I believe its because the centerLine3 that I used to make it, is in the constructer and not the other part of the program. I tried moving it as well but I just got some huge compile error, any ideas?

    Thanks,
    You can ignore the comment sections too

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.Random;
    import java.util.Scanner;
    
    public class Project8MainPanel extends JPanel
    {
    	final int TOTAL = 9;
    	private int n = 0;
    	private ImageIcon image1;
    	private JButton nextQuestion;
    	private JLabel label1, label2, leftLine1, centerLine1, centerLine2, centerLine3, rightLine1, mathQuestion;
    	private JTextField answerInput;
    	private Random numberGen = new Random();
    	private int right = 0;
    	private int correct = 0;
    
    
    	public Project8MainPanel()
    	{
    		addKeyListener(new KL());
    		
    		
    		//Make panel for left
    		Color myColor = new Color(0.3F, 1.0F, 0.3F);
    		JPanel leftPanel = new JPanel();
    		leftPanel.setPreferredSize(new Dimension (250,150));
    		leftPanel.setBackground(myColor);
    		leftLine1 = new JLabel("Your answer is correct!");
    		leftPanel.add(leftLine1);
    
    		//Correct image
    		image1 = new ImageIcon("Correct.jpg");
    		label1 = new JLabel ("", image1, SwingConstants.CENTER);
    		label1.setHorizontalTextPosition (SwingConstants.LEFT);
    		label1.setVerticalTextPosition (SwingConstants.BOTTOM);
    		
    		//Sets line and label to not display
    		leftLine1.setVisible(false);
    		label1.setVisible(false);
    
    		//Make panel for center
    		myColor = new Color(0.4F, 0.4F, 1.0F);
    		JPanel centerPanel = new JPanel();
    		centerPanel.setPreferredSize(new Dimension (250,150));
    		centerPanel.setBackground(myColor);
            centerLine1 = new JLabel("Lets play a Math Game!");
    	    centerLine2 = new JLabel("Here are your questions:");
    	    centerLine3 = new JLabel("You got " + right + " out of 10!");
    		mathQuestion = new JLabel("");
    
    		//nextQuestion JButton
    		nextQuestion = new JButton ("Next Question");
    		nextQuestion.addActionListener(new newQuestion());
    		nextQuestion.setVisible(false);
    
    		int x = numberGen.nextInt(20) + 1;
    		int y = numberGen.nextInt(20) + 1;
    		correct = x + y;
    		mathQuestion.setText(x + " + " + y + " = ");
    		
    		//Add lines into the centerPanel
    		centerPanel.add(centerLine1);
    		centerPanel.add(centerLine2);
    		centerPanel.add(centerLine3);
    		centerPanel.add(mathQuestion);
    		centerLine3.setVisible(false);
    		answerInput = new JTextField(5);
    		answerInput.addKeyListener(new KL());
    		centerPanel.add(answerInput);
    		centerPanel.add(nextQuestion);
    
    		//Make panel for right
    		myColor = new Color(1.0F, 0.3F, 0.3F);
    		JPanel rightPanel = new JPanel();
    		rightPanel.setPreferredSize(new Dimension (250,150));
    		rightPanel.setBackground(myColor);
    		rightLine1 = new JLabel("Your answer is wrong, sorry!");
    		rightPanel.add(rightLine1);
    		
    		//Wrong image
    		ImageIcon image2 = new ImageIcon("Wrong.jpg");
    		label2 = new JLabel ("", image2, SwingConstants.CENTER);
    		label2.setHorizontalTextPosition (SwingConstants.LEFT);
    		label2.setVerticalTextPosition (SwingConstants.BOTTOM);
    		
    		//Sets line and label to not display
    		rightLine1.setVisible(false);
    		label2.setVisible(false);
    
    		//Combine parts into main panel
    		leftPanel.add(label1);
    		rightPanel.add(label2);
    		setBackground(Color.black);
    		add(leftPanel);
    		add(centerPanel);
    		add(rightPanel);
    	}
    
    	//Lets the user use the Enter key to submit answer
    	private class KL implements KeyListener
    	{
    		public void keyPressed(KeyEvent e)
    		{
    			int key = e.getKeyCode();
    
    			if (key == KeyEvent.VK_ENTER)
    			{
    				if(!answerInput.getText().equals(""))
    				{
    					if(Integer.parseInt(answerInput.getText()) == correct)
    					{
    						right++;
    						leftLine1.setVisible(true);
    						label1.setVisible(true);					
    					}
    					else
    					{
    						rightLine1.setVisible(true);
    						label2.setVisible(true);
    					}
    				}
    				answerInput.setVisible(false);
    				nextQuestion.setVisible(true);
    
    			}
    		}
    
    		public void keyReleased(KeyEvent arg0)
    		{
    		}
    
    		public void keyTyped(KeyEvent arg0)
    		{
    		}
    	}
    	
    	//newQuestion JButton
    	private class newQuestion implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			Random numberGen = new Random();
    
    			//Sets everything to invisible
    			leftLine1.setVisible(false);
    			label1.setVisible(false);
    			rightLine1.setVisible(false);
    			label2.setVisible(false);
    			answerInput.setVisible(false);
    
    			//Displays end result if 10 questions are answered
    			if (n == TOTAL)
    			{
    				answerInput.setVisible(false);
    				centerLine3.setVisible(true);
    				mathQuestion.setVisible(false);
    				nextQuestion.setVisible(false);
    				
    			}			
    			
    			//Displays 10 questions
    			else
    			{
    				int x = numberGen.nextInt(20) + 1;
    				int y = numberGen.nextInt(20) + 1;
    				correct = x + y;
    				mathQuestion.setText(x + " + " + y + " = ");
    				answerInput.setVisible(true);
    				n++;
    			}
    		}
    	}
    }
    Last edited by soccerplayr; 04-26-2013 at 08:27 PM.

  2. #2
    sevoxx is offline Oracle Java Developer
    Join Date
    Apr 2013
    Location
    In a PineApple Under the Sea
    Posts
    20
    Rep Power
    0

    Default Re: Int not reading correctly for GUI

    Quote Originally Posted by soccerplayr View Post
    centerLine3 = new JLabel("You got " + right + " out of 10!");
    You have pre-defined what the JLabel is with the Integer 'right' already and are not updating the label at any time. The JLabel will not just magically update itself whenever you answer a question. You need to update it with the newly incremented integer value. So in simple terms, add the following after right++;

    Java Code:
    rightLine1.setText("You got " + right + " out of 10!");
    Edit:
    -----

    Or just set the label text at the end of the questions list before displaying it. Also, you do not need so many labels, just use a single label and update its text.
    Last edited by sevoxx; 04-28-2013 at 12:27 AM.

Similar Threads

  1. Help with If/Else not working correctly?
    By kalaiss in forum New To Java
    Replies: 8
    Last Post: 04-16-2012, 06:32 AM
  2. I can't get this to run correctly
    By LostinJavaLand in forum New To Java
    Replies: 4
    Last Post: 07-15-2010, 06:49 AM
  3. Did I do this THREAD correctly?
    By TimHuey in forum New To Java
    Replies: 4
    Last Post: 04-24-2010, 05:54 AM
  4. how to scale correctly ?
    By h9h in forum Java 2D
    Replies: 10
    Last Post: 10-29-2009, 07:06 AM
  5. Replies: 5
    Last Post: 08-21-2009, 11:03 AM

Posting Permissions

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