Results 1 to 4 of 4

Thread: Why doesn't JLabels runnable works

  1. #1
    Join Date
    Nov 2009
    Posts
    47

    Why doesn't JLabels runnable works

    I made this code to display JLabels "runnable" but that leaves me with lots of errors.

    Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    @SuppressWarnings("serial")
    public class Game extends JFrame implements MouseListener {
    	public JButton bar1 = new JButton("", new ImageIcon("bar1.gif"));
    	public JButton bar2 = new JButton("", new ImageIcon("bar2.gif"));
    	public JButton bar3 = new JButton("", new ImageIcon("bar3.gif"));
    	public JButton bar4 = new JButton("", new ImageIcon("bar4.gif"));
    	public JButton field1 = new JButton("", new ImageIcon("field.png"));
    	public JButton field2 = new JButton("", new ImageIcon("field.png"));
    	public JButton river = new JButton("", new ImageIcon("river.bmp"));
    	public JComboBox Menu1 = new JComboBox();
    	public int ClickX = 0;
    	public int ClickY =0;
    	public byte numberOfDoodeGreen=7;	
    	public byte numberOfDoodeBlue=8;
    	public byte numberOfDoodeRed=4;
    	public byte numberOfDoodeYellow=4;
    	public byte numberEnemies=75;
    	public int numberOfDoode = numberOfDoodeGreen + numberOfDoodeBlue + numberOfDoodeRed + numberOfDoodeYellow;
    	public short doowatt=500;
    	public short food=500; 
    	public short geleeBlack; 
    	public short numberEnemiesTues=0;
    	public String pseudo;
    	public Container content2;
    	public JLabel fond;
    	public Game (String title) {
    		setTitle (title);
    		Container content2 = getContentPane();	
    		content2.addMouseListener(this);
    		public JLabel BarGreen = new JLabel();
    		public JLabel BarBlue = new JLabel();
    		public JLabel BarRed = new JLabel();
    		public JLabel BarYellow = new JLabel();
    		public JLabel fond = new JLabel();
    		content2.setLayout(null);
    		Image image = Toolkit.getDefaultToolkit().createImage("map5.jpg");
    		fond.setIcon(new ImageIcon(image));
    		public void go(){
    		content2.add(BarGreen);
    		BarGreen.setText(""+numberOfDoodeGreen);
    		BarGreen.setBounds(98,2,20,20);
    		BarGreen.setForeground(Color.white); 
    		content2.add(BarBlue);
    		BarBlue.setText(""+numberOfDoodeBlue);
    		BarBlue.setBounds(292,2,20,20);
    		BarBlue.setForeground(Color.white);
    		content2.add(BarRed);
    		BarRed.setText(""+numberOfDoodeRed);
    		BarRed.setBounds(492,2,20,20);
    		BarRed.setForeground(Color.white);
    		content2.add(BarYellow);
    		BarYellow.setText(""+numberOfDoodeYellow);
    		BarYellow.setBounds(676,2,20,20);
    		BarYellow.setForeground(Color.white);
    }
    		content2.add(bar1);
    		bar1.setBounds(5,5,190,55);
    		bar1.setFocusPainted(false);
    		bar1.setBorderPainted(false);
    		bar1.setContentAreaFilled(false);
    		bar1.addMouseListener(this); 
     		content2.add(bar2);
    		bar2.setBounds(187,5,190,55);
    		bar2.setFocusPainted(false);
    		bar2.setBorderPainted(false);
    		bar2.setContentAreaFilled(false);
    		bar2.addMouseListener(this);
    		content2.add(bar3);
    		bar3.setBounds(372,5,190,55);
    		bar3.setFocusPainted(false);
    		bar3.setBorderPainted(false); 
    		bar3.setContentAreaFilled(false);
    		bar3.addMouseListener(this);
    		content2.add(bar4);
    		bar4.setBounds(557,5,190,55);
    		bar4.setFocusPainted(false);
    		bar4.setBorderPainted(false);
    		bar4.setContentAreaFilled(false);
    		bar4.addMouseListener(this);
    		content2.add(field1);
    		field1.setBounds(1000,500,190,55);
    		field1.setFocusPainted(false);
    		field1.setBorderPainted(false);
    		field1.setContentAreaFilled(false);
    		field1.addMouseListener(this);
    		content2.add(field2);
    		field2.setBounds(250,800,190,55);
    		field2.setFocusPainted(false);
    		field2.setBorderPainted(false); 
    		field2.setContentAreaFilled(false);
    		field2.addMouseListener(this);
    		content2.add(river);
    		river.setBounds(0,0,190,55);
    		river.setFocusPainted(false);
    		river.setBorderPainted(false);
    		river.setContentAreaFilled(false);
    		river.addMouseListener(this);
    		content2.add(fond);
    		fond.setBounds(0, 0, 1280, 1024);
    		setSize(1280,1024);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		setVisible(true);
    	}
    	public void mouseClicked(MouseEvent e) {	
    		Object source = e.getSource();	
    		if (source != bar1){
    			ClickX = e.getX();
    			ClickY = e.getY();
    			System.out.print("\n the variable is Ă*:"+ClickX+" and to "+ClickY);
    			numberOfDoodeRed++;
    		}
    	}
    	public void mouseEntered(MouseEvent arg0) {
    	}
    	public void mouseExited(MouseEvent arg0) {
    	}
    	public void mousePressed(MouseEvent Pressevt) {
    	}
    	public void mouseReleased(MouseEvent arg0) {
    	}
    	class PlayAnimation implements Runnable{
            @Override
            public void run() {
                    go();          
    }
    errors:

    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
    Duplicate field Game.fond
    Syntax error, insert "}" to complete MethodBody
    Duplicate field Game.fond
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "null", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "setIcon", = expected after this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "bar1", VariableDeclaratorId expected after this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on tokens, delete these tokens
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "this", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "bar2", VariableDeclaratorId expected after this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on tokens, delete these tokens
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "this", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "bar3", VariableDeclaratorId expected after this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on tokens, delete these tokens
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "this", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "bar4", VariableDeclaratorId expected after this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on tokens, delete these tokens
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "this", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "field1", VariableDeclaratorId expected after this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on tokens, delete these tokens
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "this", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "field2", VariableDeclaratorId expected after this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on tokens, delete these tokens
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "this", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "river", VariableDeclaratorId expected after this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on tokens, delete these tokens
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "false", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "this", delete this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on token "fond", VariableDeclaratorId expected after this token
    Syntax error on token(s), misplaced construct(s)
    Syntax error on tokens, delete these tokens
    Syntax error on token(s), misplaced construct(s)
    Syntax error on tokens, AnnotationName expected instead
    Syntax error on token ";", @ expected
    Syntax error on token ";", @ expected
    Syntax error on token "}", delete this token

    at Game.<init>(Game.java:46)
    at Menu.actionPerformed(Menu.java:83)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Why doesn't JLabels runnable works

    I too tried but given the error code your Java is not java. Did you used an IDE? Finally, if you miss it and braces may be another component of syntax.

  3. #3
    Join Date
    Nov 2009
    Posts
    47

    Re: Why doesn't JLabels runnable works

    No, actually my method was in my constructor, and it hangs. I repeated the trick, and I do know not why, but my JPanel "numberOfDoodeGreen does not gets displayed ..

    Code:
    ................
    class PlayAnimation implements Runnable
    {
    	public void go()
    	{
    		BarGreen.setText(""+numberOfDoodeGreen);
    		go();
    	}
    	@Override
    	public void run() 
    	{
    		go();      
    	}
    }

  4. #4
    Join Date
    Nov 2008
    Posts
    1,221

    Re: Why doesn't JLabels runnable works

    The problem is infinite recursive call!
    Code:
    public void go(){
      BarGreen.setText(""+numberOfDoodeGreen);
      go();
    }
     
    @Override
    public void run() {
      go();   
    }
    This is not how we do animations. No recursive call! This will eat up all memory on the computer. It is iterative loops. Let the time the computer has to do something else and then take breaks. Something like:
    Code:
    public void go(){
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
           BarGreen.setText(""+numberOfDoodeGreen);
         }
      }
    }
     
    @Override
    public void run() {
      try {
        while (true) {
          go();
          Thread.sleep(500);
        }
      }
      catch (InterruptedException ie) {
      }
    }
    But that's if your ever Runnable PlayAnimation is a start in another Thread. Otherwise it is also possible to run in the EDT using a Swing Timer (javax.swing.Timer) who has what it takes to be re-called at regular intervals.

Similar Threads

  1. Replies: 5
    Last Post: 26-08-2011, 10:31 PM
  2. Sony X2- sms doesn't works anymore
    By Tajdar7 in forum Portable Devices
    Replies: 3
    Last Post: 01-10-2010, 11:57 AM
  3. Right click doesn't works in firefox 3.5
    By spectre330 in forum Technology & Internet
    Replies: 3
    Last Post: 10-08-2009, 07:58 PM
  4. Replies: 2
    Last Post: 21-02-2009, 12:01 AM
  5. New Mouse doesn't works
    By urgaffel in forum Hardware Peripherals
    Replies: 4
    Last Post: 22-10-2008, 07:53 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,642,554,203.20789 seconds with 17 queries