Hey
Seem to be having a problem updating a JLabel updateScore() and don't seem to know why.
this is effecting my keeping score. I know it something small but for life of me can't figure it out.
Other JLabels have been up dated (see attachment for full programe)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JTextField;
import javax.swing.event.* ;
import java.net.*;
import java.io.*;
import java.awt.Color;
import java.util.Random;
/**
*
* @author Stephen
*/
public class NumberGame extends JFrame
{
JLabel sumQ,picture,score,life;
JPanel picpan,mainpan,playerpan;
protected int total, answeertotal,lives = 5,scores;
JTextField answeer;
private Container c;
MainPanel correction = new MainPanel();
ButtonHandler handler = new ButtonHandler();
NumberGame()
{
c = getContentPane();
c.setBackground(Color.white);
setSize(300,500);
setLocation(250,100);
c.setVisible(true);
c.setBackground(Color.PINK);
PicPanel pic = new PicPanel();
JPanel picpan = new JPanel();
picpan.add(pic);
MainPanel pane = new MainPanel();
JPanel mainpan = new JPanel();
mainpan.add(pane);
player status = new player();
JPanel playerpan = new JPanel();
playerpan.add(status);
c.add(picpan,BorderLayout.NORTH);
c.add(mainpan,BorderLayout.CENTER);
c.add(playerpan,BorderLayout.SOUTH);
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
player update = new player();
answeertotal = Integer.parseInt(answeer.getText( ));
if(answeertotal == total)
{
JOptionPane.showMessageDialog(null, "Correct","",JOptionPane.INFORMATION_MESSAGE);
correction.reset();
update.updateScore();
}
else
{
JOptionPane.showMessageDialog(null, "Sorry your wrong" + answeertotal + " "+ total ,"",JOptionPane.ERROR_MESSAGE);
update.updatelife();
}
}
}
class player extends JPanel
{
String lif,sco;
player()
{
int2str();
life = new JLabel(lif);
add(life);
score = new JLabel(sco);
add(score);
}
public void int2str()
{
lif = String.valueOf("Current lives : " + lives);
sco = String.valueOf("Current score : " + scores);
}
public void updateScore()
{
scores = scores + 10;
int2str();
score.setText(sco);
}
public void updatelife()
{
lives = lives - 1;
int2str();
life.setText(lif);
}
}
public static void main(String args[])
{
JFrame frame = new NumberGame();
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Thanks alot to anyone who takes time to have a look.