我正在尝试制作一个简单的项目,根据您的 rollProgress 数量生成随机数量的“铜”。每当你获得 100 铜时,你可以将其转换为银,100 银可以转换为金。要增加滚动进度,您需要单击按钮添加一、二、五或十,并“支付”一定数量的银和铜。
“生成”按钮每次都有效,“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");
}
}
文本字段类:
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);
}
}
}
提前感谢您尝试解决/解决方案。
最佳答案
此代码存在很多结构问题,但最严重的问题是您有两个 Cointracker 实例。处理程序仅更新 GUI 实例,并且 RollProgress 没有对该实例的引用。为 RollProgress 创建一个构造函数并传递 GUI 中使用的 CoinTracker。
CoinTracker ctorp ;//= new CoinTracker();
public RollProgress(CoinTracker ct){
this.ctorp = ct;
}
处理程序正在以其他方式工作。
关于java - ActionListener 只工作一次,其他的则永远不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26427002/