• 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

    • I think they mean a phone like the s6 edge where it breaks on first drop guarantee
    • This high-end GEEKOM Mini IT12 (2025 Edition) PC has been slashed by $200 by Steven Parker GEEKOM reached out to let us know of a discount it is running on its site in the U.S., where you can save $200 off the i7 model of Mini IT12 2025 Edition. That brings the already discounted price of $699 down to just $499; buying link below. Below are the full specifications of the variant on offer GEEKOM Mini IT12 (2025 Edition) Dimensions Size 117 x 112 x 45.6mm Weight 652g CPU Intel Core i7-1280P (14 Cores, 20 Threads, 24MB Cache, up to 4.80 GHz) Graphics Intel® Iris® Xe Graphics Memory 32 GB Dual-channel DDR4-3200 SODIMM; expandable up to 64GB Storage 1 TB x M.2 2280 PCIe Gen 4 x4 SSD, expandable up to 2TB 1 x M.2 2242 SATA SSD slot, expandable up to 1TB Operating System Windows 11 Pro Bluetooth Bluetooth® v5.2 Ethernet Intel® 10/100/1000/2500 Mbps RJ45 Ethernet Wireless LAN Intel® Wi-Fi 6E AX211 Kensington Lock Yes Adapter 19V power adapter, 90W, with geo-specific AC cord (IEC C5) I/O Ports 3 x USB 3.2 Gen 2 ports 1 x USB 2.0 port 2 x USB4 ports 1 x SD card reader 1 x 3.5 mm headphone jack 1 x 2.5GbE LAN port 2 x HDMI 2.0 ports 1 x DC jack 1 x Power button MSRP $699 (see below for discount price) You may remember that we reviewed the i7-1260P variant in 2023. Here are our initial impressions of the Mini IT12 at the time. Once you have the PC out of the cushioning inside the box and the foam removed, you are greeted with a Thank You envelope. Below that, after removing the cardboard "shelf," you can find the other components, such as the power lead, HDMI cable, VESA mount plate with a bag of screws, and the instruction manual. What’s In The Box 1 x Mini IT12 Mini PC 1 x VESA Mount 1 x Power Adapter 1 x HDMI Cable 1 x User Guide 1 x Thank You Card As you can see, one HDMI cable is included in the box. Since the port is not HDMI 2.1, you will need to consider purchasing a mini DisplayPort cable or a USB4 (Type-C) to DisplayPort cable to maximize the potential of the Iris Xe Graphics display options. In addition, GEEKOM offers a one-year full warranty on its products, and if needed, you can RMA or return them locally relative to your region (the U.S. has a U.S. warehouse, and the E.U. has a Germany warehouse). Buy the i7-1280P Mini IT12 (2025 Edition) for $499 (was $699) at GEEKOM U.S. Buy the i7-1280P Mini IT12 (2025 Edition) for $499 (was $699) at Amazon U.S. When checking out, use the $30 in-page coupon or NEOIT122025 coupon code. Best of all, the shipping is quick and free.
    • That's ######ing hilarious! And it sure works when you look at both of their faces.
    • When it comes to games specifically, sure, but until now the main focus has been on doing work. All you have to do is look at how hard they're pushing AI in the productivity space to see that they've got their enterprise users in mind 1st with gamers lower on the list. Now that should all change, at least for custom gaming devices like handhelds and even, I expect, custom mini-PCs that are like consoles you can put under your TV. The whole "Xbox PC" branding they had around the show says a lot IMO.
    • I'm excited to check this out. I never played the first version, but I did just finish playing through Smalland, and while I liked it, I found myself wishing for more engaging content.
  • Recent Achievements

    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
    • Week One Done
      VicByrd earned a badge
      Week One Done
    • Reacting Well
      NeoToad777 earned a badge
      Reacting Well
    • Reacting Well
      eric79XXL earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      470
    2. 2
      +FloatingFatMan
      282
    3. 3
      ATLien_0
      249
    4. 4
      snowy owl
      202
    5. 5
      Edouard
      196
  • Tell a friend

    Love Neowin? Tell a friend!