Results 1 to 3 of 3
  1. #1
    geork's Avatar
    geork is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default Problems with JLabel 2

    hi again!
    thanks for your last advice, but when i do:
    JLabel score = new JLabel((new Integer(num)).toString());
    int num = 5;
    it tells me "illegal forward reference"
    i will give you the whole program if it is soemthing to do with soemthing elswhere:
    import static java.lang.System.out;
    import javax.swing.*;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    class GameScreen1 extends JFrame implements ActionListener{
    JLabel label8 = new JLabel("hi! this is your score!");
    JLabel label9 = new JLabel("feel free to look at your score any time");
    JLabel score = new JLabel((new Integer(num)).toString());
    JButton l = new JButton("ok");
    int num = 5;
    public GameScreen1(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    setLayout(new FlowLayout()) ;
    add(label8);
    add(label9);
    add(score);
    score.setEnabled(false);
    add(l);
    l.addActionListener(this);
    pack();
    setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
    l.setEnabled(false);
    new GameChoose();
    }
    }
    my main method is in another class
    also, as a after note, how can you add 1 to score from another class whenit doesn't allow you to do ++ or +1 etc?
    i'd be very gratefull if soem1 could help me:)

  2. #2
    geork's Avatar
    geork is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default

    to be specific i typed:
    class Success extends GameScreen1 {
    String setText(){
    return score +1;
    }
    }
    and it said i wasn't allowed to use a simple +1, what should i do?

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default Solution

    Hello geork

    To use anything in Java, you must declare it before you use it. I modified your code and it works now:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.event.*;
    import java.awt.event.*;
    
    public class GameScreen1 extends JFrame implements ActionListener{
    	[B]protected int num = 5;[/B] // I moved this above the definition of the score JLabel.
    	protected JLabel label8 = new JLabel("hi! this is your score!");
    	protected JLabel label9 = new JLabel("feel free to look at your score any time");
    	protected JLabel score = new JLabel((new Integer(num)).toString());
    	protected JButton l = new JButton("ok");
    
    	public GameScreen1(){
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    		setLayout(new FlowLayout()) ;
    		add(label8);
    		add(label9);
    		add(score);
    		score.setEnabled(false);
    		add(l);
    		l.addActionListener(this);
    		pack();
    		setVisible(true);
    	}
    	public void actionPerformed(ActionEvent e) {
    		l.setEnabled(false);
    		// I removed the "new GameChoose();" since I don't have that class
    	}
    }
    And using the above code:
    Quote Originally Posted by geork
    to be specific i typed:
    Java Code:
    class Success extends GameScreen1 {
    	String setText(){
    		return score +1;
    	}
    }
    and it said i wasn't allowed to use a simple +1, what should i do?
    Score is a JLabel object and not an integer, so you cannot add integer values to it. However, num is an integer, change that and then update the JLabel. Also, a sub class cannot be less accessible than its parent.
    Java Code:
    [B]public[/B] class Success extends GameScreen1 {
    	public void incrementScore(){
    		this.num += 1;
    		updateLabel();
    	}
    	protected void updateLabel(){
    		score.setText(new Integer(num)).toString());
    	}
    }
    I'm not sure what you are trying to do, but I can help with the Java. :D Also, please use code tags when you post your code. :p To use code tags:
    [ CODE] //My code here [ /CODE], but without spaces in the tags. Please read the FAQ.

    I hope this helps. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. JLabel append?
    By Jononomous in forum New To Java
    Replies: 0
    Last Post: 04-07-2008, 08:41 PM
  2. JLabel .setActionCommand
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 03-28-2008, 05:16 AM
  3. How to use JLabel with html
    By leonard in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 05:43 PM
  4. JLabel
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 02:55 PM
  5. JLabel
    By Freddie in forum AWT / Swing
    Replies: 2
    Last Post: 05-29-2007, 03:19 PM

Posting Permissions

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