Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Applet Canvas TextField actionPerformed()

Status
Not open for further replies.

asiavoices

Programmer
Sep 20, 2001
90
CA
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(&quot;Bank:&quot;);
displayBank = new Label(&quot;500.00&quot;);
betLabel = new Label(&quot;You Bet:&quot;);

betField = new TextField(8);

/*------ the Buttons -------*/
hit = new Button(&quot;Deal!&quot;);
stand = new Button(&quot;Stand Pat&quot;);
shuffleDeck = new Button(&quot;Shuffle&quot;);
newGame = new Button(&quot;New Game&quot;);


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(&quot;Verdana&quot;, Font.PLAIN, 11);
bigFont = new Font(&quot;Verdana&quot;, 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 = &quot;Shuffle Complete...&quot;;
repaint();
}

else if (buttonClicked == newGame)
{
doNewGame();
}

} // end of actionPerformed()


.................

/* -----------------------------
doHit() method()

This method is called when the user clicks the &quot;Hit!&quot; 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


 
Hello again all,

Unfortunately, I still have not solved my problem.

I've tried annonymous inner classes and still with no success.

Any ideas?

Thank you,

Christopher


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top