【发布时间】: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