• 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

    • Jony Ive is reportedly working on multiple AI devices for OpenAI, including a mobile gadget by Hamid Ganji Last month, OpenAI announced acquiring Jony Ive's startup, io, in a $6.5 billion deal. Ive, who served as Apple's design chief for over two decades, moved to OpenAI to work on the company's AI hardware projects. While the details of these projects have remained relatively untold, Mark Gurman's Power On newsletter revealed some exciting insights about Jony Ive's work at OpenAI. According to Gurman, Apple's former design chief is now spearheading a series of groundbreaking AI hardware products for OpenAI. One of these under-development devices is described as a "mobile gadget" that takes the form of a "pendant" and can be worn around the neck. This device could allegedly allow users to access ChatGPT with voice commands. Another of Ive's works at OpenAI is a home device that functions similarly to a smart speaker. Users can put the device on a table and perform various tasks using OpenAI's ChatGPT. Interestingly, OpenAI also has a robot device under development. As Gurman says, this robot "will likely be a machine that develops a relationship with a human using AI." However, OpenAI's robot is still far from being made available on the market. After acquiring Jony Ive's startup, OpenAI CEO Sam Altman described Ive as the best designer in the world, adding that they can now work on the next generation of AI-powered computers. More details about OpenAI-Ive hardware products could emerge over the next few months. OpenAI was already rumored to be working on a mysterious AI gadget, presumably a screenless AI phone, which might be the same mobile gadget that Gurman revealed. While details of this alleged AI mobile are yet unknown, such a device could pose an alternative to conventional smartphones, raising the alarm for smartphone makers such as Apple, Samsung, and Google. Meanwhile, Apple might already be expecting such a device, as its services chief Eddy Cue allegedly said during his testimony at Google's antitrust case that iPhones might no longer exist 10 years from now.
    • Seems Nintendo have already thought of that after the Joy-Con drift in the Switch 2 EULA: Clause 16 of the EULA reads: "This arbitration provision precludes you and Nintendo from suing in court, having a trial by jury, or participating in a class action. You and Nintendo agree that arbitration will be solely on an individual basis and not as a class arbitration, class action, or any other kind of representative proceeding. You and Nintendo are each waiving the right to trial by a jury."
    • Cool glass phone you have in that silicon case bro
    • I can't say I ever noticed an OEM putting effort into user-friendly descriptions and icons who didn't also put effort into making good drivers. Usually, missing icons and descriptions was also a sign of bad drivers.
  • Recent Achievements

    • First Post
      brynmot earned a badge
      First Post
    • Reacting Well
      brynmot earned a badge
      Reacting Well
    • Week One Done
      Al_ earned a badge
      Week One Done
    • Week One Done
      MadMung0 earned a badge
      Week One Done
    • Reacting Well
      BlakeBringer earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      477
    2. 2
      +FloatingFatMan
      274
    3. 3
      ATLien_0
      243
    4. 4
      snowy owl
      209
    5. 5
      Edouard
      185
  • Tell a friend

    Love Neowin? Tell a friend!