Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-03-2008, 07:39 PM
geork's Avatar
Member
 
Join Date: Jan 2008
Posts: 14
geork is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-03-2008, 08:41 PM
geork's Avatar
Member
 
Join Date: Jan 2008
Posts: 14
geork is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-03-2008, 09:40 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
Solution
Hello geork

To use anything in Java, you must declare it before you use it. I modified your code and it works now:
Code:
import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; public class GameScreen1 extends JFrame implements ActionListener{ protected int num = 5; // 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:
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.
Code:
public 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. Also, please use code tags when you post your code. To use code tags:
[ CODE] //My code here [ /CODE], but without spaces in the tags. Please read the FAQ.

I hope this helps.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JLabel append? Jononomous New To Java 0 04-07-2008 08:41 PM
JLabel .setActionCommand stevemcc AWT / Swing 1 03-28-2008 05:16 AM
How to use JLabel with html leonard AWT / Swing 1 08-06-2007 05:43 PM
JLabel Jack AWT / Swing 2 07-02-2007 02:55 PM
JLabel Freddie AWT / Swing 2 05-29-2007 03:19 PM


All times are GMT +3. The time now is 12:37 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org