• 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.
  • Posts

    • New Outlook for Windows landing in Microsoft 365 Education accounts next January by Paul Hill Microsoft has announced that from January 2026, Microsoft 365 Education users will be able to switch to a “modern, AI-powered” Outlook for Windows. The Redmond giant said that a new toggle will show up for eligible users that will let them decide whether to run classic Outlook or the new Outlook for Windows. The new version, Microsoft said, brings numerous improvements including more streamlined communication, features to boost your productivity, and more ways to personalize your experience. As this update is coming to Microsoft 365 Education accounts, it means it’ll affect both students and teachers. The refreshed interface more deeply integrates with Microsoft 365 Copilot to deliver: smarter email and calendar management with features such as Pinning and Snoozing; personalized themes that match your style and preferences; and Copilot-powered assistance to help you draft, summarize, and organize your emails and appointments. Microsoft is obviously keen for users to switch over to the new interface when it's available to foist AI on you. However, if you are reticent about using these AI tools, you can always revert back to the classic Outlook for Windows, at least for a while after the new version is released. The new toggle will begin rolling out globally next January. Before then, Microsoft 365 Education users will get in-app notifications ahead of the change to prepare them, and educational organizations will have the power to opt out or manage the experience through administrator settings. While most Microsoft 365 Education users will get the toggle, there are some exceptions. These include: organizations that have opted out of automatic migration; users with perpetual licenses; on-premises account holders; and devices where the toggle is hidden via policy. The upcoming changes will not require any administrative action during the rollout. Microsoft says administrators, if they want to, can notify users about the upcoming change; update their internal documentation; and review the documentation for Admin control over migration. The firm said that after the rollout, policy controls will become available through Group Policy Objects (GPO), Cloud Policy, and Intune. In terms of compatibility with other platforms, the new Outlook for Windows will be compatible, including with Mac. Users will still be able to access their accounts normally through the Outlook app available for macOS or via the Outlook web app.
    • Might be a joke to a shortsighted person, but that's the minimum for ANY EV to be as reliable and hassle-free as my '12 Highlander Hybrid. Sure my Hybrid gets even less at 372mi (in the winter) - 455mi (in the summer) on a tank, but at least I can refill it for another 372mi-455mi in merely 10 minutes or less without ANY of the following worries: Without all of that peace of mind, an EV will require 800mi/charge to give similar peace of mind for me. I ain't buying and worrying about separate vehicles just for city commuting and long road trips. That's a total waste of cash IMO. My Highlander Hybrid perfectly suits both use cases, and for any EV to be worth my consideration, they'll have to suit them too. Maybe for you that solely resides in 1 city and never travel by road it makes no sense, but for anyone with experience of traveling across cities here in Canada, it makes total sense given the long distances. I ain't suddenly adding another 1hr to my road trip just for EVs, mere coffee and snacks suffice for my trips (~800km), and those certainly don't take me 1hr.
    • Microsoft will reportedly hit Xbox division with a massive wave of layoffs soon by Pulasthi Ariyasinghe It was only a few days ago that a report came out regarding a mass layoff wave at Microsoft. While that report only mentioned sales staff that were to be affected, now, another report has landed that mentions the company's Xbox gaming division being another layoff target. According to Bloomberg's Jason Schreier, Microsoft has another major round of layoffs planned for the Xbox division as it reorganizes the company ahead of the new fiscal year. This unfortunate news for many staff will be announced as soon as next week, per the report Bloomberg has gained the information from sources that had asked not to be named, who had said that managers inside Xbox are already expecting the job cuts to take effect soon. While no specific developers, divisions, or numbers were mentioned, "substantial cuts" are reportedly incoming as a part of this change. The gaming division of Microsoft has gone through multiple layoff waves in recent years. Late 2024 saw 650 jobs being cut from Xbox corporate and supporting teams. Earlier in the same year, over 1,900 staff from Activision Blizzard, ZeniMax, and Xbox development teams were let go from Microsoft. Microsoft has released a number of high-profile first games as of late, including Doom: The Dark Ages, Avowed, and Indiana Jones and the Great Circle, and is also pursuing a multi-platform strategy that has Xbox games releasing on PlayStation platforms. It has also confirmed next-gen Xbox hardware is now in development and recently unveiled a first-ever official Xbox handheld initiative. If the latest report turns out to be accurate, we should have an official announcement sometime next week. Microsoft's current fiscal year is slated to end on June 30.
    • LibreOffice is fine for individual use, but its collaboration tools are far behind Office 365.
    • First thing I wondered as well. I miss the old color. I always thought it was nice how each app had its own color. Then they go and take 2 of the most commonly used apps and give them the same color.
  • Recent Achievements

    • Week One Done
      Sharon dixon earned a badge
      Week One Done
    • Dedicated
      Parallax Abstraction earned a badge
      Dedicated
    • First Post
      956400 earned a badge
      First Post
    • Week One Done
      davidfegan earned a badge
      Week One Done
    • First Post
      Ainajohn earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      593
    2. 2
      ATLien_0
      223
    3. 3
      Michael Scrip
      170
    4. 4
      +FloatingFatMan
      152
    5. 5
      Som
      135
  • Tell a friend

    Love Neowin? Tell a friend!