• 0

Netbeans Reset Button


Question

I'm creating a simple game in Netbeans but I've come across a problem with my reset button which when I click on it, it only clear the text in the label but doesn't add it back to Total Coins?

 

My code for the reset button,

private void ResetActionPerformed(java.awt.event.ActionEvent evt) {                                         
            coins.setText("");

ex136c6.png

Link to comment
https://www.neowin.net/forum/topic/1257698-netbeans-reset-button/
Share on other sites

17 answers to this question

Recommended Posts

  • 0

If the code you've posted is the whole function (you're missing at least the closing bracket), then it's only clearing the label and doing nothing more because you have not told it to do any more. Add some additional statements into this function to get it to do what you want it to do!

  • 0
  On 25/05/2015 at 14:45, theblazingangel said:

If the code you've posted is the whole function (you're missing at least the closing bracket), then it's only clearing the label and doing nothing more because you have not told it to do any more. Add some additional statements into this function to get it to do what you want it to do!

I do have a closing bracket in the code, just that I didn't copy it properly :blush:, I'm not sure on additional statements code as I'm a beginner and only followed this guide for the reset button

  • 0

If you don't understand what a statement is (essentially a line of code), then you need to find yourself an introductory book (or website) from which to teach yourself basic programming. A Java book from the "Sams Teach Yourself" line of introductory programming books might be a good place to start.

  • 0
  On 25/05/2015 at 16:39, theblazingangel said:

If you don't understand what a statement is (essentially a line of code), then you need to find yourself an introductory book (or website) from which to teach yourself basic programming. A Java book from the "Sams Teach Yourself" line of introductory programming books might be a good place to start.

I know what a statement is but I think the code I need is to reset or update the total coins back to 500 when I click on the Reset button

  • 0

You need the program to know what the initial values of total coins should be.

Perhaps a constant or a variable, lets call it TotalCoins.

You should then have this function reset your current coins variable;

Also, reseting the Total Coins label.

Something in the order of:

CurrentCoins = TotalCoins;
TotalCoinsLabel.text = CurrentCoins;

BTW, Posting the whole code you are using would help us give you a more complete answer.

  • 0
  On 25/05/2015 at 21:15, Litherz said:

I know what a statement is but I think the code I need is to reset or update the total coins back to 500 when I click on the Reset button

 

Okay. Well it's a little difficult to advise on exactly what you need to do without having seen the rest of your code.

 

Do your add and subtract buttons work yet? You surely have an integer variable somewhere in your code, accessible to multiple functions, holding the current total coins value, and another integer variable holding the value currently being payed with (that '5' in your example screenshot). When your 'Add' button is clicked, the event handler for that button should (I guess) add the current value to the total (TotalCoins += CurrentCoins), and update the displayed total coins value (total_coins.SetText(String.valueOf(TotalCoins))), along with whatever else needs doing in your game (move on to next picture or whatever).

 

The action handler for the reset button should reset the value of the TotalCoins integer to some value, e.g. TotalCoins = 500, and update the displayed text (total_coins.SetText(String.valueOf(TotalCoins))), along with whatever else it needs to do (e.g. display first game picture again).

  • 0
  On 25/05/2015 at 21:53, lj300 said:

You need the program to know what the initial values of total coins should be.

Perhaps a constant or a variable, lets call it TotalCoins.

You should then have this function reset your current coins variable;

Also, reseting the Total Coins label.

Something in the order of:

CurrentCoins = TotalCoins;
TotalCoinsLabel.text = CurrentCoins;

BTW, Posting the whole code you are using would help us give you a more complete answer.

 

 

  On 25/05/2015 at 23:29, theblazingangel said:

Okay. Well it's a little difficult to advise on exactly what you need to do without having seen the rest of your code.

 

Do your add and subtract buttons work yet? You surely have an integer variable somewhere in your code, accessible to multiple functions, holding the current total coins value, and another integer variable holding the value currently being payed with (that '5' in your example screenshot). When your 'Add' button is clicked, the event handler for that button should (I guess) add the current value to the total (TotalCoins += CurrentCoins), and update the displayed total coins value (total_coins.SetText(String.valueOf(TotalCoins))), along with whatever else needs doing in your game (move on to next picture or whatever).

 

The action handler for the reset button should reset the value of the TotalCoins integer to some value, e.g. TotalCoins = 500, and update the displayed text (total_coins.SetText(String.valueOf(TotalCoins))), along with whatever else it needs to do (e.g. display first game picture again).

Sorry about that, this is my code,

 

ex4d21e.jpg

 

  • 0

You will need to set the value of totalCoins back to 500 in the event handler of the refresh button. Then you need to update the component which displays the value to reflect this change. You have done this already in the other two event handlers.

 

Or better than that try to avoid code repetition (which you are already suffering from) and make a method which refreshes all of the GUI components which their current backing values stored in the corresponding variables. Then just call that new method each time you make an update to any value.

  • 0
  On 26/05/2015 at 10:10, Mulrian said:

You will need to set the value of totalCoins back to 500 in the event handler of the refresh button. Then you need to update the component which displays the value to reflect this change. You have done this already in the other two event handlers.

 

Or better than that try to avoid code repetition (which you are already suffering from) and make a method which refreshes all of the GUI components which their current backing values stored in the corresponding variables. Then just call that new method each time you make an update to any value.

Thanks for the quick reply, but I don't know how to reset the value back to 500 after I click on the reset button as well as to update the components, can you help me please

 

I have tried declaring int CurrentCoins = TotalCoins; and lblpoints.setText(String.valueOf(totalcoins)); in the button which does reset the total coins back to 500 however when I click on the add button, it doesn't take a coin from 500 but only uses the previous number before I cleared it?

  • 0
  On 26/05/2015 at 10:25, Mulrian said:

You don't need any new variables for this. Just update the one you already have - totalCoins  - back to 500.

I've placed this code lblpoints.setText(String.valueOf(totalcoins)); in the Reset button but it still doesn't reset totalcoins back to 500 when I click the button, maybe I'm doing something wrong?

  • 0
  On 26/05/2015 at 11:05, Mulrian said:

You are just updating the component to display the current value of the variable. You are not actually changing the value of the variable.

I don't know the code to change the value of the variable, can you help me please as I can't seem to solve it for few days

  • 0
  On 26/05/2015 at 12:29, Mulrian said:
variableName = newValue;

To be honest if you don't know how to do that you obviously have some pretty major gaps in your knowledege and need to go back and learn the basics.

 

I think I'm nearly there, I've placed this code lblpoints.setText(String.valueOf(totalcoins = 500)); in the Reset button and it does reset back to 500 when I click on the reset button however the ocoins label doesn't reset and when I click on the add button it will add onto the previous reset number

  • 0
  On 26/05/2015 at 16:11, Litherz said:

I think I'm nearly there, I've placed this code lblpoints.setText(String.valueOf(totalcoins = 500)); in the Reset button and it does reset back to 500 when I click on the reset button however the ocoins label doesn't reset and when I click on the add button it will add onto the previous reset number

 

Here:

public class coins extends javax.swing.JFrame {
    final int COINS_MAX = 500;
    int totalCoins;
    int oCoins;
    
    public static void main() {
        resetCoins();
    }
    
    private void resetActionPerformed(java.awt.event.ActionEvent evt) {
        resetCoins();
    }
    
    private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {
        if (oCoins < COINS_MAX) {
            oCoins += 1;
            totalCoins -= 1;
            refreshDisplayedCoins();
        } else {
            JOptionPane.showMessageDialog(null, "you have reached the limit");
        }
    }
    
    private void btnminusActionPerformed(java.awt.event.ActionEvent evt) {
        if (oCoins > 0) {
            oCoins -= 1;
            totalCoins += 1;
            refreshDisplayedCoins();
        } else {
            JOptionPane.showMessageDialog(null, "you have reached the limit");
        }
    }
    
    private void resetCoins() {
        oCoins = 0;
        totalCoins = COINS_MAX;
        refreshDisplayedCoins();
    }
    
    private void refreshDisplayedCoins() {
        lblpoints.setText(String.valueOf(totalCoins));
        coinpoints.setText(String.valueOf(oCoins));
    }
}
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.