我对硬币计划的作业有疑问。
我们需要编写一个小 GUI 程序,它可以翻转硬币并显示当前货币。
我写了几乎所有的东西,但是更新两个JLabel
状态仍然有一些问题,似乎两个下注按钮和重置按钮工作正常,经过我System.out.println
的测试,但是当我们单击时标签不会同时更新按钮。
这是我的代码,其中包含四个类:coin.java
、player.java
和。coinpanel.java
coinPanelMain.java
播放器.java:
公共类播放器{
/** * @param ownMoney is currently the player own money * @param coin is new Coin object; */ private int currMoney; private Coin coin; /** * no-args parameter * default constructor */ public Player(){ currMoney = 10; coin = new Coin(); } /** * a bet method that takes in a bet and the side of coin * it will filp the coin and change the player's money * depend on whether the player won or lost the bet */ public void bet(){ coin.flip(); System.out.println("filp over"); if(coin.getFace().equals ("Heads")){ currMoney ++; } else if(coin.getFace().equals("Tails")){ currMoney --; } System.out.println("filp over2"); } /** * a getter for getting current money * @return currMoney */ public int getCurrMon(){ System.out.println("money is" + currMoney); return currMoney; } /** * a reset method make current money return to 10; * @return currMoney to 10 */ public void reset(){ currMoney = 10; }
}
coinPanel.java
导入 javax.swing。; 导入 java.awt.event。; 导入 java.awt。; / *
- 硬币面板类显示硬币游戏的结果。
- 它包含三个按钮,当前货币和当前翻转
- 一旦用户点击重置按钮,当前货币将返回到 10。 * * */
公共类 CoinPanel 扩展 JPanel {
private Player player = new Player();
private Coin coin = new Coin();
private JLabel label3 = new JLabel("Enter a bet");
private JTextField text;
private int value = 0;
public int getVal(){
return value;
}
public CoinPanel(){
JLabel label= new JLabel("Current Money:"+player.getCurrMon());
JLabel label2 = new JLabel("Current Flip:" + coin.getFace());
JLabel label4 = new JLabel("");
text = new JTextField(30);
//JTextField text = new JTextField(30);
//String betNum = text.getText();
//int betNumber = Integer.parseInt(betNum);
JButton headsBt = new JButton("Bet Heads");
JButton tailsBt = new JButton("Bet Tails");
JButton reset = new JButton("Reset");
setLayout(new GridLayout(5,1,10,10));
add(label);
add(label2);
add(headsBt);
add(tailsBt);
add(text);
add(reset);
add(label3);
headsBt.addActionListener(new BetButtonListener());
tailsBt.addActionListener(new BetButtonListener());
reset.addActionListener(new RESETButtonListener());
}
public class RESETButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
player.reset();
System.out.println("reset button");
}
}
public class BetButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
//value = Integer.parseInt(text.getText());
player.bet();
int value = Integer.parseInt(text.getText());
//catch (NumberFormatException e){
if(value > player.getCurrMon()){
label3.setText("You are out of money");
repaint();
}
}
}
}
非常感谢你。我真的很感谢你的帮助!
重新编辑代码后跟 这里的指令是一些错误显示如下,应用程序无法运行。我不知道为什么。错误是:
"
money is10
face isTails
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at CoinPanel.<init>(CoinPanel.java:48)
at CoinPanelMain.main(CoinPanelMain.java:17)
" 上面的问题已经解决了。我忘记在课堂上初始化 label3 了..
很抱歉给您带来太多问题......无论用户在文本字段中输入什么(我的意思是数字),当前的货币标签总是增加 2 或减少 2。我认为它应该随着用户输入而改变?