Java Blackjack Game

Mabel Ling

Get Help

Table of Contents

In this workshop, I'll show you how to make the game Blackjack using Java. You'll need to first download Netbeans and a JDK for this tutorial!

Setting Up the GUI

Click this button to learn how to set up a jFrame project in Netbeans:

Set Up Netbeans

Creating the GUI

Before you start this section, make sure you have a jFrame set up in Netbeans. Go to the Design tab of your project.

Then, from the panel on the left or bottom of the screen, drag 3 Buttons and 3 Text Fields from the "Swing Controls" section into the square.

The variable names of the text boxes and buttons can be edited by right clicking the object and choosing "Change Variable Name". Make sure the names are different and are easy to remember so you can keep track. Another thing to note is that the names of the buttons and text boxes can be edited as well. Changing this will change the text that appears on the buttons when your project is run, but it does effect the code. On the left side of your screen, there is a list of all created objects on the jFrame with their variable names in case you forget.

Your list of objects should now look something like this:

Your GUI (graphic user interface) should look somewhat similar to this:

Now that your GUI is set up, we can now begin coding for the buttons!

Coding the Buttons

Let's start with the variables. Click the "Source" tab beside the "Design" tab to begin.

Underneath the line that says public class Blackjack extends javax.swing.JFrame { , variables can be added. For this project, we need 4 variables. Name the variables as follows:



int dealer = 0; int player = 0; int card = 0; int blackjack = 21;

Now, our variables are ready for use. Go back to the design tab and double click the "Deal" button. When the "Deal" button is clicked while our program is running, we want to start a new round of Blackjack.

Under private void buttonDealActionPerformed(java.awt.event.ActionEvent evt) { , type this code:



buttonStand.setEnabled(true); buttonHit.setEnabled (true);
textStatus.setText(""); textDealer.setText("");
player = 0; textPlayer.setText("" + player);
for (int k=0; k<2; k++) { card = (int)(Math.random()*13)+1; if (card > 10) { card = 10; player += card; textPlayer.setText("" + player); } }

This code allows for the player to be "dealt" 2 cards, and the total is added up and shown in the textPlayer text box. There is no deck coded for this project, but it is simulated using the probability of being dealt a card.

Once the code for "Deal" is complete, we can move onto the code for "Hit". Go back to the "Design" tab again and double click the "Hit" button. Under private void buttonHitActionPerformed(java.awt.event.ActionEvent evt) { , type in this code:



player += card; textPlayer.setText("" + player);
if (player > blackjack) { textStatus.setText("Bust!"); buttonHit.setEnabled(false); buttonStand.setEnabled(false); }

This code means that a card will be added to the player's total. If the player's total is higher than 21, the status box will say "Bust!", and the game must reset.

Finally, the code for the stand button is the final part before the game is complete. Once again, go back to the "Design" tab, but this time double click the "Stand" button.

Type in this code under private void buttonStandActionPerformed(java.awt.event.ActionEvent evt) {. This code will first let the dealer add value to his total. In this case, the dealer will only hit if their total is below or equal to 16.

This section also shows the dealer's total (which should be hidden up until this point) and changes the message in the status text box.



for (int k=0; k<2; k++) { card = (int)(Math.random()*13)+1; if (card > 10) { card = 10; dealer += card; }
if (dealer < 16) { dealer += card; } }
textDealer.setText("" + dealer); if (player > dealer) { textStatus.setText("Push! It's a tie!"); }
else if (player == dealer) { textStatus.setText("You win!"); }
else { textStatus.setText("You lost!"); }
buttonHit.setEnabled(false); buttonStand.setEnabled(false);

Your code is now finished!

Running the Project

To be able to test your project, right click anywhere on the "Source" tab and press "Run File" or hit Ctrl + F6 on your keyboard. If something is not working, or you receive an error, read over your code for bugs.

Congratulations! Your Blackjack project is now complete! Feel free to add on to this project, such as by simulating a deck that can be shuffled, or making the dealer more complex, or adding a money system!

Back to Workshops