• 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

    • OpenAI exposes secret propaganda campaigns tied to multiple countries by David Uzondu Back in February, OpenAI shut down accounts that were busy developing Chinese surveillance tools aimed at the West. These tools were designed to snoop on social media, look for anti-China sentiment and protests, and report back to Chinese authorities. Now, OpenAI has announced it has disrupted even more shady operations, and not just those tied to China. In a report released Thursday, the company detailed how it recently dismantled ten different operations that were misusing its artificial intelligence tools. One of the China-linked groups, which OpenAI called "Sneer Review," used ChatGPT to churn out short comments for sites like TikTok, X, and Facebook. The topics varied, from U.S. politics to criticism of a Taiwanese game, where players work against the Chinese Communist Party. This operation even generated posts and then replied to its own posts to fake real discussions. What is particularly interesting is that the group also used ChatGPT to write internal performance reviews, describing how well they were running their influence campaign. Another operation with ties to China involved individuals posing as journalists and geopolitical analysts. They used ChatGPT to write social media posts and biographies for their fake accounts on X, translate messages from Chinese to English, and analyze data. OpenAI mentioned that this group even analyzed correspondence addressed to a U.S. Senator. On top of that, these actors used OpenAI's models to create marketing materials, basically advertising their services for running fake social media campaigns and recruiting intelligence sources. OpenAI also disrupted operations, probably originating in Russia and Iran. There was also a spam operation from a marketing company in the Philippines, a recruitment scam linked to Cambodia, and a deceptive job campaign that looked like something North Korea might orchestrate. Ben Nimmo, from OpenAI's intelligence team, noted the wide range of tactics and platforms these groups are using. However, he also said these operations were mostly caught early and did not manage to fool large numbers of real people. According to Nimmo, "We didn't generally see these operations getting more engagement because of their use of AI. For these operations, better tools don't necessarily mean better outcomes."
    • Long ago, I was in a networking class on a lab computer. The guy next to sarcastically told me to SHIFT+DELETE the C:\Windows folder. I said that I was sure Windows wouldn't allow such a thing (Windows 2000), and would either totally block the action or give some kind of dire warning. I was so confident that I tried it...not only was I wrong, but it didn't even give the standard "are you sure" warning, just went to town. I pressed cancel as quick as I could, but it was too late, shortly after, the system blue-screened and never booted again. I had to stay late and reinstall Windows for the teacher, but that ended up being a good thing, had great repour with him for the rest of the year, even got to help him get Active Directory setup in his lab.
    • My best decision: SHIFT+DELETE WINDOWS Then Installed Fedora Linux. Now I am a Happy Person
    • I value a game as a whole including graphics. But going backwards in terms of graphics with a game I'm not already invested in (as in something I played and loved in the past and still do) is off-putting. Its a very poor demo trailer if they didn't crank the settings to max that would be ridiculous. Different strokes for different folks i guess, i'm not into slop the trailer and likely game could have been far better, it doesn't it instead looks like it was made on a shoe string budget or with lack of experience. But that could also be down to trying to get it running on low budget/outdated hardware and sacrificing the top end.
    • > Our goal is to ensure that the App Store remains an outstanding opportunity for developers and a safe, trusted experience for our users. There are so many scam apps on the platform that it is hard to believe they are truly interested in having a safe, trusted experience for their users.
  • Recent Achievements

    • One Year In
      survivor303 earned a badge
      One Year In
    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      419
    2. 2
      +FloatingFatMan
      182
    3. 3
      snowy owl
      181
    4. 4
      ATLien_0
      176
    5. 5
      Xenon
      136
  • Tell a friend

    Love Neowin? Tell a friend!