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

    Default problem with JLabels

    hi!
    this is my first time writing, and i need some help.
    i want to be able to make a JLabel in which i can store a value (such as int) which i can add one or subtract one from. it doesn't seem to like:
    JLabel score = new JLabel(num);
    int num = 5;
    i probabely have done soemthing really stupid or missed soemthing out but hey! i'm new to java.
    if you can help me: thank you so much, because i am really confused. :confused:
    cyas

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

    Default Welcome!

    Welcome, geork, to Java Forums!

    First, you must declare the integer, num, and then you can use it like this:
    Java Code:
    int num = 10;
    JLabel label = new JLabel(([COLOR="RoyalBlue"]new Integer(num)[/COLOR]).[COLOR="Teal"]toString()[/COLOR]);
    I do this, because this construction is expecting an argument of type String. To convert the number into a String, you can create a new Integer object and then use its toString() method.

    Hope this helped. :D
    Last edited by tim; 01-31-2008 at 07:43 PM. Reason: For some reason I keep typing "the" instead of "to" :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Also,
    Java Code:
    new JLabel(String.valueOf(num));

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

Similar Threads

  1. Having Trouble Aligning JLabels
    By Mark_Petrov in forum AWT / Swing
    Replies: 0
    Last Post: 01-20-2008, 05:22 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
  •