asiavoices
Programmer
Hello all,
I have a peculiar problem with my applet in that my applet does not seem to accept a value in a TextField.
Background:
1. My application is a Blackjack game.
2. The TextField in question :-( is the one I use to have people enter the dollar amount of their bet.
3. There are other buttons (Deal, Stand, Shuffle and New Game) which all work accordingly.
Question:
1. What am I doing wrong in not having my program recognize or accept values in the TextField? Remember... everything works but now I want to have the ability to have users enter an amount to bet.
Here's a snippet of my code:
/* ==================================
= my code =
================================== */
import java.awt.*;
import java.awt.Image;
import java.awt.event.*;
import java.applet.*;
public class Blackjack extends Applet
{
Button hit;
Button stand;
Button shuffleDeck;
Button newGame;
BJCanvas board;
Panel buttonPanel;
Label bankLabel;
Label displayBank;
Label betLabel;
TextField betField; <--- the problem!
private double betEntered = 0.00d;
public void init()
{
/*------ the labels -----------*/
bankLabel = new Label("Bank:"
displayBank = new Label("500.00"
betLabel = new Label("You Bet:"
betField = new TextField(8);
/*------ the Buttons -------*/
hit = new Button("Deal!"
stand = new Button("Stand Pat"
shuffleDeck = new Button("Shuffle"
newGame = new Button("New Game"
setBackground( new Color(0,50,40) );
setLayout( new BorderLayout(3,3) );
Panel buttonPanel = new Panel();
buttonPanel.setBackground( new Color(220,200,180) );
buttonPanel.setLayout( new GridLayout(1,8) );
add(buttonPanel, BorderLayout.NORTH);
/* --- my own canvas BJCanvas ---*/
BJCanvas board = new BJCanvas();
add(board, BorderLayout.CENTER);
buttonPanel.add(bankLabel);
buttonPanel.add(displayBank);
buttonPanel.add(betLabel);
betField.addActionListener(board);
buttonPanel.add(betField);
hit.addActionListener(board);
buttonPanel.add(hit);
stand.addActionListener(board);
buttonPanel.add(stand);
shuffleDeck.addActionListener(board);
buttonPanel.add(shuffleDeck);
newGame.addActionListener(board);
buttonPanel.add(newGame);
} // end init()
............
/* -------------------------------------------------
This class is NESTED in my Blackjack class. A class that displays the card game and does all the work of keeping track of the state and responding to user events.
------------------------------------------------- */
class BJCanvas extends Canvas implements ActionListener
{
Deck deck;
BlackjackHand dealerHand;
BlackjackHand playerHand;
String message;
boolean gameInProgress;
Font bigFont;
Font smallFont;
/* ------ my Constructor. Creates fonts and starts the first game. -------------*/
BJCanvas()
{
setBackground(new Color(255,204,102));
smallFont = new Font("Verdana", Font.PLAIN, 11);
bigFont = new Font("Verdana", Font.BOLD, 13);
doNewGame();
} // end of my constructor
......
/*---- actionPerformed() method -----
Respond when the user clicks on a button by calling the appropriate procedure. Note that the canvas is registered as a listener in the Blackjack class.
-------------*/
public void actionPerformed(ActionEvent buttonEvent)
{
Object buttonClicked = buttonEvent.getSource();
//If the HIT button was clicked
if (buttonClicked == hit)
{
doHit();
}
else if (buttonClicked == stand)
{
doStand();
}
else if (buttonClicked == shuffleDeck)
{
deck.shuffle();
message = "Shuffle Complete...";
repaint();
}
else if (buttonClicked == newGame)
{
doNewGame();
}
} // end of actionPerformed()
.................
/* -----------------------------
doHit() method()
This method is called when the user clicks the "Hit!" button. I want to display the text entered to see if it does work! :-(
----------------------------- */
void doHit()
{
double betEntered = (Double.parseDouble(betField.getText().trim()));
displayBank.setText(Double.toString(betEntered));
...
} // end of doHit() method
} // end nested class BJCanvas
} // End of Blackjack applet
any ideas?
Thank you,
Christopher
I have a peculiar problem with my applet in that my applet does not seem to accept a value in a TextField.
Background:
1. My application is a Blackjack game.
2. The TextField in question :-( is the one I use to have people enter the dollar amount of their bet.
3. There are other buttons (Deal, Stand, Shuffle and New Game) which all work accordingly.
Question:
1. What am I doing wrong in not having my program recognize or accept values in the TextField? Remember... everything works but now I want to have the ability to have users enter an amount to bet.
Here's a snippet of my code:
/* ==================================
= my code =
================================== */
import java.awt.*;
import java.awt.Image;
import java.awt.event.*;
import java.applet.*;
public class Blackjack extends Applet
{
Button hit;
Button stand;
Button shuffleDeck;
Button newGame;
BJCanvas board;
Panel buttonPanel;
Label bankLabel;
Label displayBank;
Label betLabel;
TextField betField; <--- the problem!
private double betEntered = 0.00d;
public void init()
{
/*------ the labels -----------*/
bankLabel = new Label("Bank:"
displayBank = new Label("500.00"
betLabel = new Label("You Bet:"
betField = new TextField(8);
/*------ the Buttons -------*/
hit = new Button("Deal!"
stand = new Button("Stand Pat"
shuffleDeck = new Button("Shuffle"
newGame = new Button("New Game"
setBackground( new Color(0,50,40) );
setLayout( new BorderLayout(3,3) );
Panel buttonPanel = new Panel();
buttonPanel.setBackground( new Color(220,200,180) );
buttonPanel.setLayout( new GridLayout(1,8) );
add(buttonPanel, BorderLayout.NORTH);
/* --- my own canvas BJCanvas ---*/
BJCanvas board = new BJCanvas();
add(board, BorderLayout.CENTER);
buttonPanel.add(bankLabel);
buttonPanel.add(displayBank);
buttonPanel.add(betLabel);
betField.addActionListener(board);
buttonPanel.add(betField);
hit.addActionListener(board);
buttonPanel.add(hit);
stand.addActionListener(board);
buttonPanel.add(stand);
shuffleDeck.addActionListener(board);
buttonPanel.add(shuffleDeck);
newGame.addActionListener(board);
buttonPanel.add(newGame);
} // end init()
............
/* -------------------------------------------------
This class is NESTED in my Blackjack class. A class that displays the card game and does all the work of keeping track of the state and responding to user events.
------------------------------------------------- */
class BJCanvas extends Canvas implements ActionListener
{
Deck deck;
BlackjackHand dealerHand;
BlackjackHand playerHand;
String message;
boolean gameInProgress;
Font bigFont;
Font smallFont;
/* ------ my Constructor. Creates fonts and starts the first game. -------------*/
BJCanvas()
{
setBackground(new Color(255,204,102));
smallFont = new Font("Verdana", Font.PLAIN, 11);
bigFont = new Font("Verdana", Font.BOLD, 13);
doNewGame();
} // end of my constructor
......
/*---- actionPerformed() method -----
Respond when the user clicks on a button by calling the appropriate procedure. Note that the canvas is registered as a listener in the Blackjack class.
-------------*/
public void actionPerformed(ActionEvent buttonEvent)
{
Object buttonClicked = buttonEvent.getSource();
//If the HIT button was clicked
if (buttonClicked == hit)
{
doHit();
}
else if (buttonClicked == stand)
{
doStand();
}
else if (buttonClicked == shuffleDeck)
{
deck.shuffle();
message = "Shuffle Complete...";
repaint();
}
else if (buttonClicked == newGame)
{
doNewGame();
}
} // end of actionPerformed()
.................
/* -----------------------------
doHit() method()
This method is called when the user clicks the "Hit!" button. I want to display the text entered to see if it does work! :-(
----------------------------- */
void doHit()
{
double betEntered = (Double.parseDouble(betField.getText().trim()));
displayBank.setText(Double.toString(betEntered));
...
} // end of doHit() method
} // end nested class BJCanvas
} // End of Blackjack applet
any ideas?
Thank you,
Christopher