【问题标题】:ActionListener only working once and others neverActionListener 只工作一次,其他人从不
【发布时间】:2014-10-17 14:14:26
【问题描述】:

我正在尝试制作一个简单的项目,该项目会根据您的 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);
    }
}

图形界面类:

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

提前感谢您尝试解决/解决方案。

【问题讨论】:

  • 尽快发布minimal compilable example 以获得更好的帮助
  • 没有人会想要阅读那么多代码。尝试将您的代码缩减为一个较小的示例.. 可能有 100 行长
  • 您的输出中是否有任何异常?
  • 发布 Cointracker。这段代码似乎工作正常。我将 System.out.println 放入您执行的每个操作中,它们都可以多次工作。
  • 我无法重现该问题。我复制并粘贴了代码,添加了一些 println,一切似乎都正常。您确定要重新编译还是遗漏了任何内容?

标签: java jbutton actionlistener


【解决方案1】:

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

CoinTracker ctorp ;//= new CoinTracker();

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

处理程序以其他方式工作。

【讨论】:

  • Button 和 TextFields 类没有真正的用途,除非您打算重用 GUI 的那部分。 Button 类更加令人困惑,因为它扩展了 JFrame,但不使用该扩展来做任何事情(而且 Button 是一个令人困惑的名称)。为什么不将 Button 和 Textfields 折叠到 GUI 中?每个领域都是公开的,面对封装。
猜你喜欢
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
相关资源
最近更新 更多