Results 1 to 9 of 9
  1. #1
    MrSulfat is offline Member
    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0

    Post Need Help with Magic 8 ball game

    Hey guys.
    I have made a java magic 8 ball project, but im getting a error in my actionListener.
    My problem is that: The final local variable "svar" cannot be assigned, since it is defined in an enclosing.
    i think it this part.
    Java Code:
    final int svar = 0;
    		
    		button.addActionListener(new ActionListener(){ 
    			  public void actionPerformed(ActionEvent ae) {
    				  svar = (int)(Math.random()*3) +1;
    				  
    					System.out.println("her"+text.getText()+", "+svar);
    					label.setText("her"+text.getText()+", "+svar);
    					label.setVisible(true);
    here is all my code
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    
    
    public class Magic8 {
    
    	private final static int JA =1;
    	private final static int NEJ =2;
    	private final static int MÅSKE =3;
    	
    public static void main(String[] args)  {
    		
    		JFrame frame = new JFrame("Magic 8 Ball");
    		final JTextField text = new JTextField("type her");
    		JButton button = new JButton("Enter");
    		final JLabel label = new JLabel();
    		JPanel panel = new JPanel();
    		final int svar = 0;
    		
    		button.addActionListener(new ActionListener(){ 
    			  public void actionPerformed(ActionEvent ae) {
    				  svar = (int)(Math.random()*3) +1;
    				  
    					System.out.println("her"+text.getText()+", "+svar);
    					label.setText("her"+text.getText()+", "+svar);
    					label.setVisible(true);
    				
    				}
    		});
    
    		
    		
    		panel.add(text);
    		panel.add(button);
    		panel.add(label);
    		frame.add(panel);
    		frame.setSize(400, 400);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: Need Help with Magic 8 ball game

    Move svar outside of main and make it a non-final instance field. Then you need to create a final instance of Magic8 inside of main to access svar in the anonymous class.

    A better alternative might be to create your actionlistener inside of an instance method of Magic8 class instead of the static main entry point.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Need Help with Magic 8 ball game

    Hi MrSulfat,

    Your problem is you are trying to change the value of a variable declared as 'final'. Drop the word final from line 26 and it should work.

    EDIT: Ignore me and stick with Jim's comments.

    Regards.
    Last edited by Ronin; 03-15-2013 at 02:46 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Need Help with Magic 8 ball game

    Can I ask why svar is not simply local to the actionPerformed method?
    It doesn't seem to be used anywhere else...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: Need Help with Magic 8 ball game

    That of course would simplify matters. I was more focused on explaining how to access a non-final from an anonymous class than pointing out a better approach.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Need Help with Magic 8 ball game

    I am beginner at java but i believe that the problem was in line 26 in which you declare the final variable svar = 0 and then you try to change this variable.. i think that when you set a variable as a final the value of this variable can't changed so the solution may be this :

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
     
     
    public class Magic8 {
     
        private final static int JA =1;
        private final static int NEJ =2;
        private final static int MASKE =3;
         
    public static void main(String[] args)  {
             
           JFrame frame = new JFrame("Magic 8 Ball");
            final JTextField text = new JTextField("type her");
            JButton button = new JButton("Enter");
            final JLabel label = new JLabel();
    		JPanel panel = new JPanel();
    		final int svar = (int)(Math.random()*3) +1;
             
    			button.addActionListener(new ActionListener(){ 
    				public void actionPerformed(ActionEvent ae) {
    					
                      
    					System.out.println("her"+text.getText()+", "+svar);
    					label.setText("her"+text.getText()+", "+svar);
    					label.setVisible(true);
                     
                    }
            });
     
             
             
            panel.add(text);
            panel.add(button);
            panel.add(label);
            frame.add(panel);
            frame.setSize(400, 400);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Need Help with Magic 8 ball game

    Except then you would only ever have one result from the 8 ball, no matter how many times you pushed the button.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Need Help with Magic 8 ball game

    Oh.. you have right.. hmm..



    Edit: So this should work fine!

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener; 
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
      
      
      
    public class Magic8 {
        private final static int JA =1;
        private final static int NEJ =2;
        private final static int MASKE =3;
      
    public static void main(String[] args)  {
    	JFrame frame = new JFrame("Magic 8 Ball");
        final JTextField text = new JTextField("type her");
        JButton button = new JButton("Enter");
        final JLabel label = new JLabel();
        JPanel panel = new JPanel();
        button.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent ae) {
    		final int svar = (int)(Math.random()*3) +1;
    			System.out.println("her"+text.getText()+", "+svar);
                label.setText("her"+text.getText()+", "+svar);
                label.setVisible(true);
            }
        });
        panel.add(text);
        panel.add(button);
        panel.add(label);
        frame.add(panel);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        }
    }
    Last edited by ShadowWalker; 03-15-2013 at 04:05 PM. Reason: Correct!

  9. #9
    MrSulfat is offline Member
    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0

    Default Re: Need Help with Magic 8 ball game

    Thansk for all the replies, is really nice and i got it fix :)
    Mrsulfat

Similar Threads

  1. Need help with a third ball in game.
    By vlan in forum Java Applets
    Replies: 2
    Last Post: 05-30-2010, 03:37 PM
  2. [SOLVED] Magic Points Game
    By jimmy7 in forum Java Applets
    Replies: 6
    Last Post: 04-26-2009, 11:40 PM
  3. Problem using buttons to creat a magic square game
    By goldman in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 04:04 AM

Tags for this Thread

Posting Permissions

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