我正在尝试制作一个简单的项目,该项目会根据您的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);
}
}
}
在此先感谢您为解决/解决方案所做的任何尝试。
最佳答案
这段代码有很多结构性问题,但最严重的问题是您有两个左右浮动的Cointracker实例。处理程序仅更新GUI实例,而RollProgress没有对该实例的引用。为RollProgress创建一个构造函数,并传递GUI中使用的CoinTracker。
CoinTracker ctorp ;//= new CoinTracker();
public RollProgress(CoinTracker ct){
this.ctorp = ct;
}
处理程序正在其他工作。