1

I have made this application:

image

For example when I click on the clear button when the counter JLabel (pointsAvailable) is 19 then the counter JLabel goes blank as expected, however when I start adding points again it starts from 19 not 40 as set on the start. I would like to make it to reset back to 40 instead of just making it blank

Code for the clear button

private void JButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
    speedPoints.setText("");
    attackPoints.setText("");
    defencePoints.setText("");
    powerPoints.setText("");
    agilityPoints.setText("");
    focusPoints.setText("");
    availablePoints.setText("");
}  

Code for Jlabel counter

    public class addingPointsUI extends javax.swing.JFrame {
int pointsAvailable=40;
        int speed=0;
        int power=0;
        int focus=0;
        int agility=0;
        int defence=0;
        int attack=0;

Code for buttons +/-: to allow me to add or decrease value "example power - button"

private void powerMinusActionPerformed(java.awt.event.ActionEvent evt) {                                           
    if (power > 0 ){
        if (pointsAvailable <= 0) {
    JOptionPane.showMessageDialog(null, "You are out of available points");
    return;
        }
        power = power - 1;

        pointsAvailable = pointsAvailable +1;

        availablePoints.setText(String.valueOf(pointsAvailable));

        powerPoints.setText(String.valueOf(power));

    }else {

            JOptionPane.showMessageDialog(null,"You cannot take anymore points from Power");
    }
}

Thank your for your kind replies.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Petr Cina
  • 21
  • 8

3 Answers3

1

Use a JSpinner with SpinnerNumberModel. Change the value of the model. The component will update and further changes will act on the current value of the model.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

I can quickly think of two solutions: 1. In the event handler of your clear button include the following:

private void JButtonActionPerformed(ActionEvent evt){
    ...
    pointsAvailable=40;
    speed=0;
    power=0;
    focus=0;
    agility=0;
    defence=0;
    attack=0;
}

This will reset all of your stats. 2. Or you can add an if statement to the listeners of every button that adds or subtracts a stat that will check if the specific statistic is empty. For example, for the speed buttons the code would look like so:

if (speedPoints.getText() == ""){
    pointsAvailable += speed;
    speed = 0;
}
  • Are you sure your code is correct? Solution 1 does nothing because the variables are in the speedPoints JLabel. Solution 2 gives me "comparing strings using == or !=" message. – Petr Cina May 16 '17 at 22:41
  • Make your variables global. When you have important variables of the type that are now they should be global and you can compare strings with String.compareTo(anotherString). – Jetmir Berisha May 16 '17 at 23:06
  • how to make variables global? – Petr Cina May 17 '17 at 13:47
0

Found my own solution on google:

private void ClearActionPerformed(java.awt.event.ActionEvent evt) {     
speedPoints.setText(String.valueOf(0));
powerPoints.setText(String.valueOf(0));
agilityPoints.setText(String.valueOf(0));
defencePoints.setText(String.valueOf(0));
focusPoints.setText(String.valueOf(0));
attackPoints.setText(String.valueOf(0));
availablePoints.setText(String.valueOf(40));
 }                                     

Works perfectly

Petr Cina
  • 21
  • 8