Search code examples
javajbuttonactionlistener

ActionListener only working once and others never


I'm trying to make a simple project that generates a random amount of "copper" depending on how much your rollProgress is. Whenever you get 100 copper you can convert it to silver and 100 silver can be converted to gold. To increase the rollProgress you need to click a button to add either one, two, five, or ten and "pay" a set amount of silver and copper.

The generate button works every time, as well as the copperToSilver button so I removed the code for it compact it down. I also removed the Text Fields from the GUI class which showed the amount of copper/silver/gold that you currently have.

The problem is that addRoll ActionListener is only working when I press it the first time and thereafter it doesn't work regardless of whether it meets the requirements. The project is not complete but the rest of the things which are complete doesn't seem to work at all.

I removed my temporary debugging but it seems that the RollProgressPlusTwo method doesn't run at all when you press the addTwoRoll button, which it should.

Here's the code:

Main Class:

import javax.swing.JFrame;

public class MainClass {    
    public static void main(String args[]) {
        GUI go = new GUI();
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.setSize(350,300);
        go.setResizable(true);
        go.setLocationRelativeTo(null);
        go.setVisible(true);
    }
}

GUI Class:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class GUI extends JFrame {
    Button bo = new Button();
    CoinTracker cto = new CoinTracker();
    TextFields tfo = new TextFields();
    RollProgress rpo = new RollProgress();

    public GUI() {
        super("Money dice");
        setLayout(new GridLayout(11,1,3,3));

        add(bo.generate);

        add(tfo.currentCashField);
        add(tfo.goldField);
        add(tfo.silverField);
        add(tfo.copperField);

        add(bo.copperToSilver);
        add(bo.silverToGold);
        add(bo.addRoll);
        add(bo.addTwoRoll);
        add(bo.addFiveRoll);
        add(bo.addTenRoll);

        GenerateHandler ghandler = new GenerateHandler();
        bo.generate.addActionListener(ghandler);

        ConvertToSilverHandler ctshandler = new ConvertToSilverHandler();
        bo.copperToSilver.addActionListener(ctshandler);

        PlusOneHandler pohandler = new PlusOneHandler();
        bo.addRoll.addActionListener(pohandler);

        PlusTwoHandler ptwhandler = new PlusTwoHandler();
        bo.addTwoRoll.addActionListener(ptwhandler);
    }

    private class GenerateHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            Random roll = new Random();
            cto.copper = cto.copper+1+roll.nextInt(rpo.rollProgress);
            tfo.copperField.setText(cto.copper + " copper");
            tfo.silverField.setText(cto.silver + " silver");
            tfo.goldField.setText(cto.gold + " gold");
        }
    }

    private class PlusOneHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            rpo.RollProgressPlusOne();
            tfo.copperField.setText(cto.copper + " copper");
            tfo.silverField.setText(cto.silver + " silver");
            tfo.goldField.setText(cto.gold + " gold");
        }
    }

    private class PlusTwoHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            rpo.RollProgressPlusTwo();
            tfo.copperField.setText(cto.copper + " copper");
            tfo.silverField.setText(cto.silver + " silver");
            tfo.goldField.setText(cto.gold + " gold");
        }
    }

    private class ConvertToSilverHandler implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            cto.toSilver();
            tfo.copperField.setText(cto.copper + " copper");
            tfo.silverField.setText(cto.silver + " silver");
            tfo.goldField.setText(cto.gold + " gold");
        }
    }
}

CoinTracker Class:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class CoinTracker {
    public int copper = 98;
    public int silver = 1;
    public int gold = 0;

    public void toSilver() {
        if(copper >= 100) {
            copper -= 100;
            silver++;
        }
    }
}

Button Class:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class Button extends JFrame {
    public JButton generate;
    public JButton copperToSilver;
    public JButton silverToGold;
    public JButton addRoll;
    public JButton addTwoRoll;
    public JButton addFiveRoll;
    public JButton addTenRoll;

    public Button() {
        generate = new JButton("Generate");
        copperToSilver = new JButton("Convert 100 copper to 1 silver");
        silverToGold = new JButton("Convert 100 silver to 1 gold");
        addRoll = new JButton("Pay 1 silver to add one number to the dice");
        addTwoRoll = new JButton("Pay 2 silver to add two numbers to the dice");
        addFiveRoll = new JButton("Pay 4 silver and 50 copper to add five numbers to the dice");
        addTenRoll = new JButton("Pay 9 silver to add ten numbers to the dice");
    }
}

TextFields Class:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class TextFields {
    public JTextField currentCashField;
    public JTextField copperField;
    public JTextField silverField;
    public JTextField goldField;
    CoinTracker ctotf = new CoinTracker();

    public TextFields() {
        currentCashField = new JTextField("Your current cash is:");
        copperField = new JTextField(ctotf.copper + " copper");
        silverField = new JTextField(ctotf.silver + " silver");
        goldField = new JTextField(ctotf.gold + " gold");   
    }
}

RollProgress Class:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class RollProgress {
    public int rollProgress = 1;
    CoinTracker ctorp = new CoinTracker();

    public void RollProgressPlusOne() { 
        if(ctorp.silver >= 1) {
            rollProgress++;
            ctorp.silver--;
            System.out.println(rollProgress);
        }
    }

    public void RollProgressPlusTwo() {
        if(ctorp.silver >= 2) {
            rollProgress++;
            rollProgress++;
            ctorp.silver--;
            ctorp.silver--;
            System.out.println(rollProgress);
        }
    }
}

Thanks in advance for any attempts at solving/solutions.


Solution

  • There are a lot of structural issues with this code, but the acute issue is that you have two instances of Cointracker floating around. Only the GUI instance is getting updated by handlers and RollProgress doesn't have a reference to that instance. Create a constructor for RollProgress and pass the CoinTracker used in GUI.

    CoinTracker ctorp ;//= new CoinTracker();
    
    public  RollProgress(CoinTracker ct){
        this.ctorp = ct;
    }
    

    The handlers are working otherwise.