I have question about my homework the coin program.
We need write a small GUI program which could flip the coin and can show the current money.
I wrote almost everything, but I still have some problem to update two JLabel
status and it seems like the two bet buttons and reset the button are work fine, after I use System.out.println
to testing, but the label just not update as the same time when we click the button.
Here is my codes, which contain four class: coin.java
, player.java
, coinpanel.java
and coinPanelMain.java
.
player.java:
public class Player {
/** * @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
import javax.swing.; import java.awt.event.; import java.awt.; /*
- the coin panel class displays the result of the coin game.
- it contain three button, current money and current flip
- once user click reset button, the current money will return to 10. * * */
public class CoinPanel extends 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();
}
}
}
}
thank you very much. I am really really appreciate your help!
After re-edit code followed by instruction here is some error display as following, the application could not run. And I don't know why. the error is:
"
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)
" above question was solved. I forget initialized the label3 in the class..
Sorry for bring you too many questions... whatever the user input in text field (i mean the number), the current money label always increase 2 or decrease 2. I think it supposed change as the user input?