View Single Post
  #3 (permalink)  
Old 02-03-2008, 10: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.
Reply With Quote