ActionListener只工作一次而其他人从不工作

时间:2014-10-17 14:14:27

标签: java jbutton actionlistener

我正在尝试创建一个简单的项目,根据rollProgress的大小生成随机数量的“铜”。每当你获得100铜时,你可以将其转换为银,100银可以转换为金。要增加rollProgress,您需要单击一个按钮添加一个,两个,五个或十个并“支付”一定量的银和铜。

每次都会生成“生成”按钮,以及copperToSilver按钮,因此我删除了代码,将其压缩。我还从GUI类中删除了文本字段,其中显示了您当前拥有的铜/银/金的数量。

问题是addRoll ActionListener仅在我第一次按下它时才起作用,此后无论它是否满足要求它都不起作用。该项目尚未完成,但其他完整的事情似乎根本不起作用。

我删除了我的临时调试,但是当你按下addTwoRoll按钮时,似乎RollProgressPlusTwo方法根本不运行。

以下是代码:

主类:

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类:

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类:​​

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++;
        }
    }
}

按钮类:

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类:

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课程:

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);
        }
    }
}

提前感谢任何解决/解决方案的尝试。

1 个答案:

答案 0 :(得分:1)

此代码存在许多结构性问题,但严重的问题是您有两个Cointracker实例浮动。处理程序仅更新GUI实例,而RollProgress没有对该实例的引用。为RollProgress创建一个构造函数,并传递GUI中使用的CoinTracker。

CoinTracker ctorp ;//= new CoinTracker();

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

处理人员正在以其他方式工作。